简体   繁体   中英

How to get position of cursor when dragging?

I am working with Google map v3. And i am newbie with java script. have anyone know how to get position of cursor on web browser when dragging( before and after dragging)? Please teach me.

The best alternative I have is tracking the movement of the map's center point (latitude, longitude). Drag events don't seem to provide the mouse location on the map.

View here: http://jsfiddle.net/Hfu8D/

<!DOCTYPE html>
<html>
  <head>
    <style type="text/css">
      html, body, #map_canvas { margin: 0; padding: 0; height: 100% }
    </style>
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
    <script type="text/javascript">
      var map;
      var mapOptions = { center: new google.maps.LatLng(0.0, 0.0), zoom: 2,
        mapTypeId: google.maps.MapTypeId.ROADMAP };

      function initialize() {
        map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

        google.maps.event.addListener(map, 'dragstart', function(event) {
          document.getElementById("start").value = map.getCenter();
        });

        google.maps.event.addListener(map, 'dragend', function(event) {
          document.getElementById("end").value = map.getCenter();
        });


      }
      google.maps.event.addDomListener(window, 'load', initialize);
    </script>
  </head>
  <body>
    start: <input id="start" size="30"><br>
    end: <input id="end" size="30">
    <div id="map_canvas"></div>
  </body>
</html>

You can use the mousemove event for the map and add an event listener that updates a text box (or any other element) in real-time as the cursor moves.

Here's an example (forked from Mia's JS Fiddle):

http://jsfiddle.net/76VLh/3/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM