无法启动google.maps.Geocoder [英] Can't initiate the google.maps.Geocoder

查看:188
本文介绍了无法启动google.maps.Geocoder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么,但当我尝试初始化google.maps.Geocoder时出现此错误。



这是错误:

 未捕获的TypeError:undefined不是一个函数

和代码:

 函数updateMapPosition(map){
var geocoder = new google.maps.Geocoder(); //在这里崩溃!
var position = geocoder.geocode({'address':$('#id_address')。val()},
function(results,status){
if(status == google .maps.GeocoderStatus.OK){
if(status!= google.maps.GeocoderStatus.ZERO_RESULTS){
map.setCenter(results [0] .geometry.location);
var marker = new google.maps.Marker({map:map,position:results [0] .geometry.location});
}
else {
alert(hop)
}
}
}

}


解决方案

由于您似乎正在使用类似jQuery的东西,因此您可能会遇到同样的问题,因为在Google地图实际完全组装之前,jQuery认为脚本加载完成。使用jQuery的 $。getScript (等价到 $。ajax dataType:脚本在这里使用),它似乎地图API的部分还没有可用于成功 / 完成回调。



<$ p $ ($($ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ url:https://maps.googleapis.com/maps/api/js?v=3&sensor=false,
dataType:script
})。done(function(){
var geocoder = new google.maps.Geocoder(),//错误!
activityMap = new google.maps.Map($(#map_canvas)[0],{
center :new google.maps.LatLng(0,0),
zoom:0,
mapTypeId:google.maps.MapTypeId.SATELLITE
});

doSomethingMapRelatedInvolvingJQuery (地理编码器,地图);
} );
});

由于某些原因,即使jQuery说这个脚本已经通过执行回调来执行了,的谷歌地图API尚未提供(可能是第一个文件执行后谷歌的额外异步内容)。



我唯一能找到的解决方案是声明一个全局变量名称来保存Google地图完全准备就绪后要负责调用的功能。您可以通过在URL中添加 callback = {yourfunctionname} querystring参数(例如 https://maps.googleapis.com /地图/ API / JS v = 3及?回调= googleMapsCallback&安培;传感器=假)。这不是最漂亮的解决方案,但它的工作原理。

  var googleMapsCallback; 
$(function(){
var doSomethingMapRelatedInvolvingJQuery = function(geocoder,map){...};

googleMapsCallback = function(){
var geocoder = new google.maps.Geocoder(),//当谷歌进行调用时工作
activityMap = new google.maps.Map($(#map_canvas)[0],{
center:new google.maps.LatLng(0,0),
zoom:0,
mapTypeId:google.maps.MapTypeId.SATELLITE
});

doSomethingMapRelatedInvolvingJQuery(geocoder ,地图);
};
$ .ajax({
url:https://maps.googleapis.com/maps/api/js?v=3&callback=googleMapsCallback& sensor = false,
dataType:script
});
});



编辑:



概念证明jsFiddle ,将所有丑陋的东西变成一个jQuery扩展的方法,允许Deferred-链接。下面是一个如何使用它的简单例子:

  $。loadGoogleMaps()。done(function(){
var geocoder = new google.maps.Geocoder();
// ...在这里做你的geocoder的东西...
});


I don't understand why but i'm getting this error when I try to initialize the google.maps.Geocoder.

Here is the error:

Uncaught TypeError: undefined is not a function

And the code :

function updateMapPosition(map){
    var geocoder = new google.maps.Geocoder(); //crashes here !!
    var position = geocoder.geocode( {'address':$('#id_address').val()},
        function(results,status){
            if(status == google.maps.GeocoderStatus.OK){
                if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
                    map.setCenter(results[0].geometry.location);
                    var marker = new google.maps.Marker({map:map, position:results[0].geometry.location});
                }
                else{
                    alert("hop")
                }
            }
        }
    )
}

解决方案

Since you appear to be using something like jQuery, you may have the same issue I had where jQuery considers the script loading complete before Google Maps is actually completely "assembled". Using jQuery's $.getScript (equivalent to $.ajax with dataType: "script" used here), it appears parts of the maps API are not yet available in the success/done callbacks.

$(function() {
    var doSomethingMapRelatedInvolvingJQuery = function(geocoder, map) { ... };

    $.ajax({
        url: "https://maps.googleapis.com/maps/api/js?v=3&sensor=false",
        dataType: "script"
    }).done(function() {
        var geocoder = new google.maps.Geocoder(), // ERROR!
            activityMap = new google.maps.Map($("#map_canvas")[0], {
                center: new google.maps.LatLng(0, 0),
                zoom: 0,
                mapTypeId: google.maps.MapTypeId.SATELLITE
            });

        doSomethingMapRelatedInvolvingJQuery(geocoder, map);
    });
});

For some reason, even though jQuery is saying the script has been executed already by executing the callbacks, parts of the Google Maps API are not yet available (possibly additional asynchronous stuff by Google after the first file executes).

The only solution I could find was to declare a global variable name to hold a function that Google Maps would be responsible for calling when it was completely ready. You can give it that control by adding the callback={yourfunctionname} querystring parameter to the URL (e.g., https://maps.googleapis.com/maps/api/js?v=3&callback=googleMapsCallback&sensor=false). It's not the prettiest solution, but it works.

var googleMapsCallback;
$(function() {
    var doSomethingMapRelatedInvolvingJQuery= function(geocoder, map) { ... };

    googleMapsCallback = function() {
        var geocoder = new google.maps.Geocoder(), // Works when google makes the call.
            activityMap = new google.maps.Map($("#map_canvas")[0], {
                center: new google.maps.LatLng(0, 0),
                zoom: 0,
                mapTypeId: google.maps.MapTypeId.SATELLITE
            });

        doSomethingMapRelatedInvolvingJQuery(geocoder, map);
    };
    $.ajax({
        url: "https://maps.googleapis.com/maps/api/js?v=3&callback=googleMapsCallback&sensor=false",
        dataType: "script"
    });
});

EDIT:

I put together a proof-of-concept jsFiddle that pulls all that ugliness into a jQuery-extended method that allows for Deferred-chaining. Here's a quick example of how it can be used:

$.loadGoogleMaps().done(function () {
    var geocoder = new google.maps.Geocoder();
    // ... do stuff with your geocoder here ...
});

这篇关于无法启动google.maps.Geocoder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆