gpt4 book ai didi

javascript - 在标记和固定点之间绘制线 - Google Maps API v3

转载 作者:行者123 更新时间:2023-11-28 02:35:18 25 4
gpt4 key购买 nike

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

我使用该代码在两个固定点之间画一条线并放置一个标记使用谷歌API v3

如何替换 2 个固定点之一的标记位置而不

<!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变量设置为

[stavanger,marker.getPosition()]

这最初将在斯塔万格和标记位置之间画一条线。

要在标记移动时保持线条更新,请在marker.dragend的回调中使用PolyLine(flightPath)的setPath方法

  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())]);
});
<小时/>

根据要求,进行修改以将线的末端设置为中心:

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

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

关于javascript - 在标记和固定点之间绘制线 - Google Maps API v3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13554565/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com