簡體   English   中英

當我單擊谷歌地圖時如何獲取鼠標的偏移量(在地圖上的左側和頂部)

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

就像這樣:

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

左:(<= 300)上:(<= 500)

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

我想在單擊時制作一條折線:

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

謝謝

我認為您可能想檢查fromLatLngToContainerPixel()方法,該方法計算包含頁面上地圖的DOM元素中給定地理位置的像素坐標。

您可能要嘗試以下示例。 單擊地圖上的任意位置以獲取容器坐標:

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