Decoding polyline with new Google Maps API

后端 未结 6 1678
时光取名叫无心
时光取名叫无心 2020-12-07 21:12

I am drawing a route between two points in a map. I receive the points this way:

StringBuilder urlString = new StringBuilder();
    urlString.append("htt         


        
6条回答
  •  -上瘾入骨i
    2020-12-07 21:51

    This method is useful for decoding polyline

     private List decodePoly(String encoded) {
        List poly = new ArrayList<>();
        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;
    }
    

    This is how i'm getting polyline

    JSONObject poly = route.getJSONObject("overview_polyline");
    String polyline = poly.getString("points");
    polyLineList = decodePoly(polyline);
    

提交回复
热议问题