[OpenLayers-Users] Problem with addMarker(): OpenLayers 2.4

Nick Whitelegg nick at hogweed.org
Sun Oct 28 10:29:57 EDT 2007


Hello everyone,

Getting a mysterious problem with addMarker() which I am unable to resolve. 
It's on my site, Freemap (www.free-map.org.uk).

If you select "Walk route" mode, you can draw a walk (hiking) route on top of 
the map - this uses the OpenLayers Vector drawing facilities. Then, when you 
have drawn your walking route, the route appears in red. The user then should 
be able to annotate the route by clicking on the map and supplying 
annotations (i.e. directions, to help people follow the walk). 

Clicking the map brings a dialog box in which the user can supply an 
annotation. Clicking Go will send the annotation to the server. When the 
response is received, a marker is meant to appear on the map - however it 
doesn't, the JavaScript crashes out without an error message. The AJAX 
callback function is as follows:

    this.annotationAdded = function()
    {
        self.ARemovePopup();

        var icon = self.walkrouteMarkersLayer.defaultIcon;

        var a = self.parent.mapclickpos; // this gives a sensible answer
        var b = self.parent.map.getLonLatFromViewPortPx(a); // so does this

        var marker = new OpenLayers.Marker(b,icon);
        if(self.walkrouteMarkersLayer)
        {
	    // this if runs
            alert('adding marker at lonlat: ' + b);
            self.walkrouteMarkersLayer.addMarker(marker);
            alert('marker added.');
        }
    }

It fails at the line:

 self.walkrouteMarkersLayer.addMarker(marker);

(self is a reference to the current object). Everything else is OK e.g. the 
lat/lon is sensible.

I've tracked the error down to the following line in the addMarker() function 
of OpenLayers.Layer.Markers:

 this.drawMarker(marker);

Strangely the same layer works if I retrieve markers by bounding box.

Any idea what might be happening here? Relevant code:

http://www.free-map.org.uk/freemap/javascript/FreemapClient.js (my 'main' 
JavaScript class)

http://www.free-map.org.uk/freemap/javascript/WalkRouteMarkersLayer.js (the 
layer I'm adding markers to)

Thanks,
Nick



More information about the Users mailing list