简体   繁体   中英

how to get the mouse's offset(left and top on the maps) when i clicked the google maps

it would be like this :

<div id="map_canvas" style="width: 500px; height: 300px;float:left;"></div>

left:(<=300) top:(<=500)

    GEvent.addListener(map, "click", function() {
      //aFn()
    });

i want to make a polyline when i clicked:

var polyline = new GPolyline([onePoint,twoPoint], "#ff0000", 5);
                            map.addOverlay(polyline);

thanks

I think you may want to check the fromLatLngToContainerPixel() method, which computes the pixel coordinates of the given geographical point in the DOM element that contains the map on the page.

You may want to try the following example. Click anywhere on the map to get the container coordinates:

<!DOCTYPE html>
<html> 
<head> 
   <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
   <title>Google Maps fromLatLngToContainerPixel Demo</title> 
   <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false" 
           type="text/javascript"></script> 
</head> 
<body onunload="GUnload()"> 

   <div id="map_canvas" style="width: 500px; height: 300px;"></div> 

   <script type="text/javascript"> 

   if (GBrowserIsCompatible()) {
      var map = new GMap2(document.getElementById("map_canvas"));

      map.setCenter(new GLatLng(39.00, -77.00), 10);

      GEvent.addListener(map, "click", function(overlay, latlng) {
          alert(map.fromLatLngToContainerPixel(latlng).x + ', ' +
                map.fromLatLngToContainerPixel(latlng).y)
      });
   }
   </script> 
</body> 
</html>

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