简体   繁体   中英

Google places api not returning geometry

I am using "google places autocomplete API" to get data about different cities using javascript like that :

var service = new google.maps.places.Autocomplete(document.getElementById('search-btn'));
service.addListener('place_changed', function () { 
    var place = service.getPlace();
    console.log(place.geometry.location);
});

The problem : the object doesn't have the (lat,lng) but it has this message

TypeError: 'caller' and 'arguments' are restricted function properties and cannot be accessed in this context.

at Function.remoteFunction (:3:14)

at Object.InjectedScript.callFunctionOn (:750:66)

I seem to have the same issue. Map worked fine and returned geometry yesterday, today it is just blank, no latitude/longitude, and the same error.

It seems they updated the API and something broke

EDIT:

For me it was fixable by taking marker coordinates instead of trying to geocode it to address and then taking coodinates of the address.

I had code like (worked 12.10.15):

function geocodePosition(pos) 
            {
               geocoder = new google.maps.Geocoder();
               geocoder.geocode
               ({
                    latLng: pos
                }, 
                    function(results, status) 
                    {
                        if (status == google.maps.GeocoderStatus.OK) 
                        {
                            $("#address").val(results[0].formatted_address);
                            $("#id_location_0").val(results[0].geometry.location.J);
                            $("#id_location_1").val(results[0].geometry.location.M);
                        } 

                    }
                );
            }

This one stopped working but was able to fix it by taking coordinates from marker. Pos variable is marker.getPosition() in my case.

                        if (status == google.maps.GeocoderStatus.OK) 
                        {
                            $("#address").val(results[0].formatted_address);
                            $("#id_location_0").val(marker.position.lat());
                            $("#id_location_1").val(marker.position.lng());
                        } 

It seems to be reported https://code.google.com/p/gmaps-api-issues/issues/detail?id=8734

You could try:

var service = new google.maps.places.Autocomplete(document.getElementById('search-btn'));
service.addListener('place_changed', function () { 
    var place = service.getPlace();
    console.log(place.geometry.location.lat(), place.geometry.location.lng());
});

I had the same problem. My Lat and Lng were appearing to be functions but also errors. In order to get the actual values, you have to actually call lat and long as functions and assign that to a variable in the callback.

  geocoder = new google.maps.Geocoder();
  geocoder.geocode({'address' : addressString}, function(results, status){
    if(status == google.maps.GeocoderStatus.OK){

         ///~~~~~~~~~The part you care about ~~~~~~~~~~///
          var lat = results[0].geometry.location.lat();
          var lng = results[0].geometry.location.lng();

      console.log(lat); // --> 37.7603726
      console.log(lng); // --> -122.47157
    }
  });

Temporary solution:

var lat = results[0]['geometry']['location'].lat();
var lng = results[0]['geometry']['location'].lng();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM