Web Programming examples

Google Maps,AngularJS

Google Maps for AngularJS Directions Service StrokeColor StrokeWeight(v1.2.1)

  • AngularJS : 1.2.23
  • Bootstrap : 3.2.0
  • Google Maps for AngularJS : 1.2.1
  • include file
  • angular.min.js, lodash.underscore.min.js, angular-google-maps.min.js, ui-bootstrap-tpls-0.11.0.min.js
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&key=xxxxxxxxxxxxxxxxxx&sensor=false"></script>
<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">
      <google-map 
        id="map-canvas"
        center="map.center"
        zoom="map.zoom"
        draggable="true"
        options="map.options"
        control="map.control"
      >
      </google-map>
    </div>
    <div class="col-sm-6">
      <form class="form-horizontal" role="form">
        <div class="form-group">
          <label class="col-sm-2 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>
        <div class="form-group">
          <label class="col-sm-2 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','ui.bootstrap']);

mapApp.controller("MyCtrl", function ($scope) {

  angular.extend($scope, {
    map: {
      center: {
        latitude: 35.681382,
        longitude: 139.766084
      },
      options: {
        maxZoom: 20,
        minZoom: 3
      },
      zoom: 16,
      control: {},
      routes: {
        start: [
          {name:'Tokyo Station', latlng:'35.6813177190391,139.76609230041504'},
        ],
        end: [
          {name:'Ootemon', latlng:'35.68567497604782,139.7612428665161'},
          {name:'Nijyubashi', latlng:'35.67947017023017,139.75772380828857'}
        ]
      }
    },
    routePoints: {
      start: {},
      end: {}
    }
  });
  $scope.routePoints.start = $scope.map.routes.start[0];
  $scope.routePoints.end = $scope.map.routes.end[0];

  var polyOptions = {
    strokeColor: '#ff0000',
    strokeOpacity: 0.7,
    strokeWeight: 7
  };
  var rendererOptions = {
    polylineOptions: polyOptions
  };
  var directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);

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