Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
727 views
in Technique[技术] by (71.8m points)

java - How to Add Snap to Roads Google Map in Android Studio

Hello I would like to ask how to add Snap to Road when I have the route given by google map API. I have a bunch of Lat lang from point A line to point B line and draw a lines like Polylines, but what i want is how to add this code snap to Road from given route? this is how to add more points from the Point A to Point B, here is what i want to add, https://developers.google.com/maps/documentation/roads/snap

my project is look like this enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  1. Get overview_polyline by Json and Gson (should use https://maps.googleapis.com/maps/api/directions/json?origin=...&destination=place_id:...&mode=DRIVING&key=...)
  2. Decode it to List by function

    public List<LatLng> decodePoly(String encoded) {
    // encoded is overview_polyline.points; 
    List<LatLng> poly = new ArrayList<LatLng>();
    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;
    
        LatLng p = new LatLng((((double) lat / 1E5)),
                (((double) lng / 1E5)));
        poly.add(p);
    }
    return poly;
    }
    

    3.Add to map:

    PolylineOptions polylineOptions= new PolylineOptions();
    polylineOptions.addAll(decodePoly(overview_polyline.points));
    mGoogleMap.addPolyline(polylineOptions.width(5).color(Color.BLUE).geodesic(false));
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...