Javascript ReferenceError:google未定义/地理编码API
我试图创建地理编码函数(geocoding API),它不断返回Javascript ReferenceError:google未定义/地理编码API,javascript,json,google-maps-api-3,geocoding,google-geocoding-api,Javascript,Json,Google Maps Api 3,Geocoding,Google Geocoding Api,我试图创建地理编码函数(geocoding API),它不断返回谷歌未定义,我已经根据更改了链接,但没有起作用:(它在geocoder=new google.maps.geocoder(); JS: geocoder=new google.maps.geocoder(); 函数地址(){ //在本例中,它从页面上的元素获取地址,但显然您可以将其传递给该方法 对于(i=0;i
谷歌未定义,我已经根据更改了链接,但没有起作用:(它在geocoder=new google.maps.geocoder();
JS:
geocoder=new google.maps.geocoder();
函数地址(){
//在本例中,它从页面上的元素获取地址,但显然您可以将其传递给该方法
对于(i=0;i
链接:
抱歉,如果问题重复,我几乎尝试了所有方法(也在堆栈外),但没有任何更改。将依赖于API的任何代码(以google.maps.
开头)放入initMap
函数中,或者同步加载API(从脚本URL中删除async defer
和&callback=initMap
)尝试使用window.google.maps.Geocoder()
而不是google.maps.Geocoder()
将代码放入documentready函数中。或者从google maps脚本标记中删除属性async和defer()
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>