我尝试创建地理编码函数(地理编码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>
抱歉,如果问题是重复的,我尝试了几乎所有的方法(也在堆栈之外),是的,什么都没有改变。
发布于 2018-06-21 20:19:14
将依赖于接口的任何代码(以google.maps.
开头)放入initMap
函数中,或者同步加载接口(从脚本URL中删除async defer
和&callback=initMap
)
发布于 2018-06-25 01:56:39
尝试使用window.google.maps.Geocoder()
而不是google.maps.Geocoder()
https://stackoverflow.com/questions/50966634
复制相似问题