我如何为每个创建的多边形打开不同的信息?谷歌地图api v3 [英] How do i open different information for each polygon i've created? Google maps api v3

查看:77
本文介绍了我如何为每个创建的多边形打开不同的信息?谷歌地图api v3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我当前正在运行的代码:
在地图上显示多边形后,用户可以创建自己的多边形,我想要显示有关每个多边形的一些信息。我现在打开一个infowindow,但是我不能得到不同多边形的信息

Here is the code i'm currently running: The user is able to create their own polygon after their polygons are shown on the map i want to display some information about every polygon. I'm now opening an infowindow but i can't get information for different polygons

有什么想法吗?

Any thoughts?

<?php foreach ($area->result() as $f):?>

// Create an array with the coordanates of each area

var area<?=$f->id?>Coords = [
    <?php $latlng=$this->resources_data->field_latlng($f->id);?>
    <?php foreach ($latlng->result() as $point):?>
    new google.maps.LatLng(<?=$point->lat?>, <?=$point->lng?>),
    <?php endforeach;?>
];

// Create a polygon with the points of the area

var area<?=$f->id?>=new google.maps.Polygon({
    paths: area<?=$f->id?>Coords,
    strokeColor: '#FF0000',
    strokeOpacity: 0.8,
    strokeWeight: 2,
    fillColor: '#FF0000',
    fillOpacity: 0.35
});
// Add the Field to the map.
area<?=$f->id?>.setMap(map);

google.maps.event.addListener(field<?=$f->id?>,'click',function(event){
      infowindow.setContent(contentString);
      if (event) {
         point = event.latLng;
      }
      infowindow.setPosition(point);
      infowindow.open(map);
});

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


<?php endforeach;?>

var contentString = '<div id="content" class="h300 rad3">'+
    '<div>'+
    '</div>'+
    '<h2 ><?=$f->name?> </h2>'+
    '<div id="bodyContent">'+
    '<div class="bgC m20l glow3cb rad3 h100 w350 inlineB vtop m15b m15r">' +
    ' '+
    ''+
    '<div class="inlineT vtop w180 m5l"> '+
    '<div class="inlineT vtop m5l"> '+
    '</div>';

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


推荐答案

将infoWindow内容与多边形相关联的一种方法是使用函数关闭,如本示例中所述(请参阅createClickablePoly函数)。如果你不需要一个可点击的侧边栏,你可以简化该功能:

One way to associate the infoWindow contents with the polygon is to use function closure, as done in this example (see the createClickablePoly function). If you don't need a clickable sidebar, you can simplify that function to:

function createClickablePoly(poly, html) {
    var contentString = html;
    google.maps.event.addListener(poly,'click', function(event) {
      infowindow.setContent(contentString);
      infowindow.setPosition(event.latLng);
      infowindow.open(map);
    }); 
}

这篇关于我如何为每个创建的多边形打开不同的信息?谷歌地图api v3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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