您能否看看这个jsfiddle链接,让我知道为什么我无法将自定义标记添加到方向服务的开始 (A) 和结束 (B) 点?
如您所见,方向服务正常工作,但是我无法将标记添加到地图!
这是我的代码:
var directionsDisplay;
var start = document.getElementById('start').value;
var end = "1883 Walnut Cres, Coquitlam V3J 7T3";
var directionsService = new
google.maps.DirectionsService();
function initialize() {
directionsDisplay = new google.maps.DirectionsRenderer({suppressMarkers: true});
// Start/Finish icons
var icons = {
start: new google.maps.MarkerImage(
// URL
'https://maps.google.com/mapfiles/kml/shapes/schools_maps.png',
// (width,height)
new google.maps.Size( 44, 32 ),
// The origin point (x,y)
new google.maps.Point( 0, 0 ),
// The anchor point (x,y)
new google.maps.Point( 22, 32 )
),
end: new google.maps.MarkerImage(
// URL
'https://maps.google.com/mapfiles/kml/shapes/schools_maps.png',
// (width,height)
new google.maps.Size( 44, 32 ),
// The origin point (x,y)
new google.maps.Point( 0, 0 ),
// The anchor point (x,y)
new google.maps.Point( 22, 32 )
)
};
var mapOptions = {
zoom: 17,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: new google.maps.LatLng(49.258423,-122.841913)
};
var map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById('directions-panel'));
var control = document.getElementById('control');
control.style.display = 'block';
map.controls[google.maps.ControlPosition.TOP_CENTER].push(control);
}
function calcRoute() {
var request = {
origin: start,
destination: end,
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
var leg = response.routes[ 0 ].legs[ 0 ];
makeMarker( leg.start_location, icons.start, "title" );
makeMarker( leg.end_location, icons.end, 'title' );
}
});
}
function makeMarker( position, icon, title ) {
new google.maps.Marker({
position: position,
map: map,
icon: icon,
title: title
});
}
google.maps.event.addDomListener(window, 'load', initialize);