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

5

请查看 javascript 控制台。它指示此错误:

Uncaught ReferenceError: icons is not defined fiddle.jshell.net/Behseini/5UYHA/3/show/:113

您的图标数组对于初始化函数是本地的(就像您的地图变量一样)。

var directionsDisplay;
var start = document.getElementById('start').value;
var end = "1883 Walnut Cres, Coquitlam V3J 7T3";
var directionsService = new google.maps.DirectionsService();
var map = null;
 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 )
  )
 };



function initialize() {
  directionsDisplay = new google.maps.DirectionsRenderer({suppressMarkers: true});
     // Start/Finish icons
  var mapOptions = {
    zoom: 17,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    center: new google.maps.LatLng(49.258423,-122.841913)
  };
  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);

更新的小提琴

于 2013-10-16T17:13:21.470 回答
1

将标记的图标设置为您的源和目标。然后要删除由directionService 设置的添加标记,请将suppressMarkers 设置为true。

directionsDisplay.setOptions( { suppressMarkers: true } );
于 2018-02-01T16:06:27.250 回答