简体   繁体   中英

Open Layers point event

Does anyone have an example of how to use an event handler with open layers point?

Thanks

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

    }

This is an example of what i have tried, for some reason this part does not work

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

Here's some similar code that I've used in the past to display a marker on hover on a list of divs on the right hand side of the page. Including it because it demonstrates how I've worked with the points in the past. I don't think it's quite what you want.

/* 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;
}

If you're wanting to add this to the point as it is being created, I will have to refresh my memory on how that's been done in the past. Alternately, the GIS SE should be helpful to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM