Google map waypoints from geoJSON

后端 未结 2 801
野趣味
野趣味 2021-01-26 06:21

I want to load an itinerary from a geoJSON file. For the moment, it works, but only with two points.

But I need to add 4 or 5 waypoints. My code only read the two first

2条回答
  •  误落风尘
    2021-01-26 06:50

    The problem is that you can't access the FeatureCollection where a feature belongs to. There is also no event that will fire when the parsing of the geoJSON has been finished(when the addfeature-event fires you'll never know if it's the last time for the particular FeatureCollection)

    You may store additional properties for the features, e.g. the number of waypoints.

    sample-JSON(including other properties to define e.g. if a point is a waypoint, origin or destination or when it's a waypoint the index of the waypoint )

    {
    "type": "FeatureCollection",
    "features": [
      {
        "type"        : "Feature",
        "properties"  : {route      :{"id"    :1,
                                      "type"  :"origin",
                                      "points":2
                                     }
                        },
        "geometry"    : {"type"       : "Point",
                         "coordinates":[8.528849, 52.030656]}
      },
    
      {
        "type"        : "Feature",
        "properties"  : {route        :{"id"    :1,
                                        "type"  :"destination",
                                        "points":2
                                       }
                        },
        "geometry"    : {"type"       : "Point",
                         "coordinates":[11.5819, 48.1351253]}
      },
    
      {
        "type": "Feature",
        "properties"  : {"route"      :{"id"    :1,
                                        "type"  :"waypoint",
                                        "index" :1,
                                        "points":2
                                       }
                        },
        "geometry"    : {"type"       : "Point",
                         "coordinates":[13.40495,52.52]}
      },
    
      {
        "type"        : "Feature",
        "properties"  : {route        :{"id":1,
                                        "type":"waypoint",
                                        "index":0,
                                        "points":2
                                       }
                        },
        "geometry"    : {"type"       : "Point",
                         "coordinates":[9.99368, 53.5510846]}
      }
    ]}
    

    It stores the properties in a custom route-property.

    The properties are:

    • type(origin,destination or waypoint)
    • id(some unique id for the route, by using the id you'll be able to define multiple routes)
    • points(the number of waypoints defined for the route)
    • index...used for type:waypoint(the index of the waypoint in the waypoints-array, starting with 0)

    parsing of these properties:

     map.data.addListener('addfeature',function(e){
      var geo=  e.feature.getGeometry();
      if(geo.getType()==='Point' && e.feature.getProperty('route')){
        var id    = e.feature.getProperty('route').id,
            type  = e.feature.getProperty('route').type,
            points= e.feature.getProperty('route').points,
            data;
        //the routes will be stored as a property of map.data
        if(!map.data.get('routes')){
            map.data.set('routes',{});
        }
        if(!map.data.get('routes')[id]){
          map.data.get('routes')[id]={waypoints:[],points:points,origin:null,destination:null};
        }
    
        data= map.data.get('routes')[id];
        switch(type){
          case 'waypoint':
            data.points--;
            data.waypoints[e.feature.getProperty('route').index]={location:geo.get()};
           break;
          default:
            data[type]= geo.get();
    
        }
        if(!data.points && data.origin && data.destination){
          //parsing of the route is complete
          delete data.points;
          //run the callback, 
          //data is an object suitable to be used as DirectionsRequest
          //you only need to add the desired travelMode
          callback(data);
        }
      }
     });
    

    Demo: http://jsfiddle.net/doktormolle/vupkbasc/

提交回复
热议问题