开发者

How to plot postcodes along with driving route exactly in the given order in a map using google api javascript embed code?

I need to plot a collection of postcodes along with the driving direction. The postcodes will be sorted in the ascending order of some crit开发者_JAVA技巧eria. So the first postcode in the sorted array should be plotted as A, Second postcode as B,......last postcode as the corresponding next alphabet (as shown in the get direction section of maps.google.com) along with the driving route.

Can any one help me out to do this using javascript embed code of Google api V3 to perform this functionality..


Finally i could do this. The code is given below,

     <!DOCTYPE html>
     <html>
     <head>
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
        <title>Google Maps JavaScript API v3 Example: Directions Waypoints</title>
        <link href="http://code.google.com/apis/maps/documentation/javascript/examples/standard.css" rel="stylesheet" type="text/css" />
        <script src="https://maps-api-ssl.google.com/maps/api/js?v=3&sensor=false"  type="text/javascript"></script>
        <script type="text/javascript">
           function initialize() {

                var directionDisplay;
                var directionsService = new google.maps.DirectionsService();

                var map;
                directionsDisplay = new google.maps.DirectionsRenderer();

                var chicago = new google.maps.LatLng(41.850033, -87.6500523);
                var myOptions = {
                zoom: 6,
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                center: chicago }

                var waypts = [];
                var PtsArray = [];

                //Give input as postcode/place name as a single string seperated by pipeline character as follows. 
                var PointsString = "LE126UW" + "|" + "NG104AH" + "|" + "B112RJ" + "|" + "London";            


                PtsArray = PointsString.split("|");

                var length = PtsArray.length;
                if (length > 2) {
                for (i = 1; i < (length - 1); i++) {
                   waypts.push({
                       location: String(PtsArray[i]),
                       stopover: true
                    });
                 }
               }

             var request = {
               origin: String(PtsArray[0]),
               destination: String(PtsArray[length - 1]),
               waypoints: waypts,
               optimizeWaypoints: false,//set this to true to get optimized path else it will plot as the given input.
               travelMode: google.maps.DirectionsTravelMode.DRIVING//set your travel mode here (walking,driving..)
              };


               directionsService.route(request, function (response, status) {

                  if (status == google.maps.DirectionsStatus.OK) {
                      map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
                      directionsDisplay.setMap(map);
                      directionsDisplay.setDirections(response);
                      var route = response.routes[0];
                      var total = 0;
                      var numberLegs = route.legs.length;
                   }
                   else {
                       alert("Could not load data.." + status);
                   }
             });
          }       

     </script>
   </head>
  <body onload="initialize()">
      <div id="map_canvas" style="float: left; width: 100%; height: 480px;">
  </div>
   <br />
   <br />
  </body>
   </html>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜