[OpenLayers-Users] Get LonLat of Draggable Marker

Brad Spencer brad at cubewerx.com.au
Sun Jul 26 03:37:29 EDT 2009


Kelvin,

I set up an oncomplete attribute on the drag control as so...

controls = { drag: new OpenLayers.Control.DragFeature(userMarkers,
{'onComplete': onCompleteMove} )	}
map.addControl(controls['drag']);
controls['drag'].activate();

Then you can get access to the feature attributes such as geometry.x and
geometry.y


function onCompleteMove(feature)
{
	if(feature)
	{	
		// replace coordinate values in feature attributes
		var pointX = feature.geometry.x;
		var pointY = feature.geometry.y;

etc....

Cheers, Brad...

-----Original Message-----
From: users-bounces at openlayers.org [mailto:users-bounces at openlayers.org] On
Behalf Of Kelvin Nicholson
Sent: Sunday, July 26, 2009 12:43 PM
To: users at openlayers.org
Subject: [OpenLayers-Users] Get LonLat of Draggable Marker

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);
        }

_______________________________________________
Users mailing list
Users at openlayers.org
http://openlayers.org/mailman/listinfo/users




More information about the Users mailing list