0

この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);
4

2 に答える 2