Created
December 2, 2014 07:46
-
-
Save bennlich/dabfa6b0a5841e88a707 to your computer and use it in GitHub Desktop.
service pattern example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* global angular, google */ | |
angular.module('app') | |
.service('router', ['$q', function($q){ | |
var router = new google.maps.DirectionsService(); | |
this.getRoutePromise = function(_origin, _destination) { | |
var deferred = $q.defer(); | |
var request = { | |
origin: _origin, | |
destination: _destination, | |
travelMode: google.maps.TravelMode.TRANSIT, | |
provideRouteAlternatives: true | |
}; | |
if (typeof request.origin == 'undefined' || typeof request.destination == 'undefined') { | |
// if origin or destination not defined, reject promise | |
deferred.reject('UNDEFINED_ENDPOINTS'); | |
} else { | |
// origin and destination are well defined | |
router.route(request, function(result, status) { | |
if (status != google.maps.DirectionsStatus.OK) { | |
// if google maps gave bad response, reject promise | |
deferred.reject(status); | |
} else { | |
// if google maps gave OK response, convert routes to promise | |
var routes = []; | |
angular.forEach( | |
result.routes, | |
function(route) { | |
this.push(route.legs[0]); | |
}, | |
routes); | |
deferred.resolve(routes); | |
} | |
}); | |
} | |
return deferred.promise; | |
/////////////////////////////////// | |
//// HOW TO USE THIS PROMISE //// | |
/////////////////////////////////// | |
// Use promise.then and promise.catch to add callbacks: | |
// var promise = router.getRoutePromise(origin,dest); | |
// promise.then(resolveCallback).catch(rejectCallback); | |
// | |
// rejectCallback should expect a string which is | |
// either 'UNDEFINED_ENDPOINTS' or one of | |
// google.maps.DirectionsStatus. | |
// | |
// resolveCallback should expect an array of google | |
// maps route objects, which looks like this (only | |
// relevant keys shown): | |
// route = { | |
// start_address (actual address, formatted str), | |
// start_location: { k (lat), B (long) }, | |
// end_address (actual address, formatted str), | |
// end_location: { k (lat), B (long) }, | |
// departure_time: { text ('00:00am'), time_zone (str), value (Date) }, | |
// arrival_time: { text ('00:00am'), time_zone (str), value (Date) }, | |
// distance: { text ('00.0 mi'), value (Int in meters) }, | |
// duration: { text ('0 hours 00 mins'), value (Int in seconds) }, | |
// steps (array of step objects) | |
// } | |
// | |
// ...and the step objects are similar: | |
// step = { | |
// start_location: { k (lat), B (long) }, | |
// end_location: { k (lat), B (long) }, | |
// distance: { text ('00.0 mi'), value (Int in meters) }, | |
// duration: { text ('0 hours 00 mins'), value (Int in seconds) }, | |
// travel_mode: (google.maps.TravelMode) | |
// instructions (description of step, formatted str) | |
// steps (array of step objects) | |
// } | |
// | |
// The steps array in the result object contains a | |
// step for each subsequent mode of transportation. | |
// Each of these steps has its own steps array which | |
// contains turn by turn directions. The instructions | |
// string describes each step and matches its | |
// specificity. | |
} | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment