如何在谷歌地图api中从鼠标到画线画线?

时间:2012-11-25 15:52:22

标签: google-maps

我想使用 google maps api

从鼠标光标到固定点绘制一条线

我找到了代码...

我可以定义固定点 但我无法定义鼠标位置

<!DOCTYPE html>
<html>
<head>
<script
src="http://maps.googleapis.com/maps/api/js?key=AIzaSyCwID2UsBJvwVKEMx_U53brmIC8EOLsBFo&sensor=false">
</script>

<script>
 var bounds = new google.maps.LatLngBounds();
    var length = markers.length;
    for (var i = 0; i < length; i++) {
        bounds.extend(new google.maps.LatLng(markers[i].lat, markers[i].lng));
        map.fitBounds(bounds);
    }


var x=new google.maps.LatLng(52.395715,4.888916);
var stavanger=new google.maps.LatLng(markers[i].lat, markers[i].lng);
var london=new google.maps.LatLng(51.508742,-0.120850);

function initialize()
{
var mapProp = {
  center:x,
  zoom:4,
  mapTypeId:google.maps.MapTypeId.ROADMAP
  };

var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);

var myTrip=[stavanger,london];
var flightPath=new google.maps.Polyline({
  path:myTrip,
  strokeColor:"#0000FF",
  strokeOpacity:0.9,
  strokeWeight:2
  });

flightPath.setMap(map);
}

google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>

<body>
<div id="googleMap" style="width:500px;height:380px;"></div>
</body>
</html>

如何检索鼠标坐标?

示例 http://apps.qiblalocator.com/ext/embed/?address=usa

1 个答案:

答案 0 :(得分:1)

您可以像这样检索鼠标坐标(假设您有一个元素ID为“latlgn”的div)。

    google.maps.event.addListener(map, "mousemove", function(pt) { 
      document.getElementById("latlgn").innerHTML = pt.latLng; 
    });

来自此thread in the google maps api v3 group

中的示例