我尝试使用Google Maps v3在 n 点(lat,lan)之间绘制路线。为此,我使用DirectionsService
为我提供路线,然后我将该坐标推入MVCArray
,然后使用Polyline
绘制该路径
Here is a jsfiddle demonstrating that part of code here
HTML:
<div id='map'></div>
CSS:
#map{
width:400px;
height:400px;
}
JavaScript的:
$(function () {
//The list of points to be connected
var markers = [
{
"title": 'Duero',
"lat": '40.480243',
"lng": '-3.866172',
"description": '1'
},
{
"title": 'Reyes Catolicos',
"lat": '40.477997',
"lng": '-3.870865',
"description": '2'
},
{
"title": 'Guadarrama',
"lat": '40.478998',
"lng": '-3.878755',
"description": '3'
}
];
var map;
var mapOptions = {
center: new google.maps.LatLng(markers[0].lat, markers[0].lng),
zoom: 15 ,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var path = new google.maps.MVCArray();
var service = new google.maps.DirectionsService();
var infoWindow = new google.maps.InfoWindow();
var map = new google.maps.Map(document.getElementById("map"), mapOptions);
var poly = new google.maps.Polyline({
map: map,
strokeColor: '#F3443C'
});
var lat_lng = new Array();
path.push(new google.maps.LatLng(markers[0].lat, markers[0].lng));
for (var i = 0; i < markers.length; i++) {
if ((i + 1) < markers.length) {
var src = new google.maps.LatLng(markers[i].lat, markers[i].lng);
var des = new google.maps.LatLng(markers[i+1].lat, markers[i+1].lng);
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]);
}
}
});
}
}
});
我应该绘制一组直线,用指定的宽度和颜色连接变量markers
中指定的点(lat,lan)。更具体地说,Point 1
已连接到 到Point 2
,后者又连接到Point 3
随着小提琴中的代码,我想看到,这:
我得到了上一张图片,但并非总是如此。如果你刷新,有几次你会得到这个,我相信这是因为添加一条额外的线连接点作为乌鸦苍蝇,但是,这是一个不一致的行为我&#39;我努力寻找它的来源,我简化了例子,你有一个尽可能小的顶点数组,据我所知,绘制的路径应该是相同的,因为数据是相同的。不可否认,我从未深入研究过google maps api,因此源代码主要是从在线样本中复制和粘贴的结果,特别是this one也遇到了同样的问题。
这意味着我可能正在使用过期版本的Google地图,错误地初始化或任何其他基本问题或有缺陷的设置。或者在另一方面,它可能是客户的问题,比如说Mac Chrome最新版本,因为显然我的手机上的Chrome for Android没有发生(Nexus 5,最新版,股票版)
答案 0 :(得分:5)
不要将目的地,来源和航点位置添加到折线,只需从路线服务点:
var map = null;
var infowindow = new google.maps.InfoWindow();
var bounds = new google.maps.LatLngBounds();
//The list of points to be connected
var markers = [{
"title": 'Duero',
"lat": '40.480243',
"lng": '-3.866172',
"description": '1'
}, {
"title": 'Reyes Catolicos',
"lat": '40.47806',
"lng": '-3.870937',
"description": '2'
}, {
"title": 'Guadarrama',
"lat": '40.478998',
"lng": '-3.878755',
"description": '3'
}];
// var map;
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(
parseFloat(markers[0].lat),
parseFloat(markers[0].lng)),
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var service = new google.maps.DirectionsService();
var infoWindow = new google.maps.InfoWindow();
map = new google.maps.Map(document.getElementById("map"), mapOptions);
var lat_lng = new Array();
var marker = new google.maps.Marker({
position: map.getCenter(),
map: map,
draggable: true
});
bounds.extend(marker.getPosition());
google.maps.event.addListener(marker, "click", function(evt) {
infowindow.setContent("coord:" + marker.getPosition().toUrlValue(6));
infowindow.open(map, marker);
});
for (var i = 0; i < markers.length; i++) {
if ((i + 1) < markers.length) {
var src = new google.maps.LatLng(parseFloat(markers[i].lat),
parseFloat(markers[i].lng));
createMarker(src);
var des = new google.maps.LatLng(parseFloat(markers[i + 1].lat),
parseFloat(markers[i + 1].lng));
createMarker(des);
// poly.setPath(path);
service.route({
origin: src,
destination: des,
travelMode: google.maps.DirectionsTravelMode.DRIVING
}, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
var path = new google.maps.MVCArray();
var poly = new google.maps.Polyline({
map: map,
strokeColor: '#F3443C'
});
for (var i = 0, len = result.routes[0].overview_path.length; i < len; i++) {
path.push(result.routes[0].overview_path[i]);
}
poly.setPath(path);
map.fitBounds(bounds);
}
});
}
}
}
function createMarker(latLng) {
var marker = new google.maps.Marker({
position: latLng,
map: map,
draggable: true
});
bounds.extend(marker.getPosition());
google.maps.event.addListener(marker, "click", function(evt) {
infowindow.setContent("coord:" + this.getPosition().toUrlValue(6));
infowindow.open(map, this);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
&#13;
html,
body,
#map {
width: 100%;
height: 100%;
}
&#13;
<script src="http://maps.googleapis.com/maps/api/js"></script>
<div id='map'></div>
&#13;