Open Layers点事件

3

有没有人有使用事件处理程序与开放图层点的示例?

谢谢

    function mapCreate(lon,lat){
        map = new OpenLayers.Map("map1");
        var osm = new OpenLayers.Layer.OSM();
        vectors = new OpenLayers.Layer.Vector("Vector Layer");
        map.addLayer(osm);
        var center = new OpenLayers.LonLat(lon,lat).transform(
            new OpenLayers.Projection("EPSG:4326"),
            map.getProjectionObject()
        );

        point = new OpenLayers.Geometry.Point(center.lon,center.lat);
        vectors.addFeatures([new OpenLayers.Feature.Vector(point)]);
        drag = new OpenLayers.Control.DragFeature(vectors);
        //map.addLayer(vectors);
        map.addControl(drag);
        drag.activate();
        map.setCenter(center, 15);
        map.addLayer(vectors);
        point.events.register('moveend',point, function(evt){
            alert('hello');
        });

    }

这是我尝试过的示例,但出现了某些问题,导致这一部分无法正常工作。

point.events.register('moveend',point, function(evt){
                alert('hello');
            });

感谢代码更新,我不区分标记和点,你怎么看?标记不就是人们用来识别给定点的东西吗? - jcolebrand
我区分这两者是因为点属于向量,而标记不属于。我想。从查看标记的API中,有一个事件属性可能可以解决我的问题。 - Paul
啊,非常好,我明白你的意思了。希望现在你可以解决它。 - jcolebrand
1个回答

0

这里有一些类似的代码,我过去用它来在页面右侧的div列表上悬停时显示标记。包括它是因为它展示了我过去如何处理点。我不认为这正是你想要的。

/* Included for per-item hovering from the paginated layer. */
function onFeatureSelected(event) {
    hoveredItem = $(this).attr('lookup');

    /* Do something here to indicate the onhover */
    // find the layer pagination id
    var feature = findFeatureById(hoveredItem);

    if (feature) {

        // use the pagination id to find the event, and then trigger the click for that event to show the popup
        // also, pass a null event, since we don't necessarily have one. 
        feature.marker.events.listeners.click[0].func.call(feature, event)
    }
}
function onFeatureUnselected(event) {
    /* Do something here to indicate the onhover */
    // find the layer pagination id
    var feature = findFeatureById(hoveredItem);

    if (feature) {

        // use the pagination id to find the event, and then trigger the click for that event to show the popup
        // also, pass a null event, since we don't necessarily have one. 
        feature.marker.events.listeners.click[0].func.call(feature, event)
    }

    /* Do something here to stop the indication of the onhover */

    hoveredItem = null;
}

function findFeatureById(featureId) {
    for (var key in map.layers) {
        var layer = map.layers[key];
        if (layer.hasOwnProperty('features')) {
            for (var key1 in layer.features) {
                var feature = layer.features[key1];
                if (feature.hasOwnProperty('id') && feature.id == featureId) {
                    return feature;
                }
            }
        }
    }
    return null;
}

如果您想在创建点时添加此内容,我需要重新回忆一下过去是如何完成的。或者,GIS SE应该对您有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接