在标记和定点之间画线 - Google Maps API v3 [英] draw line between marker and fixed point - Google Maps API v3

查看:138
本文介绍了在标记和定点之间画线 - Google Maps API v3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在标记和定点之间绘制线条 - Google Maps API v3

how to draw line between marker and fixed point - Google Maps API v3

我使用该代码在两个固定点
之间画线,使用google api v3标记

I used that code to draw a line between 2 fixed points and put a marker using google api v3

如何替换标记位置 2个固定点之一?

how can i replace the marker position instead of one of the 2 fixed points ?

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

<script>


function initialize()
{

var x=new google.maps.LatLng(52.395715,4.888916);


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

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


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

      var marker = new google.maps.Marker({
  draggable: true,
  position: x,
  map: map,
  title: "Your location"
  });


  google.maps.event.addListener(marker, 'dragend', function (event) {
    document.getElementById("latbox").value = this.getPosition().lat();
    document.getElementById("lngbox").value = this.getPosition().lng();
});

var lat = "80";
var lng = "80"; 
var stavanger=new google.maps.LatLng(lat,lng);
var london=new google.maps.LatLng(51.508742,-0.120850);

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>
 <div id="latlong">
    <p>Latitude: <input size="20" type="text" id="latbox" name="lat" ></p>
    <p>Longitude: <input size="20" type="text" id="lngbox" name="lng" ></p>
  </div>
<body onLoad="initialize()">

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


推荐答案

设置 myTrip -variable to

set the myTrip-variable to

[stavanger,marker.getPosition()]

这将最初在stavanger和标记位置之间画一条线。

This will initially draw a line between stavanger and the markers position.

标记被移动时更新的行,在marker.dragend的回调中使用PolyLine(flightPath)的setPath-method

To keep the line updated when the marker has been moved, use the setPath-method of the PolyLine(flightPath) inside the callback of marker.dragend

  google.maps.event.addListener(marker, 'dragend', function (event) {
    document.getElementById("latbox").value = this.getPosition().lat();
    document.getElementById("lngbox").value = this.getPosition().lng();
    flightPath.setPath([flightPath.getPath().getAt(0),
                        new google.maps.LatLng(this.getPosition().lat(),
                                               this.getPosition().lng())]);
});






根据要求,修改设置line to the center:


As requested, modifications to set an end of the line to the center:

var myTrip=[stavanger,map.getCenter()];

google.maps.event.addListener(map, 'center_changed', function (event) {
    flightPath.setPath([flightPath.getPath().getAt(0),this.getCenter()])
});

这篇关于在标记和定点之间画线 - Google Maps API v3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆