JSFiddle - React, Tailwind, and code Playground
HTML
<script src="http://maps.google.com/maps/api/js?sensor=false&.js"></script>
<div id="dropzone"></div>
<div id="map"></div>
run any test blah<br>
<button id="huh">test</button><br>
<br>
fake marker we will replicate<br>
<div id="gmarker"></div><br>
<br>
drag this to see how it should work from the map<br>
<div id="example"></div><br>
CSS
html, body { margin: 0px; padding: 0px; }
#dropzone { width: 100px; height: 100px; background-color: red; }
#dropzone.drophere { background-color: yellow }
#dropzone.dropaccept { background-color: green }
#map{
width: 300px;
height: 300px;
}
#gmarker {
width: 20px;
height: 34px;
overflow: hidden;
background: url('http://maps.gstatic.com/intl/en_us/mapfiles/markers/marker_sprite.png') no-repeat no-repeat;
}
#marker {
position: absolute;
width: 20px;
height: 34px;
overflow: hidden;
background: url('http://maps.gstatic.com/intl/en_us/mapfiles/markers/marker_sprite.png') no-repeat no-repeat;
}
#example{
position: absolute;
width: 20px;
height: 34px;
overflow: hidden;
background: url('http://maps.gstatic.com/intl/en_us/mapfiles/markers/marker_sprite.png') no-repeat no-repeat;
}
JavaScript
var map = null;
var marker = null;
//anchor by default is bottom middle of your icon we are using a 20x34 image
var anchor = {x: 10, y: 34 }
var myOptions = {
zoom: 12,
draggable: false,
center: new google.maps.LatLng(36.158251, -115.137405),
mapTypeId: google.maps.MapTypeId.ROADMAP,
disableDefaultUI: true,
};
map = new google.maps.Map($("#map")[0], myOptions);
//put the dropzone control in google maps
//map.controls[google.maps.ControlPosition.TOP_LEFT].push(document.getElementById('dropzone'));
marker = new google.maps.Marker({
map: map,
draggable: true,
raiseOnDrag: false,
flat: true,
animation: google.maps.Animation.DROP,
position: new google.maps.LatLng(36.158251, -115.147415),
});
//reset marker on delete clone
google.maps.event.addListener(marker, 'dragend', function() {
$('#marker').remove();
this.setPosition(new google.maps.LatLng(36.158251, -115.137405))
});
//make my clone!
google.maps.event.addListener(marker, 'dragstart', function(e) {
$("#gmarker").clone().attr('id', 'marker').appendTo('body');
$("#marker").draggable().trigger('click');
//hover over the real marker
//Map position = Event.pixel - (icon unique anchor point)
//Document position = position of google map container
//position = Map Position + Document Position
var pos = $("#map").offset();
$("#marker").offset({top: e.pixel.y+pos.top-anchor.y, left: e.pixel.x+pos.left-anchor.x});
});
//move my clone with me
google.maps.event.addListener(marker, 'drag', function(e) {
var pos = $("#map").offset();
$("#marker").trigger('drag').trigger('mousedown');
$("#marker").offset({top: e.pixel.y+pos.top-anchor.y, left: e.pixel.x+pos.left-anchor.x});
});
$("#example").draggable();
$("#dropzone").droppable({
accept: "#marker, #example",
activeClass: "drophere",
hoverClass: "dropaccept",
drop: function(event, ui) {
$(this)
.addClass("ui-state-highlight")
...