gpt4 book ai didi

android - map View 使用 google Directions API 绘制方向 - 解码多段线

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:54:11 24 4
gpt4 key购买 nike

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

如有任何帮助,我们将不胜感激。

最佳答案

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

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

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

希望对您有所帮助!肯尼


编辑:似乎 google directions 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;

}
}

关于android - map View 使用 google Directions API 绘制方向 - 解码多段线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6708408/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com