2015-10-13 17 views
1

我使用「谷歌的地方自動完成API」使用JavaScript這樣的,以獲取有關不同城市的數據:谷歌的地方API不返回幾何

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

問題:對象不具備的(緯度,經度)但它有這個消息

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)

+0

您的'search-btn'是一個按鈕? –

+0

@MrNeo它是一個文本框:'

+1

我臨時發現了一個解決方案,變量我請求谷歌地理編碼API使用位置地址(lat,lng): '$ .getJSON(「https://maps.googleapis.com/maps/api/geocode/json?address=」+ place。 formatted_address +「&key = myKey」,函數(數據,雕像)var geomtry_arr = [data ['results'] [0] .geometry.location ['lat'],data ['results'] [0]。 ();});' –

回答

2

我似乎有同樣的問題。地圖工作正常,並返回幾何昨天,今天它只是空白,沒有經度/緯度和相同的錯誤。

他們似乎更新的API和一些破

編輯:

對我來說這是通過採取標記可以解決的座標,而不是試圖進行地址解析它的地址,然後取地址的座標。

我有這樣的代碼(合作15年10月12日):

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

謝謝你,這對我完美的作品console.log(place.geometry.location.lat(),place.geometry.location.lng()); – Basit

1

臨時解決方法:

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

我有同樣的問題。我的Lat和Lng似乎是功能,但也是錯誤。 爲了獲得實際值,你必須實際上調用lat和long作爲函數,並將其分配給回調中的變量。

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 
    } 
    }); 
相關問題