0

我正在使用两个标记来获取距离 A 和 B

Javascript:

var rendererOptions = {
    draggable: true
  };
    var Mapa = {
  // HTML Nodes
  mapContainer: document.getElementById('mapa'),
  dirContainer: document.getElementById('trasa'),
  fromInput: document.getElementById('from-input'),
  toInput: document.getElementById('to-input'),
  travelModeInput: document.getElementById('travel-mode-input'),
  unitInput: document.getElementById('unit-input'),

  // API Objects

  dirService: new google.maps.DirectionsService(rendererOptions),
  dirRenderer: new google.maps.DirectionsRenderer(rendererOptions),
  map: null,

  showDirections: function(dirResult, dirStatus) {
    if (dirStatus != google.maps.DirectionsStatus.OK) {
      alert('Não há resultados!');
      return;
    }

    // Show directions
    Mapa.dirRenderer.setMap(Mapa.map);
    Mapa.dirRenderer.setPanel(Mapa.dirContainer);
    Mapa.dirRenderer.setDirections(dirResult);
  },

  getSelectedTravelMode: function() {
    var value = Mapa.travelModeInput.options[Mapa.travelModeInput.selectedIndex].value
    if (value == 'driving') {
      value = google.maps.DirectionsTravelMode.DRIVING;
    } else {
      alert('Unsupported travel mode.');
    }
    return value;
  },

  getDirections: function() {
    var fromStr = Mapa.fromInput.value;
    var toStr = Mapa.toInput.value;
    var dirRequest = {
      origin: fromStr,
      destination: toStr,
      travelMode: Mapa.getSelectedTravelMode(),
      provideRouteAlternatives: true,

    };
    Mapa.dirService.route(dirRequest, Mapa.showDirections);
  },

  init: function() {
    var latLng = new google.maps.LatLng(38.6978, -27.1624);
    Mapa.map = new google.maps.Map(Mapa.mapContainer, {
      zoom: 15,
      center: latLng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    // Show directions onload
    Mapa.getDirections();
  }
}


// Onload handler to fire off the app.
google.maps.event.addDomListener(window, 'load', Mapa.init);

我只想要第二个标记,(B)是固定的,不要拖动。

有什么想法吗?

基于此:http ://webhost.home.pl/piotr/google_maps_test/index.html

4

2 回答 2

1

我看不到您在哪里创建标记,但它们具有可拖动的属性...

marker = new google.maps.Marker({
 map:map,
 draggable:true //Change to false for B
 });
于 2013-06-06T17:08:36.023 回答
1

一种选择是自己渲染方向,使一个标记可拖动,另一个不能。缺点是您无法拖动路线来更改它(除非您自己也实现了该功能;您可能可以在 API 中发布本机可拖动方向之前找到示例)。

工作示例 (起始位置为绿色)

另一种选择:使用可拖动的方向,但抑制标记。使用类似上面示例的标记:

var directionsDisplay = new google.maps.DirectionsRenderer({
      draggable:true, 
      suppressMarkers: true
    });

具有可拖动方向的工作示例

于 2013-06-06T21:09:03.387 回答