ngmap
Version:
The Simplest AngularJS Google Maps V3 Directive
171 lines • 13.4 kB
HTML
<html>
<head>
<meta charset="utf-8">
<base href="../">
<title>JSDoc: source : directions.js</title>
<link href="css/prettify-tomorrow.css" type="text/css" rel="stylesheet">
<link href="css/site.css" type="text/css" rel="stylesheet">
</head>
<body ng-app="">
<nav id="toc">
<input placeholder="Filter" id="filter-input" class="col12 block field" type="text">
<div class="nav-wrapper">
<h2><a href="index.html">Index</a></h2>
<ul class="module">
<!-- undefined -->
<h2 class="module">
<a chref="" ng-click="moduleundefined = !moduleundefined">
module: undefined
</a>
<i ng-cloak="" ng-show="moduleundefined">+</i>
</h2>
<li id="undefined" ng-hide="moduleundefined">
<ul class="group">
<h2>
<a href="" ng-click="undefinedservice = !undefinedservice">
service
</a>
<i ng-cloak="" ng-show="undefinedservice">+</i>
</h2>
<ul ng-hide="undefinedservice">
<li>
<a href="Attr2MapOptions.html">Attr2MapOptions</a>
</li><li>
<a href="GeoCoder.html">GeoCoder</a>
</li><li>
<a href="GoogleMapsApi.html">GoogleMapsApi</a>
</li><li>
<a href="NavigatorGeolocation.html">NavigatorGeolocation</a>
</li><li>
<a href="StreetView.html">StreetView</a>
</li>
</ul>
</ul><ul class="group">
<h2>
<a href="" ng-click="undefineddirective = !undefineddirective">
directive
</a>
<i ng-cloak="" ng-show="undefineddirective">+</i>
</h2>
<ul ng-hide="undefineddirective">
<li>
<a href="bicycling-layer.html">bicycling-layer</a>
</li><li>
<a href="custom-control.html">custom-control</a>
</li><li>
<a href="directions.html">directions</a>
</li><li>
<a href="drawing-manager.html">drawing-manager</a>
</li><li>
<a href="dynamic-maps-engine-layer.html">dynamic-maps-engine-layer</a>
</li><li>
<a href="fusion-tables-layer.html">fusion-tables-layer</a>
</li><li>
<a href="heatmap-layer.html">heatmap-layer</a>
</li><li>
<a href="info-window.html">info-window</a>
</li><li>
<a href="kml-layer.html">kml-layer</a>
</li><li>
<a href="map-data.html">map-data</a>
</li><li>
<a href="map-lazy-load.html">map-lazy-load</a>
</li><li>
<a href="map-type.html">map-type</a>
</li><li>
<a href="maps-engine-layer.html">maps-engine-layer</a>
</li><li>
<a href="marker.html">marker</a>
</li><li>
<a href="overlay-map-type.html">overlay-map-type</a>
</li><li>
<a href="places-auto-complete.html">places-auto-complete</a>
</li><li>
<a href="shape.html">shape</a>
</li><li>
<a href="streetview-panorama.html">streetview-panorama</a>
</li><li>
<a href="traffic-layer.html">traffic-layer</a>
</li><li>
<a href="transit-layer.html">transit-layer</a>
</li>
</ul>
</ul><ul class="group">
<h2>
<a href="" ng-click="undefinedprovider = !undefinedprovider">
provider
</a>
<i ng-cloak="" ng-show="undefinedprovider">+</i>
</h2>
<ul ng-hide="undefinedprovider">
<li>
<a href="NgMap.html">NgMap</a>
</li>
</ul>
</ul><ul class="group">
<h2>
<a href="" ng-click="undefinedfactory = !undefinedfactory">
factory
</a>
<i ng-cloak="" ng-show="undefinedfactory">+</i>
</h2>
<ul ng-hide="undefinedfactory">
<li>
<a href="NgMapPool.html">NgMapPool</a>
</li>
</ul>
</ul>
</li>
</ul><ul class="module">
<!-- ngmap -->
<h2 class="module">
<a chref="" ng-click="modulengmap = !modulengmap">
module: ngmap
</a>
<i ng-cloak="" ng-show="modulengmap">+</i>
</h2>
<li id="ngmap" ng-hide="modulengmap">
<ul class="group">
<h2>
<a href="" ng-click="ngmapdirective = !ngmapdirective">
directive
</a>
<i ng-cloak="" ng-show="ngmapdirective">+</i>
</h2>
<ul ng-hide="ngmapdirective">
<li>
<a href="ngmap.custom-marker.html">custom-marker</a>
</li>
</ul>
</ul>
</li>
</ul><ul class="module">
<!-- ng_ap -->
<h2 class="module">
<a chref="" ng-click="moduleng_ap = !moduleng_ap">
module: ngMap
</a>
<i ng-cloak="" ng-show="moduleng_ap">+</i>
</h2>
<li id="ngMap" ng-hide="moduleng_ap">
<ul class="group">
<h2>
<a href="" ng-click="ng_apdirective = !ng_apdirective">
directive
</a>
<i ng-cloak="" ng-show="ng_apdirective">+</i>
</h2>
<ul ng-hide="ng_apdirective">
<li>
<a href="ngMap.ng-map.html">ng-map</a>
</li>
</ul>
</ul>
</li>
</ul>
</div>
</nav>
<div id="content" class="page-wrap">
<h1 class="title">
source : directions.js
</h1>
<div id="main" class="big-container">
<!-- source code html -->
<article>
<pre class="prettyprint source linenums"><code>/**
* @ngdoc directive
* @name directions
* @description
* Enable directions on map.
* e.g., origin, destination, draggable, waypoints, etc
*
* Requires: map directive
*
* Restrict To: Element
*
* @attr {String} DirectionsRendererOptions
* [Any DirectionsRendererOptions](https://developers.google.com/maps/documentation/javascript/reference#DirectionsRendererOptions)
* @attr {String} DirectionsRequestOptions
* [Any DirectionsRequest options](https://developers.google.com/maps/documentation/javascript/reference#DirectionsRequest)
* @example
* <map zoom="14" center="37.7699298, -122.4469157">
* <directions
* draggable="true"
* panel="directions-panel"
* travel-mode="{{travelMode}}"
* waypoints="[{location:'kingston', stopover:true}]"
* origin="{{origin}}"
* destination="{{destination}}">
* </directions>
* </map>
*/
/* global document */
(function() {
'use strict';
var NgMap, $timeout, NavigatorGeolocation;
var requestTimeout, routeRequest;
// Delay for each route render to accumulate all requests into a single one
// This is required for simultaneous origin\waypoints\destination change
// 20ms should be enough to merge all request data
var routeRenderDelay = 20;
var getDirectionsRenderer = function(options, events) {
if (options.panel) {
options.panel = document.getElementById(options.panel) ||
document.querySelector(options.panel);
}
var renderer = new google.maps.DirectionsRenderer(options);
for (var eventName in events) {
google.maps.event.addListener(renderer, eventName, events[eventName]);
}
return renderer;
};
var updateRoute = function(renderer, options) {
var directionsService = new google.maps.DirectionsService();
/* filter out valid keys only for DirectionsRequest object*/
var request = options;
request.travelMode = request.travelMode || 'DRIVING';
var validKeys = [
'origin', 'destination', 'travelMode', 'transitOptions', 'unitSystem',
'durationInTraffic', 'waypoints', 'optimizeWaypoints',
'provideRouteAlternatives', 'avoidHighways', 'avoidTolls', 'region'
];
if (request) {
for(var key in request) {
if (request.hasOwnProperty(key)) {
(validKeys.indexOf(key) === -1) && (delete request[key]);
}
}
}
if(request.waypoints) {
// Check for acceptable values
if(!Array.isArray(request.waypoints)) {
delete request.waypoints;
}
}
var showDirections = function(request) {
if (requestTimeout && request) {
if (!routeRequest) {
routeRequest = request;
} else {
for (var attr in request) {
if (request.hasOwnProperty(attr)) {
routeRequest[attr] = request[attr];
}
}
}
} else {
requestTimeout = $timeout(function() {
if (!routeRequest) {
routeRequest = request;
}
directionsService.route(routeRequest, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
renderer.setDirections(response);
// Unset request for the next call
routeRequest = undefined;
}
});
$timeout.cancel(requestTimeout);
// Unset expired timeout for the next call
requestTimeout = undefined;
}, routeRenderDelay);
}
};
if (request && request.origin && request.destination) {
if (request.origin == 'current-location') {
NavigatorGeolocation.getCurrentPosition().then(function(ll) {
request.origin = new google.maps.LatLng(ll.coords.latitude, ll.coords.longitude);
showDirections(request);
});
} else if (request.destination == 'current-location') {
NavigatorGeolocation.getCurrentPosition().then(function(ll) {
request.destination = new google.maps.LatLng(ll.coords.latitude, ll.coords.longitude);
showDirections(request);
});
} else {
showDirections(request);
}
}
};
var directions = function(
Attr2MapOptions, _$timeout_, _NavigatorGeolocation_, _NgMap_) {
var parser = Attr2MapOptions;
NgMap = _NgMap_;
$timeout = _$timeout_;
NavigatorGeolocation = _NavigatorGeolocation_;
var linkFunc = function(scope, element, attrs, mapController) {
mapController = mapController[0]||mapController[1];
var orgAttrs = parser.orgAttributes(element);
var filtered = parser.filter(attrs);
var options = parser.getOptions(filtered, {scope: scope});
var events = parser.getEvents(scope, filtered);
var attrsToObserve = parser.getAttrsToObserve(orgAttrs);
var attrsToObserve = [];
if (!filtered.noWatcher) {
attrsToObserve = parser.getAttrsToObserve(orgAttrs);
}
var renderer = getDirectionsRenderer(options, events);
mapController.addObject('directionsRenderers', renderer);
attrsToObserve.forEach(function(attrName) {
(function(attrName) {
attrs.$observe(attrName, function(val) {
if (attrName == 'panel') {
$timeout(function(){
var panel =
document.getElementById(val) || document.querySelector(val);
console.log('setting ', attrName, 'with value', panel);
panel && renderer.setPanel(panel);
});
} else if (options[attrName] !== val) { //apply only if changed
var optionValue = parser.toOptionValue(val, {key: attrName});
console.log('setting ', attrName, 'with value', optionValue);
options[attrName] = optionValue;
updateRoute(renderer, options);
}
});
})(attrName);
});
NgMap.getMap().then(function() {
updateRoute(renderer, options);
});
element.bind('$destroy', function() {
mapController.deleteObject('directionsRenderers', renderer);
});
};
return {
restrict: 'E',
require: ['?^map','?^ngMap'],
link: linkFunc
};
}; // var directions
directions.$inject =
['Attr2MapOptions', '$timeout', 'NavigatorGeolocation', 'NgMap'];
angular.module('ngMap').directive('directions', directions);
})();
</code></pre>
</article>
<!-- index.html -->
<!-- class files -->
</div>
<footer style="clear:both">
Documentation generated by
<a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.2</a>
using
<a href="https://github.com/allenhwkim/angular-jsdoc">Angular-JSDoc template</a>
</footer>
</div>
<script src="js/prettify.js"></script>
<script src="js/angular.min.js"></script>
<script src="js/site.js"></script>
<!--%= prettyJson %-->
<script>
prettyPrint();
var lineNo = window.location.hash.match(/#line([0-9]+)$/);
lineNo && document.querySelector("ol li:nth-child("+(lineNo[1])+")").scrollIntoView();
</script>
</body>
</html>