我有一个应用程序,在网址映射的url请求中给出2个邮政编码后解析json响应。我从响应中获得了overviewpolyline节点并将其转换为字符串。我假设overviewpolyline节点是一条折线的整个路线。
下面是一些代码,我发现当将折线作为字符串传递时,它会将该字符串转换为GeoPoints列表。我已经检查过折线字符串是否为空,而对于2个邮政编码,字符串中有700多个字符。所以那里没问题。
我已经在下面的代码源中标记了异常发生的位置。任何有关索引错误的想法。循环由'while'语句控制,只在小于折线长度时才迭代。
@SuppressWarnings("unchecked")
private List decodePolyLine(final String poly) {
int len = poly.length();
Log.e(TAG, "poly string length = "+poly.length());
int index = 0;
List decoded = new ArrayList();
int lat = 0;
int lng = 0;
while (index < len) {
int b;
int shift = 0;
int result = 0;
do {
b = poly.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 = poly.charAt(index++) - 63; <--------****error here****
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lng += dlng;
decoded.add(new GeoPoint(
(int)(lat*1e6) , (int)(lon * 1e6)));
}
return decoded;
}//end of decodePolyLine
答案 0 :(得分:0)
解决。问题与上面的代码无关,而是我如何解析json响应。我需要在overviewpolyline对象中获取points对象。例如
JSONObject results = null;
try {
results = new JSONObject(jsonOutput);
routes = results.getJSONArray("routes");
anonObject = routes.getJSONObject(0);
bounds = anonObject.getJSONObject("bounds");
overViewPolyline = anonObject.getJSONObject("overview_polyline");
polyPoints = overViewPolyline.getString("points");
Log.e(TAG,"overview_polyline = " + overViewPolyline);
Log.e(TAG,"points = " + polyPoints);
northeast = bounds.getJSONObject("northeast");
lat = (Double) northeast.get("lat");
lon = (Double) northeast.get("lng");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
答案 1 :(得分:0)
我们从该响应得到的一些编码的“ overview_polyline”进行了编码,具有双编码的反斜杠。要处理对静态地图的请求,需要将\转换为\