2016-12-28 105 views
0

這是我用來getPlace()它總是返回undefined對象的函數:爲什麼Google地圖getPlace()總是返回Undefined對象?

var element = document.getElementById('hdnOldAddress'); 
var AutoCompleteAddress2; 
element.value = 'Marketfair Mall 1916 Skibo Road Fayetteville NC 28314'; 

autocomplete2 = new google.maps.places.Autocomplete(element); 


autocomplete2.addListener('place_changed', function() { 
    var place2 = autocomplete2.getPlace(); 
    if (place2.address_components) { 
     AutoCompleteAddress2 = [ 
      (place2.address_components[0] && place2.address_components[0].short_name || ''), 
      (place2.address_components[1] && place2.address_components[1].short_name || ''), 
      (place2.address_components[2] && place2.address_components[2].short_name || ''), 
      (place2.address_components[3] && place2.address_components[3].short_name || ''), 
      (place2.address_components[4] && place2.address_components[4].short_name || ''), 
      (place2.address_components[5] && place2.address_components[5].short_name || ''), 
      (place2.address_components[6] && place2.address_components[6].short_name || ''), 
      (place2.address_components[7] && place2.address_components[7].short_name || ''), 
      (place2.address_components[8] && place2.address_components[8].short_name || ''), 
      (place2.address_components[9] && place2.address_components[9].short_name || ''), 
      (place2.address_components[10] && place2.address_components[10].short_name || ''), 
      (place2.address_components[11] && place2.address_components[11].short_name || ''), 
      (place2.address_components[12] && place2.address_components[12].short_name || ''), 
      (place2.address_components[13] && place2.address_components[13].short_name || ''), 
      (place2.address_components[14] && place2.address_components[14].short_name || ''), 
     ].join(' '); 
    } 
}); 
google.maps.event.trigger(autocomplete2, 'place_changed'); 

編輯

替換this.getPlace()autocomplete2.getPlace()

回答

0

編輯:沒關係,我錯了。在這段代碼中,this = autocomplete2。

問題是place_changed不會被觸發...


一個你遇到的問題是,「這」是不是你希望它是什麼。您不能在處理事件的函數中使用「this」(表示自動完成的實例)(在place_changed上)。裏面那些事件的功能這個變量是指受到影響的元素(點擊,懸停,...)

試試這個:

取代VAR place2 = this.getPlace();通過var place2 = autocomplete2.getPlace();

讓我知道,如果這個固定(否則,我們需要進一步研究)

+0

我試過了,用'var place2 = autocomplete2.getPlace();'替換,但不工作。獲取相同的錯誤。 – JMD

+0

我們正在手動設置'元素'(文本框)的值,而不是從谷歌地圖提供的自動建議下拉列表中進行選擇。這是我們沒有得到地方的原因嗎? – JMD

+0

問題是place_changed事件的觸發器。 – geocodezip

1

這是我用過的替代品以上。而不是使用Autocomplete()我已經使用AutocompleteService(),它返回predicted地方,並使用該地方的地址,我把它傳遞到geocodingmap顯示位置的預期。

var service = new google.maps.places.AutocompleteService(); 
service.getPlacePredictions({ input: 'Marketfair Mall 1916 Skibo Road Fayetteville NC 28314' }, function (predictions, status) {      
    if (status == google.maps.places.PlacesServiceStatus.OK) { 
     matchAddress = predictions[0].structured_formatting.secondary_text; 
     //Add further processing for geocoding for found address. 
    } 
}); 
+0

這並沒有完全解決我的問題,但幫助我的方式!那謝謝啦!!基本上我用google.maps.places替換了自動填充。 –

+0

...更完整的答案在這裏https://stackoverflow.com/questions/48817397/google-maps-api-autocomplete-getplace-inconsistently-returns-geometry/49018363#49018363 –

相關問題