首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >ReferenceError:谷歌未定义/地理编码API

ReferenceError:谷歌未定义/地理编码API
EN

Stack Overflow用户
提问于 2018-06-21 18:47:45
回答 2查看 1.2K关注 0票数 -1

我尝试创建地理编码函数(地理编码API),它一直返回google is not defined,我已经根据this answer更改了链接,但它不起作用:(它在geocoder = new google.maps.Geocoder();出错

JS:

代码语言:javascript
复制
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);
            }
        });
    }
}

链接:

代码语言:javascript
复制
<script src="https://maps.google.com/maps/api/js?key=AIzaSyCNpeRgwCQoHIlLn-X8TIB9SnO8iLPt808&callback=initMap"
async defer></script>

抱歉,如果问题是重复的,我尝试了几乎所有的方法(也在堆栈之外),是的,什么都没有改变。

EN

回答 2

Stack Overflow用户

发布于 2018-06-21 20:19:14

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

票数 0
EN

Stack Overflow用户

发布于 2018-06-25 01:56:39

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

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50966634

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档