简体   繁体   English

当我单击谷歌地图时如何获取鼠标的偏移量(在地图上的左侧和顶部)

[英]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) 左:(<= 300)上:(<= 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. 我认为您可能想检查fromLatLngToContainerPixel()方法,该方法计算包含页面上地图的DOM元素中给定地理位置的像素坐标。

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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