如何用多色多段线绘制谷歌地图航点 [英] how to draw a google maps waypoint with multi-colored polylines
本文介绍了如何用多色多段线绘制谷歌地图航点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我尝试在Google地图上绘制带有路线指示的多段线。
我尝试了类似的东西。
函数initMap(){
var directionsService = new google.maps.DirectionsService ;
var directionsDisplay = new google.maps.DirectionsRenderer;
var map = new google.maps.Map(document.getElementById('map'),{
zoom:6,
center:{lat:41.85,lng:-87.65}
});
directionsDisplay.setMap(map);
calculateAndDisplayRoute(directionsService,directionsDisplay);
}
函数calculateAndDisplayRoute(directionsService,directionsDisplay){
var waypts = [
{
location:'41 .062317,28.899756',
中途停留:真实
},
{
位置:'41 .062276,28.898866',
中途停留:真实
},
{
位置: '41 .061993,28.8982',
中途停留:true
}
];
directionsService.route({
origin:{lat:41.063328,lng:28.901215},
destination:{lat:41.060756,lng:28.900046},
waypoints:waypts,
optimizeWaypoints:true,
travelMode:google.maps.TravelMode.DRIVING
},function(response,status){
if(status === google.maps.DirectionsStatus.OK ){
directionsDisplay.setOptions({
directions:response,
})
drawpolylines(directionsDisplay.getMap())
var route = response.routes [0] ;
} else {
window.alert('由于'+状态导致请求失败);
}
});
$ b函数drawpolylines(map){
var flightPlanCoordinates = [
{lat:41.062317,lng:28.899756},
{ lat:41.062276,lng:28.898866},
];
var flightPath = new google.maps.Polyline({
path:flightPlanCoordinates,
geodesic:true,
strokeColor:'#FF0000',
strokeOpacity:1.0,
strokeWeight:2
});
flightPath.setMap(map);
解决方案
您需要创建单独的多段线为每个彩色片段。以下示例使用此相关问题中 renderDirectionsPolylines
的修改版本:
程式码片段:
$ b
var map; var infowindow = new google.maps.InfoWindow(); function initMap(){var directionsService = new谷歌地图。为DirectionsService; var directionsDisplay = new google.maps.DirectionsRenderer({suppressPolylines:true,infoWindow:infowindow}); map = new google.maps.Map(document.getElementById('map'),{zoom:6,center:{lat:41.85,lng:-87.65}}); directionsDisplay.setMap(地图); calculateAndDisplayRoute(directionsService,directionsDisplay);}函数calculateAndDisplayRoute(directionsService,directionsDisplay){var waypts = [{location:'41 .062317,28.899756',stopover:true},{location:'41 .062276,28.898866',stopover:true},{location :'41 .061993,28.8982',stopover:true}]; directionsService.route({origin:{lat:41.063328,lng:28.901215},destination:{lat:41.060756,lng:28.900046},waypoints:waypts,optimizeWaypoints:true,travelMode:google.maps.TravelMode.DRIVING},function响应,状态){if(status === google.maps.DirectionsStatus.OK){directionsDisplay.setOptions({directions:response,})var route = response.routes [0]; renderDirectionsPolylines(response,map);} else {window.alert('Directions requests failed to'+ status');}});} google.maps.event.addDomListener(window,load,initMap); var polylineOptions = {strokeColor:'#C83939',strokeOpacity :1,strokeWeight:4}; var colors = [#FF0000,#00FF00,#0000FF,#FFFF00,#FF00FF,#00FFFF]; var polylines = []; function renderDirectionsPolylines(response){var bounds = new google.maps.LatLngBounds(); for(var i = 0; i< polylines.length; i ++){polylines [i] .setMap(null); } var legs = response.routes [0] .legs; for(i = 0; i< legs.length; i ++){var steps = legs [i] .steps; for(j = 0; j
html,body,#map {height:100%;宽度:100%; margin:0px; < script src =https:// //maps.googleapis.com/maps/api/js\"></script><div id =map>< / div>
Hi I try to draw polylines with directions waypoints on google maps .
I tried something like that. My draw I want to draw the routes with different colors like this.
Google maps directions example ss
I wrote this sample code .
function initMap() {
var directionsService = new google.maps.DirectionsService;
var directionsDisplay = new google.maps.DirectionsRenderer;
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 6,
center: {lat: 41.85, lng: -87.65}
});
directionsDisplay.setMap(map);
calculateAndDisplayRoute(directionsService, directionsDisplay);
}
function calculateAndDisplayRoute(directionsService, directionsDisplay) {
var waypts = [
{
location: '41.062317, 28.899756',
stopover: true
},
{
location: '41.062276, 28.898866',
stopover: true
},
{
location: '41.061993, 28.8982',
stopover: true
}
];
directionsService.route({
origin: {lat: 41.063328, lng:28.901215},
destination:{lat: 41.060756, lng:28.900046},
waypoints: waypts,
optimizeWaypoints: true,
travelMode: google.maps.TravelMode.DRIVING
}, function(response, status) {
if (status === google.maps.DirectionsStatus.OK) {
directionsDisplay.setOptions({
directions :response,
})
drawpolylines(directionsDisplay.getMap())
var route = response.routes[0];
} else {
window.alert('Directions request failed due to ' + status);
}
});
}
function drawpolylines(map) {
var flightPlanCoordinates = [
{lat: 41.062317, lng: 28.899756},
{lat: 41.062276, lng: 28.898866},
];
var flightPath = new google.maps.Polyline({
path: flightPlanCoordinates,
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2
});
flightPath.setMap(map);
}
解决方案
You need to create separate polylines for each colored segment. Example below using a modified version of the renderDirectionsPolylines
from this related question: Google Maps click event on route (modified to use an array of colors, applying each color to the step in the route of the number).
code snippet:
var map;
var infowindow = new google.maps.InfoWindow();
function initMap() {
var directionsService = new google.maps.DirectionsService;
var directionsDisplay = new google.maps.DirectionsRenderer({
suppressPolylines: true,
infoWindow: infowindow
});
map = new google.maps.Map(document.getElementById('map'), {
zoom: 6,
center: {
lat: 41.85,
lng: -87.65
}
});
directionsDisplay.setMap(map);
calculateAndDisplayRoute(directionsService, directionsDisplay);
}
function calculateAndDisplayRoute(directionsService, directionsDisplay) {
var waypts = [{
location: '41.062317, 28.899756',
stopover: true
}, {
location: '41.062276, 28.898866',
stopover: true
}, {
location: '41.061993, 28.8982',
stopover: true
}];
directionsService.route({
origin: {
lat: 41.063328,
lng: 28.901215
},
destination: {
lat: 41.060756,
lng: 28.900046
},
waypoints: waypts,
optimizeWaypoints: true,
travelMode: google.maps.TravelMode.DRIVING
}, function(response, status) {
if (status === google.maps.DirectionsStatus.OK) {
directionsDisplay.setOptions({
directions: response,
})
var route = response.routes[0];
renderDirectionsPolylines(response, map);
} else {
window.alert('Directions request failed due to ' + status);
}
});
}
google.maps.event.addDomListener(window, "load", initMap);
var polylineOptions = {
strokeColor: '#C83939',
strokeOpacity: 1,
strokeWeight: 4
};
var colors = ["#FF0000", "#00FF00", "#0000FF", "#FFFF00", "#FF00FF", "#00FFFF"];
var polylines = [];
function renderDirectionsPolylines(response) {
var bounds = new google.maps.LatLngBounds();
for (var i = 0; i < polylines.length; i++) {
polylines[i].setMap(null);
}
var legs = response.routes[0].legs;
for (i = 0; i < legs.length; i++) {
var steps = legs[i].steps;
for (j = 0; j < steps.length; j++) {
var nextSegment = steps[j].path;
var stepPolyline = new google.maps.Polyline(polylineOptions);
stepPolyline.setOptions({
strokeColor: colors[i]
})
for (k = 0; k < nextSegment.length; k++) {
stepPolyline.getPath().push(nextSegment[k]);
bounds.extend(nextSegment[k]);
}
polylines.push(stepPolyline);
stepPolyline.setMap(map);
// route click listeners, different one on each step
google.maps.event.addListener(stepPolyline, 'click', function(evt) {
infowindow.setContent("you clicked on the route<br>" + evt.latLng.toUrlValue(6));
infowindow.setPosition(evt.latLng);
infowindow.open(map);
})
}
}
map.fitBounds(bounds);
}
html,
body,
#map {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map"></div>
这篇关于如何用多色多段线绘制谷歌地图航点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文