【发布时间】:2019-03-22 02:32:42
【问题描述】:
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
..........
var markers = locations.map(function(location, i) {
return new google.maps.Marker({
position: location,
label: labels[i % labels.length]
});
});
// Add a marker clusterer to manage the markers.
var markerCluster = new MarkerClusterer(map, markers,
{imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
}
//Store LatLng of PostalCode
var locations = [];
function getLatLng(zipcode, callback)
{
var geocoder = new google.maps.Geocoder();
var address = zipcode;
geocoder.geocode({ 'address': address }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
callback({lat: latitude, lng: longitude });
} else {
alert("Request failed.")
}
});
}
function getpc(postalcode) {
getLatLng(postalcode, function (data) {
locations.push({
lat : data.lat,
lng : data.lng
});
console.log("lat = " + data.lat + " and lng = " + data.lng);
});
}
getpc("640632");
当我尝试调用函数 getpc() 时,它返回 google 未定义。但是,如果我尝试删除它,则不会发生错误。我搜索了很多方法来解决这个问题,但没有一个奏效。感谢任何帮助。
这是 console.log 上显示的错误:
Uncaught ReferenceError: google is not defined
at getLatLng (GoogleMapTest.aspx:73)
at getpc (GoogleMapTest.aspx:88)
at GoogleMapTest.aspx:98
【问题讨论】:
-
@brian-lives-outdoors 是的,我在
标签: javascript geocoding google-geocoder