Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,

Im trying to get current location Lat,long and postcode from my desktop im getting default city Lat,long and postcode but i want the current located Lat,long and postcode

i tried this code...

if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(successCallback, errorCallback, {
maximumAge: 0
});
$('body').append("");
} else {
$('body').append("");
}

function successCallback(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
var geocoder = new google.maps.Geocoder();
var latLng = new google.maps.LatLng(latitude,longitude);
alert(latLng);
if (geocoder) {
geocoder.geocode({ 'latLng': latLng}, function (results,status) {
if (status.match(google.maps.GeocoderStatus.OK)) {
var address = results[0].address_components;
var zipcode = address[address.length - 1].long_name;
alert(zipcode);
//alert(results[0].formatted_address);

}

});
}
}


function errorCallback(error) {
var msg = "

Can't get your location. Error = ";
if (error.code == 1)
msg += "PERMISSION_DENIED";
else if (error.code == 2)
msg += "POSITION_UNAVAILABLE";
else if (error.code == 3)
msg += "TIMEOUT";
msg += ", msg = "+error.message;

$('body').append(msg);
}

Im getting the lat,long and postcode but not accurate one....

Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900