2012-07-11 65 views
1

現在,當我點擊位置時,自動完成框工作得很好,但是當我按下時,突出顯示我想要去的位置,然後按回車鍵,它就會回到地圖的歸屬地。對此有何見解?我在initialize()中調用這個函數。我失去了我可能做錯了什麼。這只是一個谷歌API的錯誤?如果是這樣,關於如何解決它的任何見解?Google Places API自動完成功能不起作用

function setupAutoComplete() { 
    var defaultBounds = new google.maps.LatLngBounds(
     new google.maps.LatLng(-90, -180), 
     new google.maps.LatLng(90, 180)); 

    var input = document.getElementById('placeSearch'); 
    var options = { 
     bounds: defaultBounds, 
     types: ['(regions)'] 
    }; 
    autocomplete = new google.maps.places.Autocomplete(input, options); 

    google.maps.event.addListener(autocomplete, 'place_changed', function() { 
     alert('hi'); 
     removeAllOverlays(); 
     var place = autocomplete.getPlace(); 
     var mapCenter = place.geometry.location; 
     var colLat = mapCenter.lat() - (halfPoints)*latSeparation; 
     var colLng = mapCenter.lng() - (halfPoints)*lngSeparation; 
     var tempStart = new google.maps.LatLng(colLat, colLng); 
     map.setCenter(mapCenter); 
     pointArray[0][0] = tempStart; 
     reService(); 
     mapSearch(); 
     drawBounds(); 
    }); 
} 

非常感謝!

+0

我不太明白你的意思在這裏:「......但是當我按下去,突出我想要去的位置,然後按回車鍵。」 – Manatok 2012-07-11 10:51:03

回答

0

我猜input#placeSearch放在<form>裏面。

按[ENTER]鍵提交表單。

您可以通過添加刪除周圍的形式或取消提交:

onsubmit="return false" 

...到窗體元素。

+0

太謝謝你了!這解決了問題!我花了很長時間來修復它。謝謝!! – user1517446 2012-07-11 20:28:52

0

我剛剛遇到這個問題,並與以下,因爲我想在稍後階段提交表單。此代碼是從google groups

var input = document.getElementById('create-location'); 
var options = { 
    //types: ['(cities)'], 
}; 

autocomplete = new google.maps.places.Autocomplete(input, options); 
google.maps.event.addDomListener(input, 'keydown', function(e) { 
    if (e.keyCode == 13) 
    { 
     if (e.preventDefault) 
     { 
      e.preventDefault(); 
     } 
     else 
     { 
      // Since the google event handler framework does not handle early IE versions, we have to do it by our self. :-( 
      e.cancelBubble = true; 
      e.returnValue = false; 
     } 
    } 
}); 
相關問題