gpt4 book ai didi

javascript - 如何在谷歌地图上动态绘制多段线

转载 作者:行者123 更新时间:2023-11-30 07:40:11 25 4
gpt4 key购买 nike

我无法在谷歌地图上绘制折线。正在动态获取值。

var flightPlanCoordinates=[];
n = "new google.maps.LatLng(";
q = ")";
var flightPlanCoordinates = new Array();
for(i=0;i<len;i++)
{

o =n+r[i].split(',')[0]+","+r[i].split(',')[1]+q;

flightPlanCoordinates.push(o);

}


var flightPath = new google.maps.Polyline({
path: flightPlanCoordinates,
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2
});

flightPath.setMap(map);
google.maps.event.addDomListener(window, 'load', initialize);

我有一个这样的列表 r=['13.034056,80.250489|13.036324,80.248538|13.026394,80.237562']。我在上面的部分有问题。但我可以静态传递值,并且我得到了我预期的折线。提前致谢。

最佳答案

您正在创建一个字符串数组 ("new google.maps.LatLng(coor1,coord2)"),而不是 google.maps.LatLng 的数组对象。

这对我有用。

var r=['13.034056,80.250489|13.036324,80.248538|13.026394,80.237562'];
var coordinates = r[0].split("|");
var flightPlanCoordinates = new Array();
for(i=0;i<coordinates.length;i++)
{
var point =new google.maps.LatLng(coordinates[i].split(',')[0],coordinates[i].split(',')[1]);
flightPlanCoordinates.push(point);
}


var flightPath = new google.maps.Polyline({
path: flightPlanCoordinates,
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2
});

flightPath.setMap(map);
}
google.maps.event.addDomListener(window, 'load', initialize);

working example

代码片段:

function initialize() {
var mapOptions = {
zoom: 3,
center: new google.maps.LatLng(0, -180),
mapTypeId: google.maps.MapTypeId.TERRAIN
};

var map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);

var r=['13.034056,80.250489|13.036324,80.248538|13.026394,80.237562'];
var coordinates = r[0].split("|");
var flightPlanCoordinates = new Array();
var bounds = new google.maps.LatLngBounds();
for(i=0;i<coordinates.length;i++)
{
var point =new google.maps.LatLng(coordinates[i].split(',')[0],coordinates[i].split(',')[1]);
bounds.extend(point);
flightPlanCoordinates.push(point);
}


var flightPath = new google.maps.Polyline({
path: flightPlanCoordinates,
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2
});

flightPath.setMap(map);
map.fitBounds(bounds);
}
google.maps.event.addDomListener(window, 'load', initialize);
html, body, #map-canvas {
height: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="map-canvas"></div>

关于javascript - 如何在谷歌地图上动态绘制多段线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19594040/

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