Web Programming examples

Google Maps,AngularJS

Google Maps for AngularJS Directions Service StrokeColor StrokeWeight(v2.4.1)

  • AngularJS : 1.6.5
  • Google Maps for AngularJS : 2.4.1
  • include file
  • angular.min.js, lodash.underscore.min.js, angular-simple-logger.min.js,angular-google-maps.min.js
<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-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', ['uiGmapgoogle-maps']);

mapApp.config(
    ['uiGmapGoogleMapApiProvider', function(GoogleMapApiProviders) {
        GoogleMapApiProviders.configure({
          key: 'your Google Map api key',
          v: '3', //defaults to latest 3.X anyhow
          libraries: 'weather,geometry,visualization'
        });
    }]
);

mapApp.controller("MyCtrl",['$scope', 'uiGmapGoogleMapApi', function ($scope,uiGmapGoogleMapApi) {
  angular.extend($scope, {
    map: {
      center: {
        latitude: 35.6813,
        longitude: 139.7660
      },
      options: {
        maxZoom: 20,
        minZoom: 3
      },
      zoom: 16,
      control: {},
      routes: {
        start: [
          {name:'Tokyo Station', latlng:'35.6813,139.7660'},
          {name:'Ootemathi Station', latlng:'35.6842,139.7629'}
        ],
        end: [
          {name:'Ootemon', latlng:'35.6856,139.7612'},
          {name:'Nijyubashi', latlng:'35.6794,139.7577'}
        ]
      }
    },
    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
  };

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

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

Leave a Reply

Your email address will not be published. Required fields are marked *