我如何获得点击多边形的参考? (谷歌地图api v3) [英] How do I get a reference to the polygon clicked? (google maps api v3)

查看:153
本文介绍了我如何获得点击多边形的参考? (谷歌地图api v3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了一些多边形,将它们绘制在地图上就好了。点击时我也设法触发console.log。但是,如何才能确定究竟哪个多边形实际被点击了?



正如您在示例代码中所见,我将每个对象存储在集合lots ,但是 - 点击它们只会给我点击的经纬度。我想我可能需要循环通过我的多边形,并检查点被点击是否与它们相交,从而找出它是哪个多边形...有没有更简单的解决方案?



< pre $ var lot = new google.maps.Polygon({
paths:me.area,
strokeColor:'black',
strokeOpacity:0.35,
strokeWeight:1,
fillColor:fillcol,
fillOpacity:0.35
});

lot.setMap(map);
var obj = {
'id':me.id,
'rented':me.rented,
'area':lot
};

google.maps.event.addListener(lot,'click',function(event){
console.log(event);
});

lots.push(lot);


解决方案

为什么不给每个多边形分配一些id属性当您创建它们并稍后使用 this.myID 时?真正来说,您可以将所需的所有信息都挂在该多边形对象上。

  var lot = new google.maps.Polygon({ 
路径:me.area,
strokeColor:'black',
strokeOpacity:0.35,
strokeWeight:1,
fillColor:fillcol,
fillOpacity: 0.35
});

lot.setMap(map);

var obj = {
'id':me.id,
'rented':me.rented,
'area':lot
} ;
lot.objInfo = obj;

google.maps.event.addListener(lot,'click',function(event){
console.log(this.objInfo);
});

lots.push(lot);

在循环中比路径比较更有效,或者我错过了什么? :)

I've set up some polygons, drew them on the map just fine. I also managed to fire console.log when they were clicked. However, how would I go on about figuring out which polygon was actually clicked?

As you can see in my sample code here I store each object within the collection "lots", however - clicking them only gives me the lat-long of the click. I figured I might need to loop through my polygons and check if the point was clicked is intersecting them and thus figure out which polygon it is... is there an easier solution?

var lot = new google.maps.Polygon({
    paths: me.area,
    strokeColor: 'black',
    strokeOpacity: 0.35,
    strokeWeight: 1,
    fillColor: fillcol,
    fillOpacity: 0.35
});

lot.setMap(map);
var obj = {
    'id':me.id,
    'rented':me.rented,
    'area':lot
};

google.maps.event.addListener(lot, 'click', function(event) {
    console.log(event);
});

lots.push(lot);

解决方案

Why don't assign to each polygon some id property when you create them and later just use this.myID? Truly speaking, you can hang all information you need on that polygon object.

var lot = new google.maps.Polygon({
        paths: me.area,
        strokeColor: 'black',
        strokeOpacity: 0.35,
        strokeWeight: 1,
        fillColor: fillcol,
        fillOpacity: 0.35
    });

    lot.setMap(map);

    var obj = {
        'id':me.id,
        'rented':me.rented,
        'area':lot
    };
    lot.objInfo = obj;

    google.maps.event.addListener(lot, 'click', function(event) {
        console.log(this.objInfo);
    });

    lots.push(lot);

It would be more effective than path comparison in a loop, or am i missing something? :)

这篇关于我如何获得点击多边形的参考? (谷歌地图api v3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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