[OpenLayers-Users] Get LonLat of Draggable Marker

Kelvin Nicholson kelvin at kelvinism.com
Sat Jul 25 22:43:22 EDT 2009


Good morning/evening all:

Here's a quickie, I hope.  I have a small map that displays a draggable
marker - this is working fine. I want to allow the user to move around a
marker, and when they submit a form, it takes the lon/lat from the map
and includes it with the other data.

I'm currently using the drawFeatures DragFeature function from the
examples (see end of email).  However, I'm curious what is the best
method to re-obtain where the marker has been dragged.  Should I look at
using a doneDragging function out of the DragFeature control, and if so,
how would I call it? Or is there a more elegant method?

Below is a snippit of the JS I'm fumbling through.

Any advice is much appreciated!

Kelvin

window.onload = function (){ ...

            layer = new OpenLayers.Layer.Vector(
                "Marker Drop Shadows",
                {
                    styleMap: new OpenLayers.StyleMap({
                        // Set the external graphic and background
graphic images.
                        externalGraphic: "img/marker-gold.png",
                        backgroundGraphic: "img/marker_shadow.png",

                        // Makes sure the background graphic is placed
correctly relative
                        // to the external graphic.
                        backgroundXOffset: 0,
                        backgroundYOffset: -7,

                        // Set the z-indexes of both graphics to make
sure the background
                        // graphics stay in the background (shadows on
top of markers looks
                        // odd; let's not do that).
                        graphicZIndex: MARKER_Z_INDEX,
                        backgroundGraphicZIndex: SHADOW_Z_INDEX,

                        pointRadius: 10
                    }),
                    rendererOptions: {yOrdering: true}
                }
            );


          map.addLayers([mapnik, layer]);
          map.setCenter(centre, zoom);

            var dragFeature = new OpenLayers.Control.DragFeature(layer);

            map.addControl(dragFeature);

            dragFeature.activate();
            drawFeatures();
}

        function drawFeatures() {

            layer.removeFeatures(layer.features);

            // Create features at random around the center.
            var center = map.getViewPortPxFromLonLat(map.getCenter());

            // Add the ordering features. These are the gold ones that
all have the same z-index
            // and succomb to y-ordering.
            var features = [];

                var lonLat = new OpenLayers.LonLat({{ coords }});
                features.push(
                    new OpenLayers.Feature.Vector(
                        new OpenLayers.Geometry.Point(lonLat.lon,
lonLat.lat)
                    )
                );

            layer.addFeatures(features);
        }




More information about the Users mailing list