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

怎甘沉沦 提交于 2019-11-28 02:09:59

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


See also J2ME/Android/BlackBerry - driving directions, route between two locations




回答5:


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.



来源:https://stackoverflow.com/questions/1612533/android-drivingdirections-removed-since-api-1-0-how-to-do-it-in-1-5-1-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!