How can I drag and drop a DOM object (image) (which is outside) into the map and then create a marker there?
1 answer
I'm not sure if you are using jQuery UI or not, but I would recommend using this library as it will make it easier to create a draggable DOM element and find the position for the element.
This example relies on jQuery UI to find the Div Pixel coordinates of a floated element after it has been moved.
Using jQuery UI - we're going to do a couple of things:
- Make element Draggable
- Create an event listener for when the drag is stopped.
-
Pass the position of the element as soon as the element has moved to another function called "createMarker" where we will create our marker
$("#dragMe").draggable({ stop:function(event,ui){ window.createMarker(ui.position); } });
Create marker will be responsible for performing several actions:
- Get the left and top position of our draggable element
- Calculate the equivalent GLatLng point for this point, taking into account the offset of our map.
-
Create a marker and add it to the map
function createMarker(position) { //Adjust Offset var offset = { left:10, top:-5 } //Create a new GPoint var myGPoint = new GPoint( position.left+offset.left, position.top+offset.top ); //Calculate the LatLng for this point var myLatLng = map.fromDivPixelToLatLng(myGPoint); //Create the Marker and add it to the Map var marker = new GMarker(myLatLng); map.addOverlay(marker); }
I've also created a working example of this code located here . Rest assured and let me know if you have any other questions, I hope this helps!
:)
+3
a source to share