如何在javascript google map API中显示多个坐标之间的路径
How to display a path between multiple co-ordinates in javascript google map API
我想在来自数组和方向服务的多个坐标之间创建一条路径,我想渲染这些坐标之间的路径。如果您在此处查看坐标数组:
var requestResource = [{"origin": {"lat": 26.4499, "lng": 80.3319},
"destination": {"lat": 28.6139, "lng": 77.2090}}, {"origin": {"lat": 28.6139,
"lng": 77.2090},"destination": {"lat": 28.9845, "lng": 77.7064}}];
我正在尝试使用以下代码生成代码,但它只给了我最后一组起点和目的地之间的路径。
var directionsService = new google.maps.DirectionsService();
var indiaLatlng = {lat: 28.6139, lng : 77.2090};
map = new google.maps.Map(document.getElementById("map"), {
center : indiaLatlng,
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var rendererOptions = {
preserveViewport: true,
suppressMarkers:true
};
var requestResource = [{"origin": {"lat": 26.4499, "lng": 80.3319}, "destination": {"lat": 28.6139, "lng": 77.2090}}, {"origin": {"lat": 28.6139, "lng": 77.2090},"destination": {"lat": 28.9845, "lng": 77.7064}}];
for(var r in requestResource) {
var resource = requestResource[r];
var mapRequest = {
origin : resource["origin"],
destination : resource["destination"],
travelMode: google.maps.TravelMode.DRIVING
}
var directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);
directionsDisplay.setMap(map);
directionsService.route(mapRequest, function(result, status) {
console.log(result);
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(result);
}
});
}
代码中只有一个google.maps.DirectionsRenderer
对象。 每次设置方向时,它都会覆盖之前的显示。 为要显示的每条路线创建新的方向渲染器。
概念验证小提琴
代码片段:
var map;
function initialize() {
var directionsService = new google.maps.DirectionsService();
var indiaLatlng = {
lat: 28.6139,
lng: 77.2090
};
map = new google.maps.Map(document.getElementById("map"), {
center: indiaLatlng,
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var rendererOptions = {
preserveViewport: true,
suppressMarkers: true
};
var requestResource = [{
"origin": {
"lat": 26.4499,
"lng": 80.3319
},
"destination": {
"lat": 28.6139,
"lng": 77.2090
}
}, {
"origin": {
"lat": 28.6139,
"lng": 77.2090
},
"destination": {
"lat": 28.9845,
"lng": 77.7064
}
}];
var bounds = new google.maps.LatLngBounds();
for (var r in requestResource) {
var resource = requestResource[r];
var mapRequest = {
origin: resource["origin"],
destination: resource["destination"],
travelMode: google.maps.TravelMode.DRIVING
}
bounds.extend(new google.maps.LatLng(resource["origin"].lat, resource["origin"].lng));
bounds.extend(new google.maps.LatLng(resource["destination"].lat, resource["destination"].lng));
map.fitBounds(bounds);
directionsService.route(mapRequest, function(result, status) {
console.log(result);
if (status == google.maps.DirectionsStatus.OK) {
var directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);
directionsDisplay.setMap(map);
directionsDisplay.setDirections(result);
} else alert("directions request failed, status:" + status)
});
}
}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry,places&ext=.js"></script>
<div id="map"></div>
相关文章:
- JS:给定一个x,y坐标列表,可以找到介于1,1和给定最大值之间的缺失坐标
- 检查坐标存在于直线的两个点之间
- 诺基亚HERE地图:计算两个坐标之间的位移
- 计算两个GPS坐标之间的指南针方位的问题
- “我当前的地理位置”和其他坐标之间的计算
- 为坐标之间的区域添加背景色
- 坐标之间的音差地理位置距离
- javascript中两个画布坐标之间的距离
- Highcharts-悬停时x坐标之间的不同填充颜色
- 根据百分比计算两个坐标之间的点
- AngularJS获取多个坐标之间的距离
- 使用数组计算坐标之间的距离,并设置Y轴和比例- D3.js
- js如何找到上次点击和鼠标当前位置的坐标之间的度
- 点与直线坐标之间的运动学关系
- 如何在驱动模式下使用javascript查找两个坐标之间的距离
- 从城市表中获取2个gps坐标之间的路线点
- 如何在 Mapbox.js 中的两个坐标之间沿直线移动标记
- 查找 LatLng 坐标是否位于另外两个 LatLng 坐标之间
- 如何在javascript google map API中显示多个坐标之间的路径
- 计算距离一定米的两个坐标之间的经纬度