-
Notifications
You must be signed in to change notification settings - Fork 75
Online routing
Nutiteq edited this page Apr 8, 2014
·
3 revisions
For online routing there are couple of backend implementations. CloudMade is based on XML response, MapQuest provides JSON data, which is much clearer to parse. All the backend servers provide their specific additional data, and somewhat different logics, so you probably want to modify these server API implementations.
Update 2014 March: CloudMade API service is deprecated since May 2014, we suggest to use Mapquest instead.
Both routing samples in AdvancedMap3D sources require that you click on two locations on map (start and end point), and it shows route as Line (click on line to see route lenght) and turn points as markers (click on marker shows turn instructions).
See AdvancedMap3D sample app has MapQuestRouteActivity.
- Add RouteMapEventListener to MapView which follows map clicks, and provides events for first and second click on map.
- RouteActivity.setStartMarker(MapPos) is called for first click on map
- RouteActivity.setStopMarker(MapPos) and showRoute() are both called for the second click.
- Vector Layers are created and added to the map: one for route Line, and another for Markers (route start, end and waypoints)
- Activity implements RouteActivity interface
- setStartMarker adds a marker to start location
- setStopMarker adds marker to route end
- showRoute() initiates real routing:
@Override
public void showRoute(final double fromLat, final double fromLon,
final double toLat, final double toLon) {
Log.debug("calculating path " + fromLat + "," + fromLon + " to "
+ toLat + "," + toLon);
Projection proj = mapView.getLayers().getBaseLayer().getProjection();
StyleSet<LineStyle> routeLineStyle = new StyleSet<LineStyle>(LineStyle.builder().setWidth(0.05f).setColor(0xff9d7050).build());
Map<String, String> routeOptions = new HashMap<String,String>();
routeOptions.put("unit", "K"); // K - km, M - miles
routeOptions.put("routeType", "fastest");
// Add other route options here, see http://open.mapquestapi.com/directions/
directionsService = new MapQuestDirections(this, new MapPos(fromLon, fromLat), new MapPos(toLon, toLat), routeOptions, MAPQUEST_KEY, proj, routeLineStyle);
directionsService.route();
}
- When routing is done, then MapQuestDirections calls routeResult(Route) of the Activity to report about the results. As it is online service, then couple of seconds are expected and service runs in the backgrund
@Override
public void routeResult(Route route) {
// sometimes routing fails
if(route.getRouteResult() != Route.ROUTE_RESULT_OK){
Toast.makeText(this, "Route error", Toast.LENGTH_LONG).show();
return;
}
// cleanup old route, just in case
startMarker.setVisible(false);
stopMarker.setVisible(false);
routeLayer.clear();
// add route layer to map, refresh to be sure it is visible
routeLayer.add(route.getRouteLine());
mapView.requestRender();
Log.debug("route line points: "+route.getRouteLine().getVertexList().size());
// Log.debug("route line: "+route.getRouteLine().toString());
Toast.makeText(this, "Route "+route.getRouteSummary(), Toast.LENGTH_LONG).show();
// finally load and add turn marker images - takes time (some seconds) as these are loaded online
directionsService.startRoutePointMarkerLoading(markerLayer, MARKER_SIZE);
}