[OpenLayers-Users] Register an event with a marker

Andreas Hocevar ahocevar at opengeo.org
Sun Jun 21 14:28:08 EDT 2009


Hi,

On Fri, Jun 19, 2009 at 3:00 PM, Adrian
Popa<adrian_gh.popa at romtelecom.ro> wrote:
> Here's what I'm trying to do (let me know if I'm reinventing the wheel):
> I want to load a bunch of markers onto an existing map, and I want to
> register a function to react to a click event. The function will create
> a popup and get some content through an ajax call to display on the map.

Please consider using a vector layer instead of a markers layer for
your markers. Then, you add a SelectFeature control to the map,
configured with your vector layer. Now all you have to do is create
your popup in a listener for the layer's featureselected event.

As a starting point, have a look at the sundials example
(http://www.openlayers.org/dev/examples/sundials.html). Create your
vector layer without the protocol and strategies options. You can add
your markers (called features for a vector layer) to the layer anytime
by saying:

var feature = new OpenLayers.Feature.Vector(
    new OpenLayers.Geometry.Point(lon, lat), {
        myAttribute1: "foo",
        myAttribute2: "bar"
    }, {
        externalGraphic: "yourMarkerImageURL"
    }
);
map.addFeatures([feature]);

For creating the popup content (see onFeatureSelect function in the
sundials example), you can e.g. access the myAttribute1 property by
evt.feature.attributes.myAttribute1.

> I went through a lot of examples on the net, but things got complicated
> - because they started using jugl.js (don't know what that is), or
> displaying KML content.

Jugl is a cool template system, created by Tim Schaub. And using
vector layers, it makes no difference whether you display KML content
or features that you add to your layer programmatically.

> Can you point me to an example of a popup that gets its contents through
> a definable function?

See above, I think that should get you there.

Regards,
Andreas.

-- 
Andreas Hocevar
OpenGeo - http://opengeo.org/
Expert service straight from the developers.



More information about the Users mailing list