Monday, January 17, 2011

Map Services -- Get Directions

RasterMap’s getDirections is used to get directions between two points or transit through a list of waypoints.  The result is also returned with a callback function.
The following example returns the route between “Perth” and “Sydney”.
The result is stored in an instance of MapDirection, which contains detail information like distance, driving instructions of each route, each steps.
public class MapRoutingMIDP extends MapDemoMIDP implements CommandListener,
        IRoutingListener {
   private Command mapGetDirectionCommand = new Command("Get Direction", Command.OK, 1);
    public void startApp() {
        init();
        canvas.addCommand(mapGetDirectionCommand);
        map.setRoutingListener(this);
        canvas.setCommandListener(this);
        GeoLatLng center = new GeoLatLng(-31.948275, 115.857562);
        map.setCenter(center, 13, MapType.GOOGLEMAP);
        Display.getDisplay(this).setCurrent(canvas);
    }

    public void commandAction(Command c, Displayable d) {
        if (c == mapGetDirectionCommand) {
            String name1 = "perth";
            String name2 = "sydney";
            map.getDirections("from: " + name1 + " to: " + name2);

        }
    }

    public void done(String query, MapDirection result) {
        if (result != null) {
            map.setMapDirection(result);
            map.resize(result.getBound());
            map.zoomOut();
        }
    }
}




For map service, there are Google Map Service, CloudMade Map Servcie and in China, MapAbc Map service to choose from. The default map service used is Google map service.
getDirections() has three overloaded methods. The example above uses the description format. If use string as the query input, CloudMade and MapAbc requires the following format “longitude1, latitude1, longitude2, latitude2”.
To avoid confusion, you can use following
public void getDirection(GeoLatLng[] waypoints, IRoutingListener listener);
Where waypoints are the transit points for the route. It may support multiple way points (>2) depends which service you use


No comments:

Post a Comment