Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ReferenceError:google未定义/地理编码API_Javascript_Json_Google Maps Api 3_Geocoding_Google Geocoding Api - Fatal编程技术网

Javascript ReferenceError:google未定义/地理编码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

我试图创建地理编码函数(geocoding API),它不断返回
谷歌未定义
,我已经根据更改了链接,但没有起作用:(它在
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>