【发布时间】:2016-05-22 23:57:22
【问题描述】:
当用户点击提交时,我正在尝试使用 mapSearch 框中的用户输入来更新我的地图,但我遗漏了未将提交 onclick 函数与 latlng 变量连接起来的内容。
var geocoder;
var map;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng =new google.maps.LatLng(-34.397,150.644);
var mapProp = {
center: latlng,
}
var map=new google.maps.Map(document.getElementById("googleMap"), mapProp);
};
var searchBox = new google.maps.places.SearchBox(document.getElementById('mapsearch'));
google.maps.event.addDomListener(searchBox, 'places_changed', function(){
var places = searchBox.getPlaces();
var bounds = new google.maps.LatLngBounds();
var i, place;
for (i=0; place=places[i]; i++){
bounds.extend(place.geometry.location);
}
});
$( "#Submit" ).click(function codeAddress(){
var address = document.getElementById("mapsearch").value;
geocoder.geocode( {'mapsearch': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK){
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results [0].geometry.location
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
});
【问题讨论】:
-
我在发布的代码中收到一个 javascript 错误:` InvalidValueError: unknown property mapsearch` 在这一行:` geocoder.geocode( {'mapsearch': address},` (应该是 'address' )
-
我试图将它缩短到我认为是问题的部分,但看起来这是一个新手错误:这是完整的代码
标签: google-maps-api-3 google-geocoding-api