Google放置api不返回几何

Google places api not returning geometry

本文关键字:返回 放置 api Google      更新时间:2023-09-26

我正在使用"google places autocomplete API"来使用javascript获取关于不同城市的数据:

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,lng)但是它有这个消息

TypeError: 'caller'和'arguments'是受限函数属性,不能在此上下文中访问。

函数。remoteFunction (:3:14)

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

我也遇到了同样的问题。我的Lat和Lng似乎是函数,但也是错误。为了获得实际的值,你必须调用lat和long as函数并在回调中将它赋值给一个变量。

  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
    }
  });

我似乎也有同样的问题。地图工作良好,昨天返回几何图形,今天只是空白,没有经纬度,和相同的错误。

似乎他们更新了API,有些东西坏了

编辑:

对我来说,它是可以通过采取标记坐标而不是试图将其地理编码为地址,然后采取地址坐标来修复的。

我有这样的代码(工作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);
                        } 
                    }
                );
            }

这个停止工作,但能够通过从标记中获取坐标来修复它。在我的例子中,Pos变量是marker.getPosition()。

                        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());
                        } 

似乎有报道https://code.google.com/p/gmaps-api-issues/issues/detail?id=8734

你可以试试:

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());
});

临时解决方案:

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