简体   繁体   English

Google Places API不返回几何

[英]Google places api not returning geometry

I am using "google places autocomplete API" to get data about different cities using javascript like that : 我正在使用“ google place 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);
});

The problem : the object doesn't have the (lat,lng) but it has this message 问题:对象不具有(lat,lng)但它具有此消息

TypeError: 'caller' and 'arguments' are restricted function properties and cannot be accessed in this context. TypeError:“ caller”和“ arguments”是受限制的函数属性,在此上下文中无法访问。

at Function.remoteFunction (:3:14) 在Function.remoteFunction(:3:14)

at Object.InjectedScript.callFunctionOn (:750:66) 在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 似乎他们更新了API,但出现了问题

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

It seems to be reported https://code.google.com/p/gmaps-api-issues/issues/detail?id=8734 似乎已报告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. 我的Lat和Lng似乎是函数,但也有错误。 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. 为了获得实际值,您必须实际调用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
    }
  });

Temporary solution: 临时解决方案:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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