我是新手,但我有一个从用户的 W3C 浏览器地理编码信息加载的 Google V3 地图,我从地理编码对象中提取国家、州和城市并更新输入框。如果用户输入街道地址,我想通过 Google 对其进行地理编码并更改 map.setCenter 和 setZoom,并显示更新后的地图。当它起作用时,我想添加一个标记和信息窗口。尽管进行了数小时的研究和试验,我还是无法让地理编码和更新工作。Chrome 中的开发者工具似乎表明执行在 geocoder.geocode 行停止/失败,下面以粗体表示。这是相关代码。
var map, geocoder, marker, infowindow; //global variables
function initializeMap() {
// Try W3C Geolocation to geolocate desktop user
//initialize Google map, geolocate desktop user, and display after page loads
//find country, state, and city for the user's location - this all works
}
window.onload = initializeMap;
//change the Google Map after a user enters a street address
$("#streetAddress").blur(function() {
//if state, city and street address locations are present
if ( $(this).val().length > 0 && $("#state").val().length > 0 && $("#city3").val().length > 0 ) {
var gAddress = [$(this).val() + ", " + $("#city3").val() + ", " + $("#state").val()] ;
//get coordinates of this address
//if no geocode object exists, create one
if (!geocoder) {
geocoder = new google.maps.Geocoder(); //create Geocoder object else use existing one from initializeMap() ?
}
//create a GeocoderRequest object with user's street address
var geoCoderRequest = {address: gAddress}
//make a Geocoder request
geocoder.geocode( geoCoderRequest, function(results, status) { **//this line fails**
//check if status is OK
if ( status === google.maps.GeocoderStatus.OK) {
//update center of existing map on location returned for this address
map.setCenter(results[0].geometry.location);
map.setZoom(14);
}
});
} else {
return; //no action if gAddress is incomplete
}
});