[OpenLayers-Users] How to implement draggable Markers?

Brad Spencer brad at cubewerx.com.au
Tue Feb 10 23:00:39 EST 2009


Alan,

I need to move markers by dragging the selected one to a new location then
also get its new lat/long for saving. 

The included thread indicates that this should be easy. I have not seen any
examples on this and tried it myself with no luck.

I have got a mouseover and mouseout events working just fine for a popup
report but I am having problems getting mousedown/mouseup to work.

I am doing something incredibly stupid I know, so can you help me here?

My failing code looks like this:

    var marker = feature.createMarker();
	
	// create the framedCloud popup event
      marker.events.register("mouseover", feature, function (evt) 
	{
		if (this.popup == null) 
		{
			this.popup = this.createPopup();
                  map.addPopup(this.popup);
                  this.popup.show();
		} 
		else 
		{
			this.popup.toggle();
            }
            currentPopup = this.popup;
		OpenLayers.Event.stop(evt);
	});
	
	// create the framedCloud popup event
      marker.events.register("mouseout", feature, function (evt) 
	{
		this.popup.toggle();
            currentPopup = this.popup;
            OpenLayers.Event.stop(evt);
	});

	// create the mousedown event to move a marker
	marker.events.register('mousedown', feature, function (evt)
	{
		// not sure what needs to go here?????
	});
	
	// drop the marker
	marker.events.register('mouseup', feature, function (evt)
	{	
		this.moveTo(new OpenLayers.Pixel(evt.xy.x , evt.xy.y))
		OpenLayers.Event.stop(evt);
	});

	// add the marker to the markers layer
    userMarkers.addMarker(marker);

Cheers, Brad....



-----Original Message-----
From: users-bounces at openlayers.org [mailto:users-bounces at openlayers.org] On
Behalf Of Alan Boudreault
Sent: Thursday, January 08, 2009 3:55 AM
To: KartoPete
Cc: users at openlayers.org
Subject: Re: [OpenLayers-Users] How to implement draggable Markers?

KartoPete wrote:
>
> Alan Boudreault wrote:
>   
>> KartoPete wrote:
>>     
>>> Hi guys, 
>>> I#m working with OpenLayers2.6 and try make my markers draggable. I
tried
>>> a
>>> few edited classes I found here but it wouldn't work out, even the
posted
>>> example(long time ago) wouldn't work on my machine. Does anyone know if
>>> draggable markers is already working with the new or future versions? Or
>>> has
>>> someone a idea on how to implement this? 
>>> thx Pete
>>>   
>>>       
>> Something that you can do is to create a new class "DraggableMarker"
>> that inherit of OpenLayers.Marker. This class will mainly handle 2
>> events: mouseup and mousedown:
>>
>>  this.events.register('mousedown', this, this._markerDown);
>>  this.events.registerPriority('mouseup', this, this._markerUp);
>>
>> Thanks, I'll try
>> But why registerPriority()?
>>
>> After that, the mousedown function will activate the event "mousemove"
>> and the mouseup will disable it. And the mousemove handle will calculate
>> the new position and move the marker on the fly.
>>
>> Hope this help.
>> Alan
>>
>>
>> -- 
>> Alan Boudreault
>> Mapgears
>> http://www.mapgears.com
>>
>> _______________________________________________
>> Users mailing list
>> Users at openlayers.org
>> http://openlayers.org/mailman/listinfo/users
>>
>>
>>     
>
>   
You can try without the "priority" ..... i don't remember why we used
this. Maybe a problem with an old version.

-- 
Alan Boudreault
Mapgears
http://www.mapgears.com

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




More information about the Users mailing list