Google地图 - 无法读取未定义的属性'__e3_' [英] Google Maps - cannot read property '__e3_' of undefined

查看:112
本文介绍了Google地图 - 无法读取未定义的属性'__e3_'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人可以用我的Google Maps API代码来帮助我。

我已经设置了多个可成功运行的标记的地图,但是我想添加点击事件到标记以便出现一个信息窗口,这就是我遇到的问题。

  $(document)。 ready(function(){
var script = document.createElement(script);
script.type =text / javascript;
script.src =http:// maps .googleapis.com / maps / api / js?libraries = geometry& sensor = true& callback = mapInitialize;
document.body.appendChild(script);
});
函数mapInitialize(){
var options = {
zoom:5,
mapTypeId:google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map_div'),options);
geocoder = new google.maps.Geocoder();
函数codeAddress(markerValue,addressValue){
geocoder.geocode({'address':addressValue},function(results,status){
if(status == google.maps.GeocoderStatus。 OK){
map.setCenter(results [0] .geometry.location);
markerValue = new google.maps.Marker({
map:map,
position:results [0] .geometry.location
});
}
});
}
var marker1;
codeAddress(marker1,'1 My Road,Somewhere,A1 1AA');
var infowindow1 = new google.maps.InfoWindow({
content:'< div class =infowindow>< strong>< a href =http://mylink.com >标题< / a>< / strong>< br />地址详情< / div>'
});
google.maps.event.addListener(marker1,'click',function(){
infowindow1.open(map,marker1);
});





$ b

所以我得到错误'Can not read property'_ e3 '的未定义',当我包含addListener代码。



我已阅读过类似问题的其他帖子,但我已阅读所有回复并即使他们有同样的问题,但是没有任何解决方案对他们有效,或者与我写的无关。



感谢提前。 / b>

解决方案

这里有很多问题:


  1. 你试图在函数内修改传入你的 codeAddress 函数的 marker1 变量分配给参数名称 markerValue 。 JavaScript没有传递引用参数,所以 marker1 不会受到为 markerValue 。因此,稍后当您尝试使用 marker1 执行某些操作时,它仍然具有其默认值( undefined )。


  2. Google地图的API是异步。您对 geocoder.geocode 的调用会启动过程,但会在 codeAddress 已返回。




  3. 您需要将后续代码移动到 codeAddress ,特别是从 geocode 的完成回调中。



    这样的东西(完全未经测试,这个想法是为您指出正确的方向,而不是为您完美的解决方案):

      function mapInitialize(){
    var options = {
    zoom:5,
    mapTypeId:google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById('map_div'),options);
    geocoder = new google.maps.Geocoder();
    函数codeAddress(addressValue){
    geocoder.geocode({'address':addressValue},function(results,status){
    if(status == google.maps.GeocoderStatus.OK) {
    var marker,infowindow1;

    map.setCenter(results [0] .geometry.location);
    marker = new google.maps.Marker({
    map:map,
    position:results [0] .geometry.location
    });

    infowindow = new google.maps.InfoWindow({
    content:' < div class =infowindow>< strong>< a href =http://mylink.com>标题< / a>< / strong>< div>'
    });
    google.maps.event.addListener(marker,'click',function(){
    infowindow.open(map,marker);
    } );
    }
    });
    }
    codeAddress('1 My Road,Somewhere,A1 1AA');
    }


    I hope somebody can help me with my Google Maps API code.

    I have set up a map with multiple markers which works successfully, however I want to add on-click events to the markers so that an info window appears, this is where I hit the problem.

    $(document).ready(function() {
        var script = document.createElement("script");
        script.type = "text/javascript";
        script.src = "http://maps.googleapis.com/maps/api/js?libraries=geometry&sensor=true&callback=mapInitialize";
        document.body.appendChild(script);
    });
    function mapInitialize() {
        var options = {  
            zoom: 5,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById('map_div'), options);
        geocoder = new google.maps.Geocoder();
        function codeAddress(markerValue, addressValue) {
            geocoder.geocode({ 'address': addressValue}, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    map.setCenter(results[0].geometry.location);
                    markerValue = new google.maps.Marker({
                        map: map,
                        position: results[0].geometry.location
                    });
                }
            });
        }
        var marker1;
        codeAddress(marker1, '1 My Road, Somewhere, A1 1AA');
        var infowindow1 = new google.maps.InfoWindow({
            content: '<div class="infowindow"><strong><a href="http://mylink.com">Title</a></strong><br />Address details</div>'
        }); 
        google.maps.event.addListener(marker1, 'click', function() {
            infowindow1.open(map, marker1);  
        });
    }
    

    so I get the error 'Cannot read property '_e3' of undefined' when I include the addListener code.

    I've read other posts on here of similar issues, but I have read all of the responses and even though they have the same problem none of the resolutions that worked for them have worked for me, or they are irrelevant to waht I have written.

    Thanks in Advance.

    解决方案

    There are multiple problems here:

    1. You're trying to modify the marker1 variable passed into your codeAddress function from within the function by assigning to the argument name markerValue. JavaScript doesn't have pass-by-reference arguments, and so marker1 is not in any way affected by your assigning a value to markerValue. So later when you try to do something with marker1, it still has its default value (undefined).

    2. Google Maps' API is asynchronous. Your call to geocoder.geocode starts the process, but it completes later, long after codeAddress has returned.

    You need to move your subsequent code into codeAddress, specifically into the completion callback from geocode.

    Something like this (completely untested, the idea is to point you the right direction, not code a perfect solution for you):

    function mapInitialize() {
        var options = {  
            zoom: 5,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById('map_div'), options);
        geocoder = new google.maps.Geocoder();
        function codeAddress(addressValue) {
            geocoder.geocode({ 'address': addressValue}, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    var marker, infowindow1;
    
                    map.setCenter(results[0].geometry.location);
                    marker = new google.maps.Marker({
                        map: map,
                        position: results[0].geometry.location
                    });
    
                    infowindow = new google.maps.InfoWindow({
                        content: '<div class="infowindow"><strong><a href="http://mylink.com">Title</a></strong><br />Address details</div>'
                    }); 
                    google.maps.event.addListener(marker, 'click', function() {
                        infowindow.open(map, marker);  
                    });
                }
            });
        }
        codeAddress('1 My Road, Somewhere, A1 1AA');
    }
    

    这篇关于Google地图 - 无法读取未定义的属性'__e3_'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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