【问题标题】:How to add multiple (separated with a plus symbol) waypoints to a map如何向地图添加多个(用加号分隔)航点
【发布时间】:2012-10-09 11:47:22
【问题描述】:

感谢geocodezip ,我成功地创建了一个包含方向功能和加载 XML 标记的地图。但是还有一个问题:

如何添加航点?它们应该用加号或其他适当的符号分隔。

这是当前代码:

<script type="text/javascript"> 
//<![CDATA[
 // global "map" variable
var map = null;
var rendererOptions = {
draggable: true
};
var directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);;
var directionsService = new google.maps.DirectionsService();
// A function to create the marker and set up the event window function 
function createMarker(latlng, name, html) {
var marker = new google.maps.Marker({
position: latlng,
map: map,
zIndex: Math.round(latlng.lat()*-100000)<<5
});
var contentString = html;
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(contentString); 
infowindow.open(map,marker);
});
}
// ===== request the directions =====
function getDirections() {
// ==== Set up the walk and avoid highways options ====
var request = {};
if (document.getElementById("walk").checked) {
request.travelMode = google.maps.DirectionsTravelMode.WALKING;
} else {
request.travelMode = google.maps.DirectionsTravelMode.DRIVING;
}
if (document.getElementById("highways").checked) {
request.avoidHighways = true;
}   
if (document.getElementById("alternatives").checked) {
request.provideRouteAlternatives = true;
}   
// ==== set the start and end locations ====
var saddr = document.getElementById("saddr").value
var daddr = document.getElementById("daddr").value
request.origin = saddr;
request.destination = daddr;
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}
function initialize() {
// create the map
var myOptions = {
zoom: 8,
center: new google.maps.LatLng(43.907787,-79.359741),
mapTypeControl: true,
mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
navigationControl: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById("directionsPanel"));
google.maps.event.addListener(map, 'click', function() {
infowindow.close();
});
// Read the data from example.xml   
if (document.getElementById("showmarkers").checked) {
downloadUrl("example.xml", function(doc) {
var xmlDoc = xmlParse(doc);
var markers = xmlDoc.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
// obtain the attribues of each marker
var lat = parseFloat(markers[i].getAttribute("lat"));
var lng = parseFloat(markers[i].getAttribute("lng"));
var point = new google.maps.LatLng(lat,lng);
var html = markers[i].getAttribute("html");
var label = markers[i].getAttribute("label");
// create the marker
var marker = createMarker(point,label,html);
}
});
}
// Read the data from example2.xml   
if (document.getElementById("showmarkers2").checked) {
downloadUrl("example2.xml", function(doc) {
var xmlDoc = xmlParse(doc);
var markers = xmlDoc.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
// obtain the attribues of each marker
var lat = parseFloat(markers[i].getAttribute("lat"));
var lng = parseFloat(markers[i].getAttribute("lng"));
var point = new google.maps.LatLng(lat,lng);
var html = markers[i].getAttribute("html");
var label = markers[i].getAttribute("label");
// create the marker
var marker = createMarker(point,label,html);
}
});
}
}
var infowindow = new google.maps.InfoWindow(
{ 
size: new google.maps.Size(150,50)
});  
//]]>
</script>

这是我用来生成航点的代码,但现在不起作用:

var via = document.getElementById("via").value;
  if (via) {
      so = via.split("+")
      if (so.length > 1) {
          var wpArray = [];
          for (i=0; (i<so.length); i++) {
            wpArray.push({location: so[i], stopover:true})
          }
          request = {
            origin:saddr, 
            destination:daddr,
            waypoints: wpArray,
            provideRouteAlternatives: true,
            travelMode: google.maps.DirectionsTravelMode.DRIVING
          }
      } else {
          request = {
            origin:saddr, 
            destination:daddr,
            waypoints:[{location:via, stopover:true}],
            provideRouteAlternatives: true,
            travelMode: google.maps.DirectionsTravelMode.DRIVING
          }
      }
  } else {
      request = {
        origin:saddr, 
        destination:daddr,
        provideRouteAlternatives: true,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
      }
  }

编辑这是工作功能:

function getDirections() {
        // ==== Set up the walk and avoid highways options ====
        var request = {};
        if (document.getElementById("walk").checked) {
           request.travelMode = google.maps.DirectionsTravelMode.WALKING;
        } else {
          request.travelMode = google.maps.DirectionsTravelMode.DRIVING;
        }

        if (document.getElementById("highways").checked) {
           request.avoidHighways = true;
        }

        if (document.getElementById("alternatives").checked) {
           request.provideRouteAlternatives = true;
        }


        // ==== set the start and end locations ====
        var start = document.getElementById("start").value
        var end = document.getElementById("end").value

        request.origin = start;
        request.destination = end;
        request.waypoints = via;

var so;
var via = document.getElementById("via").value;



if (via) {
      so = via.split("+")
      if (so.length > 1) {
          var wpArray = [];
          for (i=0; (i<so.length); i++) {
            wpArray.push({location: so[i], stopover:false})
          }
          request = {
            origin:start, 
            destination:end,
            waypoints: wpArray,
            provideRouteAlternatives: true,
            avoidHighways: document.getElementById("highways").checked,
            travelMode: google.maps.DirectionsTravelMode.DRIVING
          }
      } else {
          request = {
            origin:start, 
            destination:end,
            waypoints:[{location:via, stopover:false}],
            provideRouteAlternatives: true,
            avoidHighways: document.getElementById("highways").checked,
            travelMode: google.maps.DirectionsTravelMode.DRIVING
          }
      }
  } else {
      request = {
        origin:start, 
        destination:end,
        provideRouteAlternatives: true,
        avoidHighways: document.getElementById("highways").checked,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
      }
  }

    directionsService.route(request, function(response, status) {
      if (status == google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);
      }
    });
  }

【问题讨论】:

  • 1.你有什么尝试添加航点? 2. “用加号或适当的符号分隔”是什么意思?在哪里? 3. 你真的应该一次只问一个问题。
  • @geocodezip 1. 在我使用问题编辑中添加的代码之前。但是,它现在不适用于新代码。 2. 在航点文本框中输入的文本看起来像 e。 G。像这样的“巴黎+阿姆斯特丹”,意味着它应该计算通过两个城市(有时超过两个)的路线。 3. 我为标记开了一个新问题:) stackoverflow.com/questions/12802617/…
  • 请贴出更完整的代码和具体的错误信息。 “不工作”不够详细。
  • @geocodezip 没有错误消息。它根本不起作用,当我单击提交按钮时,没有任何事情发生,无论是从位置还是到位置,也没有中途停留。我会尝试更多的尝试,然后发布整个代码。
  • 您需要包含足够的信息,以便我们可以重现问题、示例 xml 数据以及您正在测试的示例航点。

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


【解决方案1】:

不知道为什么你有 2 个 xml 文件或者它们的格式是什么。这是一个根据类别显示/隐藏标记的示例(再次翻译自 Mike Williams 的 v2 教程):

http://www.geocodezip.com/v3_MW_example_categories.html

(也复制到How to show/hide markers from XML file without refreshing the page,并附上它适用的部分问题)

【讨论】:

  • 问题的第二部分现在在这里:stackoverflow.com/questions/12802617/…
  • 我无法以编程方式仅在一个 xml 文件中创建所有项目,因为用于地图的项目存在很大差异。如果您有数百个经常更改的项目,我将不得不手动插入每个项目,这是一个大问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-14
  • 2017-12-22
  • 2021-12-28
  • 2018-04-16
  • 2014-02-19
相关资源
最近更新 更多