开发者

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

开发者 https://www.devze.com 2022-12-22 15:58 出处:网络
it would be like this : 开发者_开发技巧 <div id=\"map_canvas\" style=\"width: 500px; height: 300px;float:left;\"></div>

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>
0

精彩评论

暂无评论...
验证码 换一张
取 消