doyz51819 2012-05-30 08:26
浏览 73
已采纳

有多个多边形并为它们提供一个不同的弹出窗口,其中包含在数据库中指定的id

I would like to give multiple polygons a different pop up window with data that I get from a database. This script already works for when there is a single polygon in the window. This is where I take the data from the database:

var length = <?php echo count($locations); ?>;
<?php
//Tel het aantal plaatsen in de array en zet deze via json in een javascript array
for ($i=0; $i < count($locations); $i++) {
    ?>
    var counter = parseInt(<?php echo $i; ?>);
    title[counter] = <?php print json_encode($locations[$i]['Location']['title']); ?>;
    location_id[counter] = <?php print json_encode($locations[$i]['Location']['id']); ?>;
    address[counter] = <?php print json_encode($locations[$i]['Location']['address']); ?>;
    city[counter] = <?php print json_encode($locations[$i]['Location']['city']); ?>;
    coordinates[counter] = <?php print json_encode($polygon[$i]); ?>;
    <?php
}
?>

These are the functions I use to make Polygons etc. sorry for the dutch comment, but I hope someone can help me out with this

onFeatureSelect: function(feature) {
    if (title != null) {
        if (html.length > 1) {
            selectedFeature = feature;
            popup = new OpenLayers.Popup.FramedCloud("Pop up",feature.geometry.getBounds().getCenterLonLat(),null,html[i],null, true, myLocations.onPopupClose);
            feature.popup = popup;
            map.addPopup(popup);   
        } else {
            selectedFeature = feature;
            popup = new OpenLayers.Popup.FramedCloud("Pop up",feature.geometry.getBounds().getCenterLonLat(),null,html,null, true, myLocations.onPopupClose);
            feature.popup = popup;
            map.addPopup(popup);   
        }
    }
}
/**
 * Bouw de HTML string op voor de pop up window
 **/
buildHTML: function(element) {
    html = '<table>' +
            '<tr>' +
                '<td>Locatie:</td>' +
                '<td>'+title+'</td>'+
                '<td rowspan="3" style="padding-left: 10px;">' +
                    '<a href="/locations/edit/'+location_id+'" title="Bewerken" ><div class="icon edit"></div></a><br/>' +
                    '<a href="/locations/view/'+location_id+'" title="Details" ><div class="icon view"></div></a>' +
                '</td>' +
            '</tr><tr>'+
                '<td>Adres:</td>'+
                '<td>'+address+'</td>' +
            '</tr><tr>'+
                '<td>Plaats:</td>'+
                '<td>'+city+'</td>'+
            '</tr>'+
        '</table>'; 
},
/**
 * Gooi de HTML strings in een array als het er meer zijn
 **/
buildMultipleHTML: function(element) {
    html = new Array();
    for(i=0;i < title.length; i++) {
        html[i] = '<table>' +
            '<tr>' +
                '<td>Locatie:</td>' +
                '<td>'+title[i]+'</td>'+
                '<td rowspan="3" style="padding-left: 10px;">' +
                    '<a href="/locations/edit/'+location_id[i]+'" title="Bewerken" ><div class="icon edit"></div></a><br/>' +
                    '<a href="/locations/view/'+location_id[i]+'" title="Details" ><div class="icon view"></div></a>' +
                '</td>' +
            '</tr><tr>'+
                '<td>Adres:</td>'+
                '<td>'+address[i]+'</td>' +
            '</tr><tr>'+
                '<td>Plaats:</td>'+
                '<td>'+city[i]+'</td>'+
            '</tr>'+
        '</table>';
    }
}
/**
 * Meerdere polygonen tekenen
 **/
drawMultiplePolygons: function(coordinates) {
    //Array site_points leegmaken
    site_points = new Array();
    for (j=0; j < coordinates.length; j++) {
        site_points[j] = new Array();
        for (i=0; i < coordinates[j][0].length; i++) {
            //Maak de punten aan met de OpenLayers, Geometry functie
            point = new OpenLayers.Geometry.Point(coordinates[j][1][i], coordinates[j][0][i]);
            //Zet de punten om in de projectie van openstreetmaps
            point.transform(
                new OpenLayers.Projection("EPSG:4326"),
                new OpenLayers.Projection("EPSG:900913")
            );
            //Zet de punten in een array
            site_points[j].push(point);
        }
        //Lineaire ring aanmaken met de OpenLayers functie
        linear_ring = new OpenLayers.Geometry.LinearRing(site_points[j]);
        //Polygoon aanmaken
        polygonFeature = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Polygon([linear_ring]), null);
        //Polygoonvector maken
        myLayers.vector.addFeatures([polygonFeature]);
    }
}
  • 写回答

1条回答 默认 最新

  • dongshi9719 2012-06-11 11:47
    关注

    This is the fix in the onFeatureSelect function:

    var selectedFeature = myLayers.vector.selectedFeatures[0].clone();
                    var identification;
                    var i;
                    var j;
                    for (i = 0; i < tbMap.map.layers.length; i++) {
                        if (tbMap.map.layers[i].CLASS_NAME == "OpenLayers.Layer.Vector") {
                            for (j = 0; j < tbMap.map.layers[i].features.length; j++) {
                                if (selectedFeature.geometry.getBounds().bottom == tbMap.map.layers[i].features[j].geometry.bounds.bottom && selectedFeature.geometry.getBounds().top == tbMap.map.layers[i].features[j].geometry.bounds.top) {
                                    identification = j;
                                } else {
                                }
                            }
                        }
                    }
                    popup = new OpenLayers.Popup.FramedCloud("Pop up",selectedFeature.geometry.getBounds().getCenterLonLat(),null,html[identification],null, true, tbLocations.onPopupClose);
                    selectedFeature.popup = popup;
                    tbMap.map.addPopup(popup);
                } else {
                    var selectedFeature = myLayers.vector.selectedFeatures[0].clone();
                    popup = new OpenLayers.Popup.FramedCloud("Pop up",selectedFeature.geometry.getBounds().getCenterLonLat(),null,html,null, true, tbLocations.onPopupClose);
                    selectedFeature.popup = popup;
                    tbMap.map.addPopup(popup);
                }   
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档