12

我正在尝试使用 Google 路线 API 在我的地图视图上显示路线,但我无法从 JSON 响应中获取数据。我可以获得“级别”和“点”字符串,但无法弄清楚如何将它们解码为地图上的点。

任何帮助将非常感激。

4

4 回答 4

20

我有一个可以为你解码的类,添加下面的类,然后像这样调用你的代码:

int[] decodedZoomLevels = PolylineDecoder.decodeZoomLevels(levels);
GeoPoint[] gPts = PolylineDecoder.decodePoints(points, decodedZoomLevels.length);

wherepointslevels是您从 JSON 响应中提取的数据。然后,您可以浏览地理点数组,在它们之间画一条线以显示您的方向。

希望这可以帮助!肯尼


编辑:似乎谷歌方向 API 不再返回缩放级别字符串作为 JSON 响应的一部分,不过不用担心,我们使用它的目的只是检查点数,所以我们可以简单地将它们放入像这样的列表:

public static List <GeoPoint> decodePoints(String encoded_points){
int index = 0;
int lat = 0;
int lng = 0;
List <GeoPoint> out = new ArrayList<GeoPoint>();

try {
    int shift;
    int result;
    while (index < encoded_points.length()) {
        shift = 0;
        result = 0;
        while (true) {
            int b = encoded_points.charAt(index++) - '?';
            result |= ((b & 31) << shift);
            shift += 5;
            if (b < 32)
                break;
        }
        lat += ((result & 1) != 0 ? ~(result >> 1) : result >> 1);

        shift = 0;
        result = 0;
        while (true) {
            int b = encoded_points.charAt(index++) - '?';
            result |= ((b & 31) << shift);
            shift += 5;
            if (b < 32)
                break;
        }
        lng += ((result & 1) != 0 ? ~(result >> 1) : result >> 1);
        /* Add the new Lat/Lng to the Array. */
        out.add(new GeoPoint((lat*10),(lng*10)));
    }
    return out;
}catch(Exception e) {
    e.printStackTrace();
}
return out;
}

编辑:旧代码

public class PolylineDecoder {
/**
 * Transform a encoded PolyLine to a Array of GeoPoints.
 * Java implementation of the original Google JS code.
 * @see Original encoding part: <a href="http://code.google.com/apis/maps/documentation/polylinealgorithm.html">http://code.google.com/apis/maps/documentation/polylinealgorithm.html</a>
 * @return Array of all GeoPoints decoded from the PolyLine-String.
 * @param encoded_points String containing the encoded PolyLine. 
 * @param countExpected Number of points that are encoded in the PolyLine. Easiest way is to use the length of the ZoomLevels-String. 
 * @throws DecodingException 
 */
public static GeoPoint[] decodePoints(String encoded_points, int countExpected){
    int index = 0;
    int lat = 0;
    int lng = 0;
    int cnt = 0;
    GeoPoint[] out = new GeoPoint[countExpected];

    try {
        int shift;
        int result;
        while (index < encoded_points.length()) {
            shift = 0;
            result = 0;
            while (true) {
                int b = encoded_points.charAt(index++) - '?';
                result |= ((b & 31) << shift);
                shift += 5;
                if (b < 32)
                    break;
            }
            lat += ((result & 1) != 0 ? ~(result >> 1) : result >> 1);

            shift = 0;
            result = 0;
            while (true) {
                int b = encoded_points.charAt(index++) - '?';
                result |= ((b & 31) << shift);
                shift += 5;
                if (b < 32)
                    break;
            }
            lng += ((result & 1) != 0 ? ~(result >> 1) : result >> 1);
            /* Add the new Lat/Lng to the Array. */
            out[cnt++] = new GeoPoint((lat*10),(lng*10));
        }
        return out;
    }catch(Exception e) {
        e.printStackTrace();
    }
    return out;
}

public static int[] decodeZoomLevels(String encodedZoomLevels){
    int[] out = new int[encodedZoomLevels.length()];
    int index = 0;

    for(char c : encodedZoomLevels.toCharArray())
        out[index++] = c - '?';
    return out;

}
}
于 2011-07-15T14:22:24.747 回答
6

您可以使用Google Maps Android API 实用程序库。它提出了PolyUtil.decode(String encoded)一种完全符合您需要的方法!

于 2014-05-23T12:51:07.053 回答
0

GeoPoint 对我不起作用,我找不到使用它的库。这是一个返回 LatLng 值的函数。

public static ArrayList<LatLng> decodePolyPoints(String encodedPath){
    int len = encodedPath.length();

    final ArrayList<LatLng> path = new ArrayList<LatLng>();
    int index = 0;
    int lat = 0;
    int lng = 0;

    while (index < len) {
        int result = 1;
        int shift = 0;
        int b;
        do {
            b = encodedPath.charAt(index++) - 63 - 1;
            result += b << shift;
            shift += 5;
        } while (b >= 0x1f);
        lat += (result & 1) != 0 ? ~(result >> 1) : (result >> 1);

        result = 1;
        shift = 0;
        do {
            b = encodedPath.charAt(index++) - 63 - 1;
            result += b << shift;
            shift += 5;
        } while (b >= 0x1f);
        lng += (result & 1) != 0 ? ~(result >> 1) : (result >> 1);

        path.add(new LatLng(lat * 1e-5, lng * 1e-5));
    }

    return path;
}

Google Maps Android API 实用程序库中获取。代码可以在这里找到

使用代码中的硬编码字符串进行测试时的一些提醒,Java 无法正确读取

"\"

您需要添加另一个斜杠才能被java正确读取。

"\\"

只是提醒一下,因为编码的字符串包含奇怪的字符。

于 2015-09-29T08:00:33.500 回答
0

下面的代码解码任何编码的折线

private List<LatLng> decodePoly(String encoded) {
    List<LatLng> poly = new ArrayList<>();
    int index = 0, len = encoded.length();
    int lat = 0, lng = 0;

    while (index < len) {
        int b=0, shift = 0, result = 0;
        do {
            if(index<len) {
                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 {
            if(index<len) {
                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;
}
于 2021-05-11T11:04:49.080 回答