简体   繁体   English

Google Maps InfoWindow显示在错误的Map上

[英]Google Maps InfoWindow shows up on wrong Map

My Problem is in the WP-Plugin I write that when there is more than one map open on the same time, 我的问题是在WP-Plugin中,我写道,当同时打开多个地图时,
eg on the mainpage where the last 3 posts are shown each with an own minimap showing their position, that when I click on one of the markers, doesn't matter from which map, the infoWindows will show up all in the last map... 例如,在首页上显示最后3个帖子,每个帖子都带有一个自己的微型地图,该微型地图显示了它们的位置,当我单击其中一个标记时,无论从哪个地图开始,infoWindows都将显示在最后一个地图中。 。

I initialize the maps with the following code: 我使用以下代码初始化地图:

function Initialize(lat, lng, zm, pid) {    
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(lat,lng);
    var mapOptions = {
        zoom: zm,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }

    var name = "map_canvas" + pid;  

    map = new google.maps.Map(document.getElementById(name), mapOptions);
}

and set the markers as followed: 并按照以下步骤设置标记:

function AddMarker( lat, lng, address, title, link, image, target ) {

    var marker = new google.maps.Marker({
        map: map,
        position: new google.maps.LatLng(lat, lng),
        icon: new google.maps.MarkerImage( image ),
        title: title
    });

    var infowindow = new google.maps.InfoWindow({ 
        content: '<a href="' + link + '" target="_' + target + '">' + title + '</a><br /><small><font color="#000000">' + address + '</font></small>', 
    });
    google.maps.event.addListener(marker, 'click', function() {
        infowindow.open(map,marker);
    });
}

the var map stands on top of the functionfile, kinda like a Global, so that the markers are all on the right map, but sadly the infowindows won't follow them... var map位于函数文件的顶部,有点像Global,因此标记都在正确的映射上,但是可惜的是,信息窗口不会跟随它们...

does anyone else experienced such stuff, or has an idea how I can solve it? 有没有其他人经历过这样的事情,或者有一个想法我该如何解决?

Your problem is that this: 您的问题是这样的:

google.maps.event.addListener(marker, 'click', function() {
    infowindow.open(map,marker);
});

Accesses the map variable when the map is clicked not the value of the variable when you assign the handler. 单击map时访问map变量,而不是分配处理程序时访问变量的值。 Pass a copy and use that instead: 传递副本并使用该副本:

google.maps.event.addListener(marker, 'click', (function (m) {
    return function() {
        infowindow.open(m,marker);
    };
}(map)));

Edit: 编辑:

The above uses an immediately invoked function expression to pass in a copy of the map variable. 上面使用了立即调用的函数表达式来传递map变量的副本。 All this does is declare a function and then immediately call it, passing in map . 所有这些操作就是声明一个函数,然后立即调用它,并传入map

function (m) {
    return function() {
        infowindow.open(m,marker);
    };
}(map)

However, the whole thing needs to be wrapped in () so that the JS parser reads it as a function expression and not a function declaration. 但是,整个过程都需要包装在()以便JS解析器将其作为函数表达式而不是函数声明读取。

Another way of solving it would be: 解决该问题的另一种方法是:

function AddMarker( lat, lng, address, title, link, image, target ) {

    var marker = new google.maps.Marker({
        map: map,
        position: new google.maps.LatLng(lat, lng),
        icon: new google.maps.MarkerImage( image ),
        title: title
    });

    var infowindow = new google.maps.InfoWindow({ 
        content: '<a href="' + link + '" target="_' + target + '">' + title + '</a><br /><small><font color="#000000">' + address + '</font></small>', 
    });

    // Add a function that returns a new function that uses a local
    // copy of the passed in map.
    var getMapClickListener = function (m) {
        return function () {
            infowindow.open(m, marker);
        };
    };

   // Then use the new function when adding the handler.
    google.maps.event.addListener(marker, 'click', getMapClickListener(map));
}

I think the problem is that while you're assigning the map name to name correctly using the pid , each map is then being assigned to the map variable, each one overriding the other. 我认为问题在于,当您使用pid将地图名称正确分配给name ,然后pid每个地图分配给map变量,每个map变量都覆盖另一个。

What you might find useful is using an object to hold your maps using the pids as keys: 您可能会发现有用的是使用对象以pid作为键来保存地图:

var mapHolder = {};
var name = "map_canvas" + pid;
var mapHolder[pid] = new google.maps.Map(document.getElementById(name), mapOptions);

That way you can access each map without hassle just by passing in the pid to the addMarker function: 这样,您只需将pid传递给addMarker函数即可轻松访问每个地图:

function AddMarker( lat, lng, address, title, link, image, target, pid) {

  var marker = new google.maps.Marker({
    map: mapHolder[pid],
    position: new google.maps.LatLng(lat, lng),
    icon: new google.maps.MarkerImage( image ),
    title: title
  });

  var infowindow = new google.maps.InfoWindow({
    content: // content
  });

  google.maps.event.addListener(marker, 'click', function() {
    infowindow.open(mapHolder[pid], marker);
  });

}

That way all your markers and infowindows should open on the correct map. 这样,所有标记和信息窗口都应在正确的地图上打开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM