Like us on Facebook and stand a chance to win pen drives!

Google Map Multiple WayPoints

Google Map Multiple WayPoints
Google Map Multiple WayPoints


<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
    <script type="text/javascript">

        var myRouter = {
            map_: null,
            directionsHelper_: null,

            stores: [
                    {name: "Thalawa Station", location: new google.maps.LatLng(8.231796, 80.348333)},
                    {name: "Maho Station", location: new google.maps.LatLng(7.8228, 80.2778)},
                    {name: "Mirigama Station", location: new google.maps.LatLng(7.242555, 80.126472)},
                    {name: "Veyangoda Station", location: new google.maps.LatLng(7.153015, 80.058488)},
                    {name: "Gampaha Station", location: new google.maps.LatLng(7.093543, 79.993703)}
                ],

            calcRoute: function() {

                var waypts = [];

                for (var i in this.stores) {
                    waypts.push({
                        location: this.stores[i].location,
                        stopover:true
                    });
                }
                var request = {
                    //origin: new google.maps.LatLng(8.344216, 80.410917),
					origin: "Anuradhapura Railway Station",
                    destination: "Colombo Fort Railway Station",
                    waypoints: waypts,
                    optimizeWaypoints: true,
                    travelMode: google.maps.DirectionsTravelMode.DRIVING
                };

                var _SELF = this;
                this.directionsHelper_.route(request, function(response, status) {
                    if (status == google.maps.DirectionsStatus.OK) {
                        _SELF.directionsDisplay_.setDirections(response);
                        return;
                    }
                    console.log('Directions Status: ' + status);
                });
            },

            init: function(mapid) {

                this.directionsHelper_ = new google.maps.DirectionsService();
                this.directionsDisplay_ = new google.maps.DirectionsRenderer();

                var center = new google.maps.LatLng(8.344216, 80.410917);
                var myOptions = {
                    zoom:7,
                    mapTypeId: google.maps.MapTypeId.ROADMAP,
                    center: center
                }
                this.map_ = new google.maps.Map(document.getElementById(mapid), myOptions);
                this.directionsDisplay_.setMap(this.map_);

                this.calcRoute();
            }
        };

        $(document).ready(function() {
            myRouter.init('map');
        });

    </script>
    <style type="text/css">
        #map {
            height: 500px;
            width: 600px;
            border: 1px solid #000;
        }
    </style>
</head>
<body>
    <div id="map"></div>
</body>
</html>


0 comments:

Copyright © 2012 The Code Junction.