Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
532 views
in Technique[技术] by (71.8m points)

php - How do i open different information for each polygon i've created? Google maps api v3

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
});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
    }); 
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...