<script type="text/javascript">
var markers = [
{
"title": 'Shah Alam',
"lat": '3.068621',
"lng": '101.518342',
}
,
{
"title": 'Ipoh',
"lat": '4.600050',
"lng": '101.075641',
}
,
{
"title": 'Putrajaya',
"lat": '2.908764',
"lng": '101.675053',
}
,
{
"title": 'Seremban',
"lat": '2.726553',
"lng": '101.934491',
}
,
{
"title": 'Ampang',
"lat": '3.151621',
"lng": '101.767996',
}
];
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>
我测试了实际产生两组线的代码(一条直线和一条驱动模式线)。我想删除直线并仅保留驱动模式线。我怎么能实现这一目标?当我注释掉一些线时,它只会保留直线,但从未成功保留驱动模式线。
答案 0 :(得分:0)
你做错了两件事。
最简单的解决方法是为每个方向结果创建单独的折线。
var markers = [{
"title": 'Shah Alam',
"lat": '3.068621',
"lng": '101.518342',
}, {
"title": 'Ipoh',
"lat": '4.600050',
"lng": '101.075641',
}, {
"title": 'Putrajaya',
"lat": '2.908764',
"lng": '101.675053',
}, {
"title": 'Seremban',
"lat": '2.726553',
"lng": '101.934491',
}, {
"title": 'Ampang',
"lat": '3.151621',
"lng": '101.767996',
}];
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 Direction Service
var service = new google.maps.DirectionsService();
// Create a new polyline, 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];
service.route({
origin: src,
destination: des,
travelMode: google.maps.DirectionsTravelMode.DRIVING
}, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
//Set the Path Stroke Color
var poly = new google.maps.Polyline({
map: map,
strokeColor: '#4986E7'
});
//Intialize the Path Array
var path = new google.maps.MVCArray();
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);
}
});
}
}
}
html,
body,
#dvMap {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
&#13;
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="dvMap"></div>
&#13;
.select-style {
border: 1px solid #ccc;
width: 120px;
border-radius: 3px;
overflow: hidden;
background: #fafafa url("img/icon-select.png") no-repeat 90% 50%;
}
.select-style select {
padding: 5px 8px;
width: 130%;
border: none;
box-shadow: none;
background: transparent;
background-image: none;
-webkit-appearance: none;
}
.select-style select:focus {
outline: none;
}
&#13;
答案 1 :(得分:0)
google.maps.DirectionsRenderer;
https://jsfiddle.net/7b4yj2ug/
EASY版本:
var markers = [{
"title": 'Shah Alam',
"lat": '3.068621',
"lng": '101.518342',
}, {
"title": 'Ipoh',
"lat": '4.600050',
"lng": '101.075641',
}, {
"title": 'Putrajaya',
"lat": '2.908764',
"lng": '101.675053',
}, {
"title": 'Seremban',
"lat": '2.726553',
"lng": '101.934491',
}, {
"title": 'Ampang',
"lat": '3.151621',
"lng": '101.767996',
}];
window.onload = function() {
var mapOptions = {
zoom: 10,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('dvMap'), mapOptions);
var waypts = [];
for (i = 0; i < markers.length; i++) {
var data = markers[i]
var myLatlng = new google.maps.LatLng(data.lat, data.lng);
waypts.push({
location: myLatlng,
});
}
//***********ROUTING****************//
//Intialize the Direction Service
var service = new google.maps.DirectionsService();
var directionsDisplay = new google.maps.DirectionsRenderer;
directionsDisplay.setMap(map);
//Draw Path Route between the Points on MAP
service.route({
origin: waypts[0],
destination: waypts[waypts.length - 1],
waypoints: waypts.slice(1, waypts.length - 1),
optimizeWaypoints: true,
travelMode: 'DRIVING'
}, function(result, status) {
if (status == google.maps.DirectionsStatus.OK)
directionsDisplay.setDirections(result);
});
}
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#dvMap,
html,
body {
height: 100%;
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="dvMap"></div>
注意! maximum allowed waypoints is 8 (API 3.26)
<强>航点强> 类型:数组
中间航点的数组。将通过此阵列中的每个航路点从起点到目的地计算方向。允许的最大航点为8,加上原点和目的地。 Premium Plan客户可以获得23个航点,以及起点和目的地。公交路线不支持航点。可选的。