6

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.

Community
  • 1
  • 1
tim peterson
  • 23,653
  • 59
  • 177
  • 299

2 Answers2

17

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
Tony Hinkle
  • 4,706
  • 7
  • 23
  • 35
tim peterson
  • 23,653
  • 59
  • 177
  • 299
  • 1
    If the user is using a device without GPS ,eg PC, the location is most likely to be the location of IP adress.In my case the location is 7 miles out.[HTML5 Documentation](http://dev.w3.org/geo/api/spec-source.html#geolocation_interface) – david strachan Jan 29 '13 at 13:26
  • @davidstrachan thanks that is really good to know and appears to explain -@Cerbus's and -@Aspiring's comments above. – tim peterson Jan 29 '13 at 20:40
  • 2
    Great work @timpeterson it works very well. Just a little change to it... Using it as is, it redirects my browser on page load. So I removed the ',location = { ... };' part of the code as location in Javascript redirect your browser. after this, it works super fine. – Gabriel Ojomu Jun 25 '15 at 15:50
3

use HTML5 GeoLocation:

function GetGeolocation() {

navigator.geolocation.getCurrentPosition(GetCoords, GetError);

}


function GetCoords(position){

  alert(position.coords.latitude);

  alert(position.coords.longitude);

  alert(position.coords.accuracy);

 var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
geocoder.geocode({'latLng': latlng}, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK) {
    if (results[1]) {
      map.setZoom(11);
      marker = new google.maps.Marker({
          position: latlng,
          map: map
      });
      infowindow.setContent(results[1].formatted_address);
      infowindow.open(map, marker);
    }
  } else {
    alert("Geocoder failed due to: " + status);
  }
});

}
Talha
  • 18,898
  • 8
  • 49
  • 66