Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
116
rated 0 times [  119] [ 3]  / answers: 1 / hits: 31262  / 12 Years ago, tue, january 29, 2013, 12:00:00

I was interested in getting my current address using Javascript and just figured this out by assembling some other SO threads (1,2) so wanted to post this question and answer.



Please see answer below.


More From » jquery

 Answers
16

Here's the HTML:



<script src=http://maps.google.com/maps/api/js?sensor=false></script>
<p id='latitudeAndLongitude'></p>
<p id='address'></p>


Here's the JS:



var latitudeAndLongitude=document.getElementById(latitudeAndLongitude),
location={
latitude:'',
longitude:''
};

if (navigator.geolocation){
navigator.geolocation.getCurrentPosition(showPosition);
}
else{
latitudeAndLongitude.innerHTML=Geolocation is not supported by this browser.;
}

function showPosition(position){
location.latitude=position.coords.latitude;
location.longitude=position.coords.longitude;
latitudeAndLongitude.innerHTML=Latitude: + position.coords.latitude +
<br>Longitude: + position.coords.longitude;
var geocoder = new google.maps.Geocoder();
var latLng = new google.maps.LatLng(location.latitude, location.longitude);

if (geocoder) {
geocoder.geocode({ 'latLng': latLng}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
console.log(results[0].formatted_address);
$('#address').html('Address:'+results[0].formatted_address);
}
else {
$('#address').html('Geocoding failed: '+status);
console.log(Geocoding failed: + status);
}
}); //geocoder.geocode()
}
} //showPosition

[#80557] Monday, January 28, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rocioblancac

Total Points: 699
Total Questions: 96
Total Answers: 108

Location: Libya
Member since Mon, Dec 7, 2020
4 Years ago
;