[guadec-web: 1/3] added support for multiple waypoints



commit fcb5316f77b2ecd9e674840c592105bc51c0318a
Author: Jorge Sanz <xurxosanz gmail com>
Date:   Tue Jul 3 20:45:55 2018 +0200

    added support for multiple waypoints

 content/pages/map.md            | 19 ++++---------------
 src/js/guadec_map/guadec-map.js | 32 ++++++++++++--------------------
 2 files changed, 16 insertions(+), 35 deletions(-)
---
diff --git a/content/pages/map.md b/content/pages/map.md
index 4e3ecd3..6b73af8 100644
--- a/content/pages/map.md
+++ b/content/pages/map.md
@@ -97,38 +97,27 @@ Date: 20180615
         */
         routes : [
             {
-                from: 2870058034,
-                to: 435775764,
+                waypoints: [2870058034, 435775764, 974730957],
                 title: 'GUADEC bus',
                 description: 'GUADEC direct bus route',
                 color: '#f00'
             },
             {
-                from: 435775764,
-                to: 974730957,
-                title: 'GUADEC bus',
-                description: 'GUADEC direct bus route',
-                color: '#f00'
-            },
-            {
-                from: 2306864400,
-                to: 27155530,
+                waypoints: [2306864400, 27155530],
                 method: 'walking',                 
                 title: 'to the castle',
                 description: 'Walk route from the bus stop to the visit starting point',
                 color: '#526635'
             },
             {
-                from: 27155530,
-                to: 5732646949,
+                waypoints: [27155530, 5732646949],
                 method: 'walking', 
                 title: 'to the party',
                 description: 'Walk route from the castle to the party',
                 color: '#5c3566'
             },
             {
-                from: 435775764,
-                to: 187403583,
+                waypoints: [435775764, 187403583],
                 method: 'walking', 
                 title: 'to the beach party',
                 description: 'Walk route from Civitas to the chiringuito',
diff --git a/src/js/guadec_map/guadec-map.js b/src/js/guadec_map/guadec-map.js
index 9137f48..5c79ea7 100644
--- a/src/js/guadec_map/guadec-map.js
+++ b/src/js/guadec_map/guadec-map.js
@@ -48,7 +48,7 @@ class GuadecMap {
             }
         };
 
-        var get_route = function(from,to,method) {
+        var get_route = function(coords,method) {
             var base_url = 'https://api.mapbox.com/directions/v5/mapbox/'+method;
             var params = {
                 'overview': 'full',
@@ -57,7 +57,7 @@ class GuadecMap {
             };
 
             // Transform to string
-            var coordinates = from.join(',') + ';' + to.join(',');
+            var coordinates = coords.map(c => c.coordinates).map( c => c.join(',')).join(';');
             var params_str = Object.keys(params).map( p => `${p}=${params[p]}`).join('&');
             // Final URL to get data from Mapbox
             var url = `${base_url}/${coordinates}.json?${params_str}`
@@ -68,17 +68,16 @@ class GuadecMap {
         var features_promises = routes.map(function(route){
             return new Promise((resolve,reject) => {
                 // Get route data 
-                var from = get_coordinates(route['from']);
-                var to = get_coordinates(route['to']);
+                var coords = route.waypoints.map(get_coordinates);
+                // var from = get_coordinates(route['from']);
+                // var to = get_coordinates(route['to']);
                 var title = route['title'];
                 var color = route['color'];
                 var description = route['description'];
                 var method = route['method'] || 'driving';
 
-                if (from && to){
-                    var from_geom = from.coordinates;
-                    var to_geom = to.coordinates;  
-                    get_route(from_geom,to_geom,method)
+                if (coords.length >= 2){ 
+                    get_route(coords,method)
                         .then(function(route){
                             if (route.routes){
                                 var the_route = route.routes[0];
@@ -498,21 +497,14 @@ class GuadecMap {
                 'coordinates' : feature.geometry.coordinates
             }
         });
+
+        var geom_ids_ids = geom_ids.map(x => x.id);
         
-        // get the list of route from/to ids not appearing in that list
+        // get the list of route waypoint ids not appearing in that list
         var new_ids = Array.from(new Set (
-            routes.map(function(route){
-                // check from
-                if (geom_ids.filter(x => x.id == route.from).length == 0){
-                    return route.from
-                }
-            })
-            .concat(
+            [].concat.apply([], 
                 routes.map(function(route){
-                    // check to
-                    if (geom_ids.filter(x => x.id == route.to).length == 0){
-                        return route.to;
-                    }
+                    return route.waypoints.filter( point => geom_ids_ids.indexOf(String(point)) == -1)
                 })
             )
             .filter(x => x != undefined)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]