无法将自定义标记添加到方向服务的A点和B点

时间:2013-10-16 16:56:04

标签: google-maps google-maps-api-3 google-maps-markers

您能否看一下这个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);

2 个答案:

答案 0 :(得分: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);

updated fiddle

答案 1 :(得分:1)

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

directionsDisplay.setOptions( { suppressMarkers: true } );