1

im trying to create a popup in Openlayers that would open when someone clicks a marker. And the marker is setup like this: new OpenLayers.Feature.Vector() and not with the ordinary marker layer.

The marker is then placed inside another layer vesselLayer.addFeatures([markerLayer])

which is then added to the map: map.addLayer([vesselLayer])

Now, How can i get a popup to show when someone selects one of the markers?

Let it be noted that each vesselLayer has only 1 marker. But there can be a 100 dynamically added vesselLayers to the map. (Made it that way for easy access trough the LayerSwitcher).

I tried reading the docs: http://openlayers.org/dev/examples/select-feature-openpopup.html

But i could not figgure it out.

The error that comes out of the console is saying that it cannot read the property "events" of null.. I double checked the object instantiation and it does get instantiated and the property is a part of the name.

Heres the object logged out right after instantiation.

Object Overview

1 Answer 1

2

try to use following code.

   vesselLayer.addMarker(marker);
   vesselLayer.events.register("click", vesselLayer, function(evt){
   popup = new OpenLayers.Popup("chicken",
                   evt.lonlat,
                   new OpenLayers.Size(200,200),
                   "My Content",
                   true);

   map.addPopup(popup);

i hope it helps you...

4
  • Gives me basicly the same error as when i tried using the documentation: Uncaught TypeError: Cannot read property 'events' of null
    – Christer
    Commented Jan 7, 2013 at 12:11
  • The error says everything : of null tells the vesselLayer is not initialized. Have you checked where you do vesselLayer = new OpenLayers.Layer.Markers( "Marks" ); map.addLayer(vesselLayer); ??
    – Magno C
    Commented Jan 7, 2013 at 14:08
  • 2
    By the way : why you need a layer with a single feature for each mark ? Follow @Aragon code to have various features in a unique layer with a popup.
    – Magno C
    Commented Jan 7, 2013 at 14:18
  • So i found out i had taken a wrong approach to this. I was adding Layer upon Layer, which would make one layer overlay another and kinda block the click events. So i created one single layer with just the features. And then using @Aragon 's Code to make it clickable! Thanks :)
    – Christer
    Commented Jan 9, 2013 at 9:18

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.