【问题标题】:ReferenceError: google is not defined / Geocoding APIReferenceError:谷歌未定义/地理编码API
【发布时间】: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


【解决方案1】:

将任何依赖于 API 的代码(以 google.maps. 开头)放入 initMap 函数或同步加载 API(从脚本 URL 中删除 async defer&amp;callback=initMap

【讨论】:

    【解决方案2】:

    尝试使用window.google.maps.Geocoder() 而不是google.maps.Geocoder()

    【讨论】:

      猜你喜欢
      • 2020-09-20
      • 2016-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-15
      • 2017-06-22
      • 1970-01-01
      • 2019-06-13
      相关资源
      最近更新 更多