Android: DrivingDirections removed since API 1.0 - how to do it in 1.5/1.6?
Asked Answered
L

5

1

It seems that DrivingDirections have been removed since Android API 1.0

What's the way now to display a map with two points (one of them might be the current location, but can also be any other location) and the direction from one to another in Android 1.6?

Lowry answered 23/10, 2009 at 10:16 Comment(0)
G
2

I suspect the answer is to sign up for a multi-million-dollar license from TeleNav or somebody.

The reason the API was pulled was because Google itself has limitations on what it can do with driving directions (e.g., cannot do real-time turn-by-turn stuff), since Google licenses this data from TeleNav or other firms. Google, in turn, cannot let developers do those restricted things, and an open API cannot be adequately protected for this case.

I thought I saw mention that Open Street Map has driving directions, FWIW.

Goth answered 23/10, 2009 at 15:14 Comment(0)
S
4

If you just need the point connected with a line you do not need the full kml. A much faster way to do it is to just use the JSON returned from Google Maps API with output=dragdir

private String getUrl(String start, String end) {
    //If params GeoPoint convert to lat,long string here
    StringBuffer urlString = new StringBuffer();
    urlString.append("http://maps.google.com/maps?f=d&hl=en");
    urlString.append("&saddr=");// from
    urlString.append(start);
    urlString.append("&daddr=");// to
    urlString.append(end);
    urlString.append("&ie=UTF8&0&om=0&output=dragdir"); //DRAGDIR RETURNS JSON
    Log.i("URLString", urlString.toString());
    return urlString.toString();
}

This urlString can be used to get a JSON file, which you can easily extract the information using String's split()

private String getConnection(String url) {
    URL inUrl = new URL(url);
    URLConnection yc = inUrl.openConnection();
    BufferedReader in = new BufferedReader( new InputStreamReader(yc.getInputStream()));
    String inputLine;
    String encoded = "";
    while ((inputLine = in.readLine()) != null)
        encoded = encoded.concat(inputLine);
    in.close();
    String polyline = encoded.split("points:")[1].split(",")[0];
    polyline = polyline.replace("\"", "");
    polyline = polyline.replace("\\\\", "\\");
    return polyline;
}

The returned String is a polyline which can be decoded into an list of Geopoints using the method below.

private static ArrayList<GeoPoint> decodePolyline(String encoded) {
    ArrayList<GeoPoint> geopoints = new ArrayList<GeoPoint>();
    int index = 0, len = encoded.length();
    int lat = 0, lng = 0;
    while (index < len) {
        int b, shift = 0, result = 0;
        do {
            b = encoded.charAt(index++) - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);
        int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
        lat += dlat;
        shift = 0;
        result = 0;
        do {
            b = encoded.charAt(index++) - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);
        int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
        lng += dlng;
        GeoPoint p = new GeoPoint((int) (((double) lat / 1E5) * 1E6), (int) (((double) lng / 1E5) * 1E6));
        geopoints.add(p);
    }
 return geopoints;
}

The last step is to draw these points to a mapView, for that we need an overlay item that will handle a ArrayList of GeoPoints.

public class PathOverlay extends Overlay {

    private ArrayList<GeoPoint> pointList;

    public PathOverlay(ArrayList<GeoPoint> pointList) {
            this.pointList = pointList;     
    }

    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow) {
        Point current = new Point();
        Path path = new Path();
        Projection projection = mapView.getProjection();
        Iterator<GeoPoint> iterator = pointList.iterator();
        if (iterator.hasNext()) {
            projection.toPixels(iterator.next(), current);
            path.moveTo((float) current.x, (float) current.y);
        } else return;
        while(iterator.hasNext()) {
            projection.toPixels(iterator.next(), current);
            path.lineTo((float) current.x, (float) current.y);
        }

        Paint pathPaint = new Paint();
        pathPaint.setAntiAlias(true);
        pathPaint.setStrokeWidth(3.0f);
        pathPaint.setColor(Color.GREEN);
        pathPaint.setStyle(Style.STROKE);
        canvas.drawPath(path, pathPaint);
    }
}

If your not sure about some of the intermediate steps, such as how to get the overlay onto the MapView or how to set up a router class let me know and I can send you the complete code.

Sylvie answered 10/11, 2011 at 19:3 Comment(0)
G
2

I suspect the answer is to sign up for a multi-million-dollar license from TeleNav or somebody.

The reason the API was pulled was because Google itself has limitations on what it can do with driving directions (e.g., cannot do real-time turn-by-turn stuff), since Google licenses this data from TeleNav or other firms. Google, in turn, cannot let developers do those restricted things, and an open API cannot be adequately protected for this case.

I thought I saw mention that Open Street Map has driving directions, FWIW.

Goth answered 23/10, 2009 at 15:14 Comment(0)
Q
2

It maybe a little too late, but still helpful for others. I have written a few simple classes to get and display driving directions in MapView for Android:

http://home.ameliemedia.com/android-app-aroundme/#tips

Hope it helps!

Andrea.

Quiver answered 2/12, 2009 at 17:21 Comment(1)
Andrea, that URL does not provide example code--seems the domain registration has expired. Is it available somewhere else?Inchoative
B
0

The current solution from Google is to use Google Maps API Web Services. Free for limited use; you'll need to pay for (successful) commercial usage.

Bloodhound answered 16/9, 2011 at 18:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.