Google地图:事件监听器只记住变量的最终值 [英] Google Maps: Event Listener only remembering final value of variable

查看:180
本文介绍了Google地图:事件监听器只记住变量的最终值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一份Google Map,其中包含我国各个考点的位置。它在每个县绘制一个标记,当你点击县标时,它会放大并给出该县测试中心的概况。

以下是问题所在:



当我绘制县标记并点击它们时,它总是放大到最后一个县。我用来绘制县的代码如下:

 函数plotCountyMarkers(county_count)
{
//设置一个新图标
var icon = new GIcon();
var count = 0;

//获取要显示的图标类型
var centre_type = checkCentreType();
if(centre_type =='dtc')
icon.image = dtc_icon;
else
icon.image = ctc_icon;

//其他设置,包括图标阴影
icon.shadow = icon_shadow;
icon.iconSize =新的GSize(20,29);
icon.shadowSize =新的GSize(38,29);
icon.iconAnchor =新的GPoint(10,29);
icon.infoWindowAnchor =新的GPoint(10,1);

//获取映射
的县总数var count = county_count.length;
$ b $ for(key in county_count){
//设置县的LatLong
var countyLocation =新的GLatLng(county_locations [key] [0],county_locations [key] [ 1]);
//设置标记的标题文本
var centre_text = county_count [key] == 1? '中心':'中心';
var title = county_locations [key] [2] +':'+ county_count [key] +'Test'+ centre_text;
//将一个事件监听器添加到标记
var marker = new GMarker(countyLocation,{icon:icon,title:title});

GEvent.addListener(marker,click,function(){
//放大到县
showCounty(key);
});

//将标记添加到地图
map.addOverlay(marker);


$ / code $ / pre

我基本上使用完全相同的方法来传递当你点击县级标记时,​​将HTML添加到事件监听器中,并且工作正常。出于某种原因,始终是最终县的价值。我试过将 key 作为变量传递给函数,但它只是等于当前地图的经纬度。



也许我在做一些愚蠢的事情?这不会是第一次:)任何帮助将不胜感激。

解决方案

干杯,我将事件处理程序更改为下面是它的工作原理:

  GEvent.addListener(marker,click,
(function(key){
return function(){
//缩放到县
showCounty(key);
};
})(key)
);


I'm putting together a Google Map which contains the locations of various test centres around my country. It plots a marker on each county, and when you click the county marker, it zooms in and gives an overview of the test centres in that county. I'm also using jQuery with this.

Here's the problem:

When I plot the county markers and click on them, it always zooms to the last county. The code I'm using to plot the counties is as follows:

function plotCountyMarkers(county_count)
{
    // Setup a new icon
    var icon = new GIcon();
    var count = 0;

    // Get the type of icon to display
    var centre_type = checkCentreType();
    if (centre_type == 'dtc')
        icon.image = dtc_icon;
    else
        icon.image = ctc_icon;

    // Other settings including icon shadow
    icon.shadow = icon_shadow;
    icon.iconSize = new GSize(20, 29);
    icon.shadowSize = new GSize(38, 29);
    icon.iconAnchor = new GPoint(10, 29);
    icon.infoWindowAnchor = new GPoint(10, 1);

    // Get the total number of counties to map
    var count = county_count.length;

    for (key in county_count) {
        // Set the LatLong of the county
        var countyLocation = new GLatLng(county_locations[key][0],county_locations[key][1]);
        // Set the title text of the marker
        var centre_text = county_count[key]==1 ? 'Centre' : 'Centres';
        var title = county_locations[key][2]+': '+county_count[key]+' Test '+centre_text;
        // Add an event listener to the marker
        var marker = new GMarker(countyLocation,{icon: icon, title: title});

        GEvent.addListener(marker, "click", function() {
            // Zoom to county
            showCounty(key);
        });

        // Add the marker to the map
        map.addOverlay(marker);
    }
}

I'm using basically the exact same method to pass HTML into an event listener for when you click the county level markers, and that works fine. For some reason key is always the value of the final county. I've tried passing in key as a variable to the function, but it just becomes equal to the longitude and latitude of the current map poisition.

Maybe I'm doing something idiotic? It wouldn't be the first time :) Any help would be much appreciated.

解决方案

Cheers, I changed the event handler to the following and it worked:

GEvent.addListener(marker, "click",
    (function(key) {
        return function() {
            // Zoom to county
            showCounty(key);
        };
    })(key)
);

这篇关于Google地图:事件监听器只记住变量的最终值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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