2016-07-23 129 views
3

我在我的ASP.NET MVC項目上使用Google Maps API v3。
我想給用戶顯示一張地圖,可以搜索某個地方,然後選擇該地點,然後提交(非常簡單)。
在這種情況下,除了在用戶在地圖搜索框中輸入時獲取建議的地點外,一切都會好起來的。
這裏是我包含在谷歌地圖APIGoogle maps api place_changed event not Firing

<script src="https://maps.googleapis.com/maps/api/js?key=Correct_Key_Dot_Worry&libraries=places&callback=initMap" async defer></script> 

HTML

<body> 
    <input id="pac-input" class="controls" type="text" placeholder="Search Box"> 
    <input type="button" value="hakam" id="btntest" /> 
    <div id="map" style="width:500px; height:500px; margin-top:400px"></div> 
</body> 

的JavaScript

window.initMap = function(){ 
    var map = new google.maps.Map(document.getElementById('map'), { 
     center: { lat: -33.8688, lng: 151.2195 }, 
     zoom: 13 
    }); 
    var input = (document.getElementById('pac-input')); 

    map.controls[google.maps.ControlPosition.TOP_LEFT].push(input); 

    var autocomplete = new google.maps.places.Autocomplete(input); 
    autocomplete.bindTo('bounds', map); 

    var infowindow = new google.maps.InfoWindow(); 
    var marker = new google.maps.Marker({ 
     map: map, 
     anchorPoint: new google.maps.Point(0, -29) 
    }); 

    google.maps.event.addListener(autocomplete, 'place_changed', function() { 
     infowindow.close(); 
     marker.setVisible(false); 
     var place = autocomplete.getPlace(); 
     if (!place.geometry) { 
      window.alert("Autocomplete's returned place contains no geometry"); 
      return; 
     } 

     // If the place has a geometry, then present it on a map. 
     if (place.geometry.viewport) { 
      map.fitBounds(place.geometry.viewport); 
     } else { 
      map.setCenter(place.geometry.location); 
      map.setZoom(17); // Why 17? Because it looks good. 
     } 
     marker.setIcon(({ 
      url: place.icon, 
      size: new google.maps.Size(71, 71), 
      origin: new google.maps.Point(0, 0), 
      anchor: new google.maps.Point(17, 34), 
      scaledSize: new google.maps.Size(35, 35) 
     })); 
     marker.setPosition(place.geometry.location); 
     marker.setVisible(true); 


     var address = ''; 
     if (place.address_components) { 
      address = [ 
       (place.address_components[0] && place.address_components[0].short_name || ''), 
       (place.address_components[1] && place.address_components[1].short_name || ''), 
       (place.address_components[2] && place.address_components[2].short_name || '') 
      ].join(' '); 
     } 

     //infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address); 
     infowindow.setContent('<div><strong>' + "Location(" + place.geometry.location.lat() + "," + place.geometry.location.lng() + ")" + '</strong><br>'); 
     infowindow.open(map, marker); 
    }); 

} 

問題

place_changed的處理程序未觸發,並且當我開始在Google地圖搜索的文本框內寫入時,處理程序的代碼未被調用。

約6小時的搜索和心靈紡後,我讀了關於此事件的谷歌地圖API,這是因爲以下

這個事件的文件時,提供了一組PlaceResult用戶已選擇的地方。 如果用戶輸入控件未建議的地方的名稱並按Enter鍵,或者如果地方詳細信息請求失敗,將觸發一個place_changed事件,該事件包含名稱屬性中的用戶輸入,而沒有其他人屬性定義。

的文件說,正是當我點擊「確定」鍵我真的讓這個事件place_changed射擊,並再次,完全按照文件說,我得到了它包含了用戶輸入的響應。

問題 爲什麼在地獄裏a PlaceResult is NOT made available for a Place the user has selected。 (根據谷歌文檔) 我做錯了什麼使谷歌阻止我從這個事件?

注1:
請不要給我建議這個話題在計算器上的任何問題,因爲我一直在閱讀所有的人,一個接一個,從早上(6小時以上),並沒有什麼幫助了我。

注2
JavaScript的示例已從谷歌地圖API文檔中提取。

注3:
如在下面的意見建議,設置語言和區域,同時加載谷歌地圖圖書館爲以下幾點:

<script src="https://maps.googleapis.com/maps/api/js?key=mykey&&language=ar&region=EG&libraries=places&callback=initMap" async defer></script> 

並沒有解決我的問題。

+0

我剛剛嘗試過這一點,它似乎按預期工作,當您在「輸入」中添加文本時,不會觸發'place_changed'事件,當您選擇建議或按Enter鍵時會觸發此事件。 – Titus

+0

@Titus第一次你在你的機器上試過這個,它工作嗎?請告訴我這是非常重要的一點 –

+0

是的,這個建議框會顯示出來,當我選擇一個建議時,'place_changed'被觸發,當我按下回車鍵時它也會被觸發。 – Titus

回答

1

此問題已在評論中得到解答,我將對此進行總結。
一切之上(在這個問題)是正確的,在所有 沒有錯誤我只是不得不啓用Google Places API從谷歌開發者控制檯

這裏是SO一個類似的問題: Google Map Autocomplete API not adding address options

順便說一句:在place_changed正如我在想的那樣,您在搜索框中鍵入內容時不需要觸發事件。