google maps - How can I turn a nested callback into a jQuery promise -
Now I have to remove the distance through every Google Map for every home.
var campus = {Loc: [-122.4579294, 37.7632498]} House houses = [[Local: [-122.4629668, 37.75 9 62]}, {loc: [122.4629672, 37.75 9 60 ]} // ...] function distance (msg) {var type = Msg.type var distance = 0 house [type] = home [type] || || [] Home [type]. Push (msg) $ .each (home [type], function (index, value) {distance + = value.distance.value}) home [type] .allDistance = distance / 1000 * 0.62} function caculateRoute (transType, home) {Var campusLoc = campus.loc var homeLoc = home.loc // map by google map object map.travelRoute ({origin: [homeLoc [1], homeLoc [0]], destination: [campus lock [1], campus Lock [0], traffic mode: transcript, step: function (e) {distance readie ({type: transite, distance: {text: e distance text, value: e distance.value}, step_number: e.step_number, duration : E.duration, Path: e.path, Instructions: E.instructions, Directions: {left: e.instructions.indexOf ('left')> -1, right: index of 'e.instructions' ('right')> 1}})}}}} } $ .value = function (ARR) {returns for $ .apple ($, AR)} var deferreds = [] (var i = 0, l = homelist.length; i & lt; L + i ++ ) {Var home = homelist [i] // I do not promise how to wrap the caculateRoute function in a promise and then each deferred all your return / var deferred = caculateRoute ('transit', home) // deferreds.push ( Deferred)} $ Then (deferreds) .done (function () {for (var i = 0; i & lt; arguments.length; i ++) {var distance = argument [i]}})
< P> Therefore, I can bring the distance to each house, and then I need all the distance when all of them are done. How to wrap the caculateRoute and modify the range of RD function, so that I can get their distance in $. When all (deferreds) .done
After reading the jQuery Diffin and Google-Maps API, I finally found a way. Here is the code:
Function Rounded (home, message) {var type = msg.type var distance = 0 var period = 0 home [type] = home [type] || [In the house ['type'] = true home [type]. Push (msg) $ .each (home [type], function (index, value) {distance + = value.distance.value duration = value Duration.value}) home.distance = distance / 1000 * 0.62} function caculateRoute ( Home, suspended) {var transType = 'driving' var campusLoc = campus.loc.coordinates var homeLoc = home.loc.coordinates map.travelRoute ({origin: [homeLoc [1], homolog [0], destination: [campus Lock [1], campus lock [0]], traffic mode: transcript, step: function (e) {distance, ready (house, {transit type}, distance: {text: e.distance.text, value: e. Distance.value}, step_number: e.step_number, duration: e.duration, path: e.path, instructions: e.instructions, direction: {left: e.instruct Ions .indexOf ('left')> 1, right: e.instructions.indexOf ('right') & gt; -1}})}, end function (e) {defer.re solution ()} }) Return defer} $ .whenall = function {arr} {return $. when. Apely ($, arr) function for caculateDistance (homelist, cb) {var deferreds = [] (var i = 0, l = homelist, langhai; l & gt; l; i ++) {var home = homelist [ I] var defer = $ Disabled () deferreds.push (caculateRoute (home, defer) $ .whenall (deferreds) .done (function () {console.log ('done') CB ()}}}
< P> And there is only one problem that I can not solve like Google root service that search times are limited to 8 ~ 10, so it always fails if the house is more than 10 Here can be found:
Comments
Post a Comment