我有一个可以为您解码它们的类,在下面添加该类,然后像这样调用您的代码:
int[] depredZoomLevels = PolylineDeprer.depreZoomLevels(levels);GeoPoint[] gPts = PolylineDeprer.deprePoints(points, depredZoomLevels.length);
您从JSON响应中提取的数据在哪里
points和
levels。然后,您可以遍历一系列地理点,在它们之间画一条线以显示方向。
希望这可以帮助!肯尼
编辑:似乎Google Directions
API不再将缩放级别字符串作为JSON响应的一部分返回,不过不用担心,我们使用的唯一目的就是检查点数,因此我们可以简单地将它们放入像这样的列表:
public static List <GeoPoint> deprePoints(String enpred_points){int index = 0;int lat = 0;int lng = 0;List <GeoPoint> out = new ArrayList<GeoPoint>();try { int shift; int result; while (index < enpred_points.length()) { shift = 0; result = 0; while (true) { int b = enpred_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 = enpred_points.charAt(index++) - '?'; result |= ((b & 31) << shift); shift += 5; if (b < 32) break; } lng += ((result & 1) != 0 ? ~(result >> 1) : result >> 1); out.add(new GeoPoint((lat*10),(lng*10))); } return out;}catch(Exception e) { e.printStackTrace();}return out;}编辑:旧代码
public class PolylineDeprer {public static GeoPoint[] deprePoints(String enpred_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 < enpred_points.length()) { shift = 0; result = 0; while (true) { int b = enpred_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 = enpred_points.charAt(index++) - '?'; result |= ((b & 31) << shift); shift += 5; if (b < 32) break; } lng += ((result & 1) != 0 ? ~(result >> 1) : result >> 1); out[cnt++] = new GeoPoint((lat*10),(lng*10)); } return out; }catch(Exception e) { e.printStackTrace(); } return out;}public static int[] depreZoomLevels(String enpredZoomLevels){ int[] out = new int[enpredZoomLevels.length()]; int index = 0; for(char c : enpredZoomLevels.toCharArray()) out[index++] = c - '?'; return out;}}


