4

I'm trying to have some very basic functionality with vector points with Openlayers 2.12 and I can't figure out how to track a click event on a point.

I create a map using the following code and add one point:

var map;
function init(){
    map = new OpenLayers.Map('map');

    var gsat = new OpenLayers.Layer.Google(
        "Google Satellite",
        {type: google.maps.MapTypeId.SATELLITE, numZoomLevels: 22}
    );        

    map.addLayer(gsat);

    var vectorLayer = new OpenLayers.Layer.Vector("vector");

    vectorLayer.addFeatures(
        new OpenLayers.Feature.Vector(
            new OpenLayers.Geometry.Point(-57.635021, -25.276987).transform(
            new OpenLayers.Projection("EPSG:4326"),
            map.getProjectionObject()
            )
        )
    ) 

    map.setCenter(new OpenLayers.LonLat(10.2, 48.9).transform(
        new OpenLayers.Projection("EPSG:4326"),
        map.getProjectionObject()
        ), 2
    );        

    map.addLayer(vectorLayer);

Then, I've tried to identify when the point is clicked:

    // detect when points are clicked
    pointClick = new OpenLayers.Control.GetFeature(vectorLayer, OpenLayers.Handler.Point, 
    {eventListeners:{"click": clickNotice}};                                            
    map.addControl(pointClick);

    // detect point events 
    function clickNotice(evt) {
        alert('Point clicked');
        point.deactivate(); 
    }          

}

This is not working, (although I don't get any errors in Firebug), but I'm not sure of the correct syntax for handling points.

2 Answers 2

9

GetFeature control designed for retrieve features from a server and fires events that notify applications of the selected features. In your case you should use SelectFeature control:

selectFeature = new OpenLayers.Control.SelectFeature(
    vectorLayer,
    {
        onSelect: clickNotice,
        autoActivate: true
    }
);
map.addControl(selectFeature);

Your example is not working beacause you have to activate GetFeature control before using it.

1

@drnextgis is right. checkout this example here for some inspiration.

i hope it helps you...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.