android get and parse Google Directions

后端 未结 4 1484
没有蜡笔的小新
没有蜡笔的小新 2020-12-14 22:16

google Directions API

I read this guide now I can build a correct request to receive the xml file containg the directions from address A to address B. What I need is

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-14 22:53

    I've tweaked urobo's answer above (very slightly) to give you LatLngs which you'll want for Google Maps for Android V2:

    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;
    }
    

提交回复
热议问题