0
我创建了一个代码,可以在地图上绘制绘图方向,但使用多段线,因为使用航点我可以看到八个点。谷歌地图方向
首先我有一个函数nArray()
返回我想在这个控制台中显示的一个值设置为一个点
Array[5]
0: Object
1: Object
DISTANCE_FROM_PREVIOUS_OBJECT_LOCATION: 2.087970147789207
lat: "48.866588"
leftPosition: 183
lng: "2.309037999999987"
topPosition: 57
__proto__: Object
2: Object
3: Object
4: Object
length: 5
__proto__: Array[0]
现在我想对所有对象(纬度,经度):
<!DOCTYPE html>
<html>
<head>
<script src="http://maps.google.com/maps/api/js?sensor=false&libraries=places" type="text/javascript"></script>
<script>
var map = null;
var directionService = null;
var directionsRenderer = null;
var bigArray = nArray();
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(35.75936182169444, -90.35907120698232),
mapTypeId: google.maps.MapTypeId.ROADMAP,
zoom: 4
};
map = new google.maps.Map(document.getElementById("map"), mapOptions);
directionService = new google.maps.DirectionsService();
directionsRenderer = new google.maps.DirectionsRenderer({ map: map });
}
function drawDirections(myPath) {
var myLine = new google.maps.Polyline({
path: myPath,
strokeColor: '#ff0000',
strokeOpacity: 1.0,
strokeWeight: 2
});
myLine.setMap(map);
}
function drawRoutes() {
var i;
var npts = bigArray.length - 1;
for (i = 0; i < npts; i++) {
var marker = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(bigArray[i].lat, bigArray[i].lng)
});
var request = {
origin: new google.maps.LatLng(bigArray[i].lat, bigArray[i].lng),
destination: new google.maps.LatLng(bigArray[i + 1].lat, bigArray[i + 1].lng),
travelMode: google.maps.DirectionsTravelMode.DRIVING
}
directionService.route(request, function(result, status) {
//route = result.routes[0];
if (status == google.maps.DirectionsStatus.OK) {
drawDirections(result.routes[0].overview_path);
}
});
}
var marker = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(bigArray[i].lat, bigArray[i].lng)
});
}
</script>
</head>
<body onload="initialize();">
<div id="map" style="width: 500px; height: 400px;"></div>
<input type="button" onclick="drawRoutes()" value="Draw Routes" />
</body>
</html>
如何从nArray
获取每个对象的经度和纬度?
那么,你有什么建议?使用航点? –
这将是最好的。但是,如果您需要8个以上的航点,则无法使用(没有商业许可证),可能的选项有:在请求之间设置一个延迟(以便每秒不会达到极限),当数组包含10个以上点(否则你不需要解决方法,航点将工作),将数组拆分为10个位置,并请求这些点的路线...(例如,如果有15个位置,则需要2个请求,而不是14你的当前策略) –
好吧,但是有一些例子,你能告诉我一个jsfiddle吗? –