Google Maps API可以构建从源到目的地的路线。在以下Google的示例中,每个步骤都发布到HTML代码中:http://code.google.com/apis/maps/documentation/examples/directions-simple.html

我想获取此方向每一步的地址解析,并将它们存储在数组中。我相信这是有可能的,但是我不知道如何处理。

非常感谢您的回答。
问候

最佳答案

是的,您可以很容易地从GDirections中获取各个步骤。

首先,必须确保在调用getSteps: true方法时传递GDirections.load()选项。然后,您可以简单地遍历GDirections.getRoute(i).getStep(j),如以下示例所示:

<!DOCTYPE html>
<html>
<head>
   <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
   <title>Google Maps Simple Directions Demo</title>
   <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false"
              type="text/javascript"></script>
</head>
<body onunload="GUnload()">
   <div id="map" style="width: 550px; height: 400px"></div>

   <script type="text/javascript">
      var map = new GMap2(document.getElementById("map"));
      var directions = new GDirections(map);

      directions.load('from: London, UK to: Glasgow, UK', { getSteps: true });

      GEvent.addListener(directions, "load", function() {
         if (directions.getNumRoutes() > 0) {
            for (var i = 0; i < directions.getRoute(0).getNumSteps(); i++) {
               directions.getRoute(0).getStep(i).getLatLng().lat();
               directions.getRoute(0).getStep(i).getLatLng().lng();
               directions.getRoute(0).getStep(i).getDescriptionHtml();
               directions.getRoute(0).getStep(i).getPolylineIndex();
               directions.getRoute(0).getStep(i).getDistance().meters;
               directions.getRoute(0).getStep(i).getDuration().seconds;
            }
         }
      });
   </script>
</body>
</html>


进一步阅读和参考:


GDirections
GRoute
GStep

09-26 19:26