Web Programming examples

Google Maps,AngularJS

Google Maps for AngularJS Directions Service WayPoints(v2.0.7)

  • AngularJS : 1.2.23
  • Bootstrap : 3.2.0
  • Google Maps for AngularJS : 2.0.7
  • include file
  • angular.min.js, lodash.underscore.min.js, angular-google-maps.min.js, ui-bootstrap-tpls-0.11.0.min.js
(Ctrl-Click for multiple selection))
<script type='text/javascript' src='http://www.example.com/wp/wp-content/themes/ang/js/lodash.underscore.min.js'></script>
<script type='text/javascript' src='http://www.example.com/wp/wp-content/themes/ang/js/angular-google-maps.min.js'></script>
<script type='text/javascript' src='http://www.example.com/wp/wp-content/themes/ang/js/app.js'></script>
<div ng-controller="MyCtrl">
  <div class="row">
    <div class="col-sm-6">
      <ui-gmap-google-map 
        id="map-canvas"
        center="map.center"
        zoom="map.zoom"
        draggable="true"
        options="map.options"
        control="map.control"
      >
      </ui-gmap-google-map>
    </div>
    <div class="col-sm-6">
      <form class="form-horizontal" role="form">
        <div class="form-group">
          <label class="col-sm-4 control-label">start</label>
          <div class="col-sm-8">
            <select class="form-control" ng-model="routePoints.start" 
                    ng-options="route.name for route in map.routes.start">
            </select>
          </div>
        </div>
<i>(Ctrl-Click for multiple selection))</i> <br>
        <div class="form-group">
          <label class="col-sm-4 control-label">Waypoints</label>
          <div class="col-sm-8">
            <select multiple class="form-control" ng-model="routePoints.ways" 
                    ng-options="route.name for route in map.routes.ways">
            </select>
          </div>
        </div>
        <div class="form-group">
          <label class="col-sm-4 control-label">end</label>
          <div class="col-sm-8">
            <select class="form-control" ng-model="routePoints.end" 
                    ng-options="route.name for route in map.routes.end">
            </select>
          </div>
        </div>
        <button ng-click="calcRoute(routePoints)" type="submit" class="btn btn-default">Submit</button>
      </form>
    </div>
  </div>
</div>
var mapApp = angular.module('googleMapApp', ['google-maps'.ns(),'ui.bootstrap']);
mapApp.config(['GoogleMapApiProvider'.ns(), function (GoogleMapApi) {
  GoogleMapApi.configure({
    key: 'your Google Map api key',
    v: '3.17',
    libraries: ''
  });
}]);

mapApp.controller("MyCtrl",['$scope', 'GoogleMapApi'.ns(), function ($scope,GoogleMapApi) {

  angular.extend($scope, {
    map: {
      center: {
        latitude: 35.681382,
        longitude: 139.766084
      },
      options: {
        maxZoom: 20,
        minZoom: 3
      },
      zoom: 15,
      control: {},
      routes: {
        start: [
          {name:'Tokyo Station', latlng:'35.6813,139.7660'},
          {name:'Ootemathi Station', latlng:'35.6842,139.7629'}
        ],
        ways: [
          {name:'Ootemon', latlng:'35.6856,139.7612'},
          {name:'Nijyubashi', latlng:'35.6794,139.7577'},
          {name:'Tokyo Tower', latlng:'日本, 東京都港区芝公園4-2-8'},
          {name:'Hama-rikyu Gardens', latlng:'日本, 東京都中央区浜離宮庭園1-1'}
        ],
        end: [
          {name:'Tokyo Station', latlng:'35.6813,139.7660'},
          {name:'Ootemathi Station', latlng:'35.6842,139.7629'}
        ]
      }
    },
    routePoints: {
      start: {},
      end: {}
    }
  });
  $scope.routePoints.start = $scope.map.routes.start[0];
  $scope.routePoints.end = $scope.map.routes.end[1];

  GoogleMapApi.then(function(maps) {
    var directionsDisplay = new maps.DirectionsRenderer();

    $scope.calcRoute = function (routePoints) {
      var ways = [];
      for (var i = 0; i < routePoints.ways.length; i++) {
        ways.push({
          location:routePoints.ways[i].latlng,
          stopover:true
        });
      };
      var directionsService = new maps.DirectionsService();

      directionsDisplay.setMap($scope.map.control.getGMap());
      var start = routePoints.start.latlng;
      var end = routePoints.end.latlng;
      var request = {
        origin: start,
        destination: end,
        waypoints: ways,
        optimizeWaypoints: true,
        travelMode: maps.TravelMode.WALKING
      };
      directionsService.route(request, function(response, status) {
        if (status == maps.DirectionsStatus.OK) {
          directionsDisplay.setDirections(response);
        }
      });
      return;
    };
  });
}]);