Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 8 down vote accepted

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.

share|improve this answer

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

i hope it helps you...

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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