javascript - How to make overlay div in google map dragable? -
i need drag position of div. used below code , not worked. think done mistake on here.also need rotate div can place image @ right position
var overlay; debugoverlay.prototype = new google.maps.overlayview(); function initialize() { var mapoptions = { zoom: 15, center: new google.maps.latlng(40.743388, -74.007592) }; var map = new google.maps.map(document.getelementbyid('map-canvas'), mapoptions); var swbound = new google.maps.latlng(40.73660837340877, -74.01852328); var nebound = new google.maps.latlng(40.75214181, -73.99661518216243); var bounds = new google.maps.latlngbounds(swbound, nebound); var srcimage = 'http://library.marist.edu/pix/libmap3.jpg'; overlay = new debugoverlay(bounds, srcimage, map); var markera = new google.maps.marker({ position: swbound, map: map, draggable: true }); var markerb = new google.maps.marker({ position: nebound, map: map, draggable: true }); google.maps.event.addlistener(markera, 'drag', function () { var newpointa = markera.getposition(); var newpointb = markerb.getposition(); var newbounds = new google.maps.latlngbounds(newpointa, newpointb); overlay.updatebounds(newbounds); }); google.maps.event.addlistener(markerb, 'drag', function () { var newpointa = markera.getposition(); var newpointb = markerb.getposition(); var newbounds = new google.maps.latlngbounds(newpointa, newpointb); overlay.updatebounds(newbounds); }); google.maps.event.addlistener(markera, 'dragend', function () { var newpointa = markera.getposition(); var newpointb = markerb.getposition(); console.log("point1" + newpointa); console.log("point2" + newpointb); }); google.maps.event.addlistener(markerb, 'dragend', function () { var newpointa = markera.getposition(); var newpointb = markerb.getposition(); console.log("point1" + newpointa); console.log("point2" + newpointb); }); } function debugoverlay(bounds, image, map) { this.bounds_ = bounds; this.image_ = image; this.map_ = map; this.div_ = null; this.setmap(map); } debugoverlay.prototype.onadd = function () { var div = document.createelement('div'); div.style.borderstyle = 'none'; div.style.borderwidth = '0px'; div.style.position = 'absolute'; var img = document.createelement('img'); img.src = this.image_; img.style.width = '100%'; img.style.height = '100%'; img.style.opacity = '0.5'; img.style.position = 'absolute'; div.appendchild(img); this.div_ = div; var panes = this.getpanes(); panes.overlaylayer.appendchild(div); }; debugoverlay.prototype.draw = function () { debugger; var overlayprojection = this.getprojection(); var sw = overlayprojection.fromlatlngtodivpixel(this.bounds_.getsouthwest()); var ne = overlayprojection.fromlatlngtodivpixel(this.bounds_.getnortheast()); var div = this.div_; div.style.left = sw.x + 'px'; div.style.top = ne.y + 'px'; div.style.width = (ne.x - sw.x) + 'px'; div.style.height = (sw.y - ne.y) + 'px'; }; debugoverlay.prototype.updatebounds = function (bounds) { this.bounds_ = bounds; this.draw(); }; debugoverlay.prototype.onremove = function () { this.div_.parentnode.removechild(this.div_); this.div_ = null; }; google.maps.event.adddomlistener(window, 'load', initialize);
(i spent many minutes expect @ first...)
as far understand question , code, want drag floor map image directly, , corner markers followed it, correct?
html5 supports drag events. http://www.w3schools.com/html/html5_draganddrop.asp
however, in google maps v3, mouse events captured google maps. here trick.
capture mouse event
in order catch mouse events, need insert div (named mousetarget in code) overlaymousetarget pane. overlaymousetarget top layer in google maps v3.
but sets .style.display=none in normal status (not dragging). becomes .style.display=block if mouse down mousetarget div.
however, if in normal status, mousetarget div have catch position image.
dragging events
when mousedown on mousetarget, have cancel event @ once in order prevent dragging map.
then, need handle dragstart, drag, , dragleave events yourself. means need follow mouse cursor position, recalculate image position, , marker positions.
here answer first question i need drag position of div.

Comments
Post a Comment