gpt4 book ai didi

javascript - 在 Google map 上显示路径时出现问题

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

我有多个经度和纬度点。例如:

1 long lat, starting point   
2 long lat
3 long lat
4 long lat
.
.
.

enter image description here enter image description here

我的代码如下

  <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var markers = [
{
"title": '1',
"lat": '30.705911',
"lng": '76.679656',
"description": '1'
}
,
{
"title": '2',
"lat": '30.701713',
"lng": '76.684097',
"description": '2'
},
{
"title": '2',
"lat": '30.703291',
"lng": ' 76.701022',
"description": '2'
},{
"title": '2',
"lat": '30.691888',
"lng": ' 76.710721',
"description": '2'
}

];
window.onload = function () {
var mapOptions = {
center: new google.maps.LatLng(markers[0].lat, markers[0].lng),
zoom: 10,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
var infoWindow = new google.maps.InfoWindow();
var lat_lng = new Array();
var latlngbounds = new google.maps.LatLngBounds();
for (i = 0; i < markers.length; i++) {
var data = markers[i]
var myLatlng = new google.maps.LatLng(data.lat, data.lng);
lat_lng.push(myLatlng);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: data.title
});
latlngbounds.extend(marker.position);
(function (marker, data) {
google.maps.event.addListener(marker, "click", function (e) {
infoWindow.setContent(data.description);
infoWindow.open(map, marker);
});
})(marker, data);
}
map.setCenter(latlngbounds.getCenter());
map.fitBounds(latlngbounds);

//***********ROUTING****************//

//Intialize the Path Array
var path = new google.maps.MVCArray();

//Intialize the Direction Service
var service = new google.maps.DirectionsService();

//Set the Path Stroke Color
var poly = new google.maps.Polyline({ map: map, strokeColor: '#4986E7' });

//Loop and Draw Path Route between the Points on MAP
for (var i = 0; i < lat_lng.length; i++) {
if ((i + 1) < lat_lng.length) {
var src = lat_lng[i];
var des = lat_lng[i + 1];
path.push(src);
poly.setPath(path);
service.route({
origin: src,
destination: des,
travelMode: google.maps.DirectionsTravelMode.DRIVING
}, function (result, status) {
if (status == google.maps.DirectionsStatus.OK) {
for (var i = 0, len = result.routes[0].overview_path.length; i < len; i++) {
path.push(result.routes[0].overview_path[i]);
}
}
});
}
}
}
</script>
<div id="dvMap" style="width: 800px; height: 800px">
</div>

我遇到了一个问题,线条直接连接到第三点和第四点。

最佳答案

不要将src插入路径,它与概述路径中的第一个点重复。删除这一行:

path.push(src);

working fiddle

代码片段:

var markers = [{
"title": '1',
"lat": '30.705911',
"lng": '76.679656',
"description": '1'
}, {
"title": '2',
"lat": '30.701713',
"lng": '76.684097',
"description": '2'
}, {
"title": '2',
"lat": '30.703291',
"lng": ' 76.701022',
"description": '2'
}, {
"title": '2',
"lat": '30.691888',
"lng": ' 76.710721',
"description": '2'
}

];
window.onload = function() {
var mapOptions = {
center: new google.maps.LatLng(markers[0].lat, markers[0].lng),
zoom: 10,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
var infoWindow = new google.maps.InfoWindow();
var lat_lng = new Array();
var latlngbounds = new google.maps.LatLngBounds();
for (i = 0; i < markers.length; i++) {
var data = markers[i]
var myLatlng = new google.maps.LatLng(data.lat, data.lng);
lat_lng.push(myLatlng);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: data.title
});
latlngbounds.extend(marker.position);
(function(marker, data) {
google.maps.event.addListener(marker, "click", function(e) {
infoWindow.setContent(data.description);
infoWindow.open(map, marker);
});
})(marker, data);
}
map.setCenter(latlngbounds.getCenter());
map.fitBounds(latlngbounds);

//***********ROUTING****************//

//Intialize the Path Array
var path = new google.maps.MVCArray();

//Intialize the Direction Service
var service = new google.maps.DirectionsService();

//Set the Path Stroke Color
var poly = new google.maps.Polyline({
map: map,
strokeColor: '#4986E7'
});

//Loop and Draw Path Route between the Points on MAP
for (var i = 0; i < lat_lng.length; i++) {
if ((i + 1) < lat_lng.length) {
var src = lat_lng[i];
var des = lat_lng[i + 1];
// path.push(src);
poly.setPath(path);
service.route({
origin: src,
destination: des,
travelMode: google.maps.DirectionsTravelMode.DRIVING
}, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
for (var i = 0, len = result.routes[0].overview_path.length; i < len; i++) {
path.push(result.routes[0].overview_path[i]);
}
}
});
}
}
}
html,
body,
#dvMap {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="http://maps.google.com/maps/api/js"></script>
<div id="dvMap" ></div>

关于javascript - 在 Google map 上显示路径时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27686811/

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