【发布时间】:2015-03-10 12:15:48
【问题描述】:
我在航点方向谷歌地图上工作。我在路线的起点和终点之间放置了相同的位置。机器人谷歌地图只显示一个指针指向相同的两个位置。
我需要两个指针指向谷歌地图航点中的相同两个位置。
我的代码:
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Waypoints in directions</title>
<style>
html, body, #map-canvas {
height: 100%;
margin: 0px;
padding: 0px
}
#panel {
position: absolute;
top: 5px;
left: 50%;
margin-left: -180px;
z-index: 5;
background-color: #fff;
padding: 5px;
border: 1px solid #999;
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>
<script>
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
function initialize() {
directionsDisplay = new google.maps.DirectionsRenderer();
var chicago = new google.maps.LatLng(41.850033, -87.6500523);
var mapOptions = {
zoom: 6,
center: chicago
}
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
directionsDisplay.setMap(map);
}
function calcRoute() {
var start = document.getElementById('start').value;
var end = document.getElementById('end').value;
var waypts = [];
var checkboxArray = document.getElementById('waypoints');
for (var i = 0; i < checkboxArray.length; i++) {
if (checkboxArray.options[i].selected == true) {
waypts.push({
location:checkboxArray[i].value,
stopover:true});
}
}
var request = {
origin: start,
destination: end,
waypoints: waypts,
optimizeWaypoints: true,
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
var route = response.routes[0];
var summaryPanel = document.getElementById('directions_panel');
summaryPanel.innerHTML = '';
// For each route, display summary information.
console.log(route.legs);
for (var i = 0; i < route.legs.length; i++) {
var routeSegment = i + 1;
summaryPanel.innerHTML += '<b>Route Segment: ' + routeSegment + '</b><br>';
summaryPanel.innerHTML += route.legs[i].start_address + ' to ';
summaryPanel.innerHTML += route.legs[i].end_address + '<br>';
summaryPanel.innerHTML += route.legs[i].distance.text + '<br><br>';
}
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body onload="initialize()">
<div id="map-canvas" style="float:left;width:70%;height:100%;"></div>
<div id="control_panel" style="float:right;width:30%;text-align:left;padding-top:20px">
<div style="margin:20px;border-width:2px;">
<b>Start:</b>
<select id="start">
<option value="Allen Street, NY">Allen Street</option>
<option value="Ann Street, NY">Ann Street</option>
<option value="Barrow Street, NY">Barrow Street</option>
<option value="Bayard Street, NY">Bayard Street</option>
<option value="Beach Street, NY">Beach Street</option>
<option value="Beak Street, NY">Beak Street</option>
<option value="Bethune Street, NY">Bethune Street</option>
</select>
<br>
<b>Waypoints:</b> <br>
<i>(Ctrl-Click for multiple selection)</i> <br>
<select multiple id="waypoints">
<option value="Allen Street, NY">Allen Street</option>
<option value="Allen Street, NY">Allen Street</option>
<option value="Barrow Street, NY">Barrow Street</option>
<option value="Bayard Street, NY">Bayard Street</option>
<option value="Beach Street, NY">Beach Street</option>
<option value="Beak Street, NY">Beak Street</option>
<option value="Bethune Street, NY">Bethune Street</option>
</select>
<br>
<b>End:</b>
<select id="end">
<option value="Bethune Street, NY">Bethune Street</option>
<option value="Beak Street, NY">Beak Street</option>
<option value="Beach Street, NY">Beach Street</option>
<option value="Bayard Street, NY">Bayard Street</option>
<option value="Allen Street, NY">Allen Street</option>
<option value="Ann Street, NY">Ann Street</option>
<option value="Barrow Street, NY">Barrow Street</option>
</select>
<br>
<input type="submit" onclick="calcRoute();">
</div>
<div id="directions_panel" style="margin:20px;background-color:#FFEE77;"></div>
</div>
</body>
</html>
我得到以下结果:
这里我设置在相同的两个位置“艾伦街”之间。但它返回
- A->起点
- C->艾伦街,艾伦街
- D->目标点
但我需要以下几点
- A->起点
- B->艾伦街
- c->艾伦街
- D->目标点
【问题讨论】:
-
为“B”制作一个自定义标记,输入一些指示(可能是数字 2)那里有 2 个学生(/markers)。
标签: javascript google-maps google-maps-api-3 google-maps-markers