Search code examples
javascriptgoogle-mapsgoogle-geocoding-api

Google Geolocation JS API Giving Nonsense Lat/Long


I'm using the following code, with a valid API key, to obtain latitude and longitude from the Google Geocoder JS API:

<script async defer type="text/javascript"
    src="http://maps.google.com/maps/api/js?key=[key]">
</script>
<script>
    var geocoder = new google.maps.Geocoder();
    var address = "1600 Amphitheatre Parkway, Mountain View, CA";
    geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK)
        {
            console.log (results[0]);
            // results[0].geometry.location.lat
            // results[0].geometry.location.lng
        }
        else console.log(status, results);
    });
</script>

The query to the Google server works fine, and brings back results. The problem is that no matter what address I enter, location.lat comes back as _.E/this.lat() and location.lng comes back as _.E/this.lng(). The viewport coordinates are fine, but the actual latitude and longitude results are nonsense to me. The same thing happens if I put the code into a function and pass it as a callback.

Has anyone ever encountered this before? Is there something I'm missing? I can't find anything anywhere about this problem when I search, and this is my first time using the API.


Solution

  • results[0].geometry.location is a google.maps.LatLng. It doesn't have .lat/.lng properties, they are functions, you need to call them:

      var geocoder = new google.maps.Geocoder();
      var address = "1600 Amphitheatre Parkway, Mountain View, CA";
      geocoder.geocode({
        'address': address
      }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          console.log(results[0]);
          var lat = results[0].geometry.location.lat();
          var lng = results[0].geometry.location.lng();
          map.setCenter(results[0].geometry.location);
        } else console.log(status, results);
      });
    

    proof of concept fiddle

    code snippet:

    var geocoder;
    var map;
    
    function initialize() {
      var map = new google.maps.Map(
        document.getElementById("map_canvas"), {
          center: new google.maps.LatLng(37.4419, -122.1419),
          zoom: 13,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        });
      var geocoder = new google.maps.Geocoder();
      var address = "1600 Amphitheatre Parkway, Mountain View, CA";
      geocoder.geocode({
        'address': address
      }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          console.log(results[0]);
          var lat = results[0].geometry.location.lat();
          var lng = results[0].geometry.location.lng();
          var iw = new google.maps.InfoWindow();
          iw.setContent("lat:" + lat + "<br>lng:" + lng);
          iw.setPosition(results[0].geometry.location);
          iw.open(map);
          map.setCenter(results[0].geometry.location);
        } else console.log(status, results);
      });
    }
    google.maps.event.addDomListener(window, "load", initialize);
    html,
    body,
    #map_canvas {
      height: 100%;
      width: 100%;
      margin: 0px;
      padding: 0px
    }
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
    <div id="map_canvas"></div>