0

我正在使用 OpenLayers 并加载几个运行良好的 GEOJson 文件。现在我希望能够选择 GEOJson 文件的各个组件。例如,当我激活悬停选择时,如果我有多个特征(几个多边形),则不会发生任何事情。

我将悬停选择器添加到此示例http://openlayers.org/dev/examples/geojson.html

我实际上可以独立拖动元素,但我无法选择它们。我认为这是因为我使用的投影。任何想法如何解决它?

干杯!

function init(){

var options = {
    projection: new OpenLayers.Projection("EPSG:900913"),
    displayProjection: new OpenLayers.Projection("EPSG:4326"),
    units: "m",
    numZoomLevels: 18,
};

map = new OpenLayers.Map('map', options );
var mapnik = new OpenLayers.Layer.OSM({layers: 'basic'});           
map.addLayer(mapnik);

map.addControl(new OpenLayers.Control.MousePosition());

var lon = 12.18;
var lat = 53.81;
var lonLat = new OpenLayers.LonLat(lon,lat).transform(map.displayProjection,  map.projection);

var mapcent = new OpenLayers.LonLat(lon, lat).transform(map.displayProjection, map.projection);
zoom = 5.5;
map.setCenter(mapcent, zoom);
document.getElementById('noneToggle').checked = true;

var proj = new OpenLayers.Projection("EPSG:4326");

var file = [];
file[0] = "geojson/nuts3.geojson";

var style = [];

i=0;
while(i<file.length){
    style[i] = new OpenLayers.StyleMap({
        pointRadius: 2,
        strokeColor: 'red',
        strokeWidth: 1,
        strokeOpacity: 1,
        fillColor: 'red',
        fillOpacity: 0.2
    });

    var layer_name = "layer_" + i;
    vectorLayer[i] = new OpenLayers.Layer.Vector(layer_name,  {
        styleMap: style[i],
        projection: map.displayProjection,
        preFeatureInsert: function(feature){
        feature.geometry.transform(proj, map.getProjectionObject());
        },
    });
    map.addLayer(vectorLayer[i]);   

    var gjson = new OpenLayers.Format.GeoJSON();
    var featurecollection = ReadFile(file[i]);
    //Readfile is just a normal Request.POST function   
    vectorLayer[i].addFeatures(gjson.read(featurecollection));  
    i=i+1;
}

controls = {
    selecthover: new OpenLayers.Control.SelectFeature(vectorLayer[0],{
        hover: true,
        highlightOnly: true
    })
};

for(var key in controls) {
    map.addControl(controls[key]);
    controls[key].activate();

}

            var report = function(e) {
            OpenLayers.Console.log(e.type, e.feature.id);
        };

        var highlightCtrl = new OpenLayers.Control.SelectFeature(vectors, {
            hover: true,
            highlightOnly: true,
            renderIntent: "temporary",
            eventListeners: {
                beforefeaturehighlighted: report,
                featurehighlighted: report,
                featureunhighlighted: report
            }
        });


        map.addControl(highlightCtrl);
        map.addControl(selectCtrl);            
}   

geojson 文件看起来像

{“类型”:“FeatureCollection”,“特征”:[{“类型”:“特征”,“id”:0,“属性”:{},“几何”:{“类型”:“多边形”,“ coordinates": [ [ [ 9.244170, 48.847009 ], [ 9.246171, 48.822260 ], [ 9.248220, 48.796910 ], [ 9.248433, 48.794277 ], [ 9.290007, 48.791633 ], [ 9.298732, 48.785304 ], [ 9.145165, 48.859655 ], [ 9.153553, 48.861241 ], [ 9.207493, 48.852769 ], [ 9.235667, 48.848344 ], [ 9.244170, 48.847009 ] ] } } , { "type": "Feature", "id": 1, "properties": {}, "geometry" : { "类型": "多边形", "坐标": [ [ [ 9.064848, 48.755577 ], [ 9.038994, 48.741317 ], [ 9.104720, 48.709080 ], [ 9.104721, 48.709079 ], [ 9.13574.66], [ 9.13565.6]156822, 48.652414 ], [ 9.162166, 48.646602 ], [ 9.174173, 48.633544 ], [ 9.064848, 48.755577 ] ] } } 等

4

1 回答 1

0

好的,我解决了。

https://gis.stackexchange.com/questions/25314/change-vector-projection-in-openlayers

按照该问题的正确答案中提出的投影并为geojson文件的每个部分使用独立的ID,可以在悬停时进行选择。

于 2012-06-18T09:56:30.047 回答