The problem is that you cannot access the FeatureCollection to which the function belongs. Also, there is no event that will fire when the geoJSON analysis is completed (when the addfeature-event event fires, you will never know if this was the last time for this FeatureCollection)
You can save additional properties for functions, for example. number of waypoints.
sample-JSON (including other properties for determining, for example, if the point is a waypoint, source, or destination, or when it is a waypoint pointer to a 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]} } ]}
Saves properties in a custom route -property.
Properties:
type (source, destination or waypoint)id (some unique identifier of the route, using the identifier, you can define several routes)points (number of waypoints defined for the route)index ... is used for type: waypoint (waypoint index in waypoints-array, starting at 0)
analysis 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/