gpt4 book ai didi

javascript - 用折线连接多个标记

转载 作者:行者123 更新时间:2023-12-03 04:36:11 25 4
gpt4 key购买 nike

我尝试了这段代码,但折线没有成功,可能是因为小问题,我无法获得连接标记的折线。
我需要将多个标记与折线连接起来,也欢迎使用任何替代代码。

var locations = [];
// call php array
var latitude = <?php echo json_encode($latitude); ?>;
var longitude = <?php echo json_encode($longitude); ?>;
for(var i=0;i<20;i++){
locations.push(["current",latitude[i], longitude[i]]);
}

var marker;
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 5,
center: new google.maps.LatLng(12.289774217827633, 76.30976921188685)
});

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


flightPath.setMap(map);
var infowindow = new google.maps.InfoWindow();
var marker, i;

for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});

google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
}

最佳答案

flightPath should be called as an array.

请尝试这个更新的代码。

var locations = [

];

// call php array
var latitude = <?php echo json_encode($latitude); ?>;
var longitude = <?php echo json_encode($longitude); ?>;
for(var i=0;i<20;i++){
locations.push(["current",latitude[i], longitude[i]]);
}
var marker;
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 5,
center: new google.maps.LatLng(12.289774217827633, 76.30976921188685)
});


// locations.push(["current",test2, test3]);
var infowindow = new google.maps.InfoWindow();

var marker, i;
var flightPlanCoordinates = [];

for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});
flightPlanCoordinates.push(marker.getPosition());
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
var flightPath = new google.maps.Polyline({
map: map,
path: flightPlanCoordinates,
strokeColor: "#ff0000",

strokeOpacity: 1.0,
strokeWeight: 2
});
}

关于javascript - 用折线连接多个标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43291764/

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