Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
563 views
in Technique[技术] by (71.8m points)

jquery - google maps drag and drop objects into google maps from outside the Map

i'm working on a project and i need a help concerning how to drag and drop objects from outside the map into it. I tried multitude of examples but i didn't find the solution so if you can just give a sample of code to help and thanks .

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Check if this is more or less what you wanted ( you will need to provide a small icon file to be dragged - update file name acorrdingly in the source). Just to mention the shelf with icons to drag can be placed anywhere also outside of the map.

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<style type="text/css">
*{
margin: 0px;
padding: 0px;
}
html { height: 100% }
body{ height: 100%; margin: 0px; padding: 0px;}
#map_canvas { height: 100% }
#shelf{position:fixed; top:10px; left:500px; height:100px;width:200px;background:white;opacity:0.7;}
#draggable {position:absolute;top:10px, left:10px; width: 30px; height: 30px;z-index:1000000000;}
</style>
<script type="text/javascript">
$(document).ready(function() {
$("#draggable").draggable({helper: 'clone',
stop: function(e) {
    var point=new google.maps.Point(e.pageX,e.pageY);
    var ll=overlay.getProjection().fromContainerPixelToLatLng(point);
    placeMarker(ll);
    }
});
});
</script>
<script type="text/javascript"
src="http://maps.google.com/maps/api/js?sensor=false">
</script>
<script type="text/javascript">
var $map;
var $latlng;
var overlay;
function initialize() {
var $latlng = new google.maps.LatLng(66.5, 25.733333);
var myOptions = {
  zoom: 3,
  center: $latlng,
  mapTypeId: google.maps.MapTypeId.ROADMAP,
  mapTypeControlOptions: {
    style: google.maps.MapTypeControlStyle.DROPDOWN_MENU,
    position: google.maps.ControlPosition.TOP_LEFT },
     zoomControl: true,
zoomControlOptions: {
    style: google.maps.ZoomControlStyle.LARGE,
    position: google.maps.ControlPosition.LEFT_TOP
},
scaleControl: true,
scaleControlOptions: {
    position: google.maps.ControlPosition.TOP_LEFT
},
streetViewControl: false,

  panControl:false,

};
$map = new google.maps.Map(document.getElementById("map_canvas"),
    myOptions);

overlay = new google.maps.OverlayView();
overlay.draw = function() {};
overlay.setMap($map);
} 
function placeMarker(location) {
  var marker = new google.maps.Marker({
  position: location, 
  map: $map,
  icon:'spring-hot.png'
  });

}
</script>
</head>
<body onload="initialize()">
    <div id="map_canvas">    </div>
<div id='shelf'>Drag the image<br/><img id="draggable" src='spring-hot.png'/><div>
</body>
</html>

K


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...