To get geo location OR longitude and latitude current location OR current Location of user

T

To get longitude and latitude of current location there is simple java script function given below —

<script>
/*Location functions here*/

function getUserLocation() {

//check if the geolocation object is supported, if so get position
if (navigator.geolocation)
navigator.geolocation.getCurrentPosition(displayLocation, displayError);
else
document.getElementById(“locationData”).innerHTML = “Sorry – your browser doesn’t support geolocation!”;
}

function displayLocation(position) {

//build text string including co-ordinate data passed in parameter
var displayText = “” + position.coords.latitude + ” ” + position.coords.longitude;

//display the string for demonstration
document.getElementById(“locationData”).innerHTML = displayText;

}

function displayError(error) {

//get a reference to the HTML element for writing result
var locationElement = document.getElementById(“locationData”);

//find out which error we have, output message accordingly
switch(error.code) {
case error.PERMISSION_DENIED:
locationElement.innerHTML = “Permission was denied”;
break;
case error.POSITION_UNAVAILABLE:
locationElement.innerHTML = “Location data not available”;
break;
case error.TIMEOUT:
locationElement.innerHTML = “Location request timeout”;
break;
case error.UNKNOWN_ERROR:
locationElement.innerHTML = “An unspecified error occurred”;
break;
default:
locationElement.innerHTML = “Who knows what happened…”;
break;
}}
</script>

<div id=”locationData” align=”center”>

</div>

By using this script you can get current Geo location .
to display  massages or  longitude and latitude to user , here i used last div.

Thanks ..!!

 

About the author

prasham.tated
By prasham.tated

Category