Requerment是在谷歌地图上使用多个路点在起始位置和结束位置之间绘制路径。路径很好,但问题是坐标之间有几条直线。 如何删除坐标之外的额外直线?
我的代码如下:
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var markers = [
{
"title": 'A',
"lat": '26.3489',
"lng": ' 92.6845',
"description": 'A'
}
,
{
"title": 'B',
"lat": '26.3546',
"lng": '92.6902',
"description": 'B'
}
,
{
"title": 'D',
"lat": '26.3508',
"lng": '92.7102',
"description": 'D'
}
,
{
"title": 'E',
"lat": '26.4285',
"lng": '92.8497',
"description": 'E'
}
,
{
"title": 'E',
"lat": '26.5486',
"lng": '92.9008',
"description": 'E'
}
,
{
"title": 'E',
"lat": ' 26.6567',
"lng": '92.7717',
"description": 'E'
}
,
{
"title": 'C',
"lat": '26.807',
"lng": '92.895',
"description": 'C'
}
,
];
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****************//
//Initialize the Path Array
var path = new google.maps.MVCArray();
//Initialize 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: 500px; height: 500px">
</div>
中的样本
答案 0 :(得分:5)
不要将src
点推到路径上(规则服务是异步的)。删除这一行:
path.push(src);
并更改代码以将每个方向结果渲染为单独的折线。
相关问题:
代码段
var markers = [{
"title": 'A',
"lat": '26.3489',
"lng": ' 92.6845',
"description": 'A'
}, {
"title": 'B',
"lat": '26.3546',
"lng": '92.6902',
"description": 'B'
}, {
"title": 'D',
"lat": '26.3508',
"lng": '92.7102',
"description": 'D'
}
, {
"title": 'E',
"lat": '26.4285',
"lng": '92.8497',
"description": 'E'
}, {
"title": 'E',
"lat": '26.5486',
"lng": '92.9008',
"description": 'E'
}, {
"title": 'E',
"lat": ' 26.6567',
"lng": '92.7717',
"description": 'E'
}
, {
"title": 'C',
"lat": '26.807',
"lng": '92.895',
"description": 'C'
},
];
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****************//
//Initialize the Direction Service
var service = new google.maps.DirectionsService();
//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];
service.route({
origin: src,
destination: des,
travelMode: google.maps.DirectionsTravelMode.DRIVING
}, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
//Initialize the Path Array
var path = new google.maps.MVCArray();
//Set the Path Stroke Color
var poly = new google.maps.Polyline({
map: map,
strokeColor: '#4986E7'
});
poly.setPath(path);
for (var i = 0, len = result.routes[0].overview_path.length; i < len; i++) {
path.push(result.routes[0].overview_path[i]);
}
}
});
}
}
}
&#13;
html,
body,
#dvMap {
height: 100%;
width: 100%;
}
&#13;
<div id="dvMap"></div>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js"></script>
&#13;