【发布时间】:2018-11-30 16:10:41
【问题描述】:
我尝试创建地理编码功能(地理编码 API),它一直返回google is not defined,我已经根据this answer 更改了链接,但它不起作用:( 它在geocoder = new google.maps.Geocoder(); 上窃听
JS:
geocoder = new google.maps.Geocoder();
function address() {
//In this case it gets the address from an element on the page, but obviously you could just pass it to the method instead
for (i = 0; i < index.length; i++) {
var address = index[i].Origin;
console.log(address)
geocoder.geocode({
'address': address
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
//In this case it creates a marker, but you can get the lat and lng from the location.LatLng
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);
}
});
}
}
链接:
<script src="https://maps.google.com/maps/api/js?key=AIzaSyCNpeRgwCQoHIlLn-X8TIB9SnO8iLPt808&callback=initMap"
async defer></script>
抱歉,如果问题重复,我尝试了几乎所有内容(也在堆栈之外),是的,没有任何变化。
【问题讨论】:
-
将您的代码放入 documentready 函数中。或者从 Google Maps 脚本标签中删除属性 async 和 defer
标签: javascript json google-maps-api-3 geocoding google-geocoding-api