Skip to content
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).

MapQuest Directions API

MapQuest route sample in London See AdvancedMap3D sample app has MapQuestRouteActivity.

Listener

  1. Add RouteMapEventListener to MapView which follows map clicks, and provides events for first and second click on map.
  2. RouteActivity.setStartMarker(MapPos) is called for first click on map
  3. RouteActivity.setStopMarker(MapPos) and showRoute() are both called for the second click.

Route handling in Activity

  1. Vector Layers are created and added to the map: one for route Line, and another for Markers (route start, end and waypoints)
  2. Activity implements RouteActivity interface
  3. setStartMarker adds a marker to start location
  4. setStopMarker adds marker to route end
  5. 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();
    }
  1. 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);
    }