我想检查谷歌地图上的新折线A绘图是否接触或重叠到任何先前绘制的折线(B,C,D ......)。在搜索解决方案期间,我得到了如下算法
Polyline findExistingPolyline(Polyline[] polylines, Polyline polyline) {
LatLng[] polylinePoints = PolylineDecoder.toLatLng(polyline);
for (Polyline existing: polylines) {
LatLng[] existingPoints = PolylineDecoder.toLatLng(existing);
if (isMostlyCovered(existingPoints , polylinePoints)) {
return existing;
}
}
return null;
}
boolean isMostlyCovered(LatLng[] existingPoints, LatLng[] polylinePoints) {
int initialSize = polylinePoints.length;
for (LatLng point: polylinePoints) {
for (LatLng existingPoint: existingPoints) {
if (distanceBetween(existingPoint, point) <= 100) {
polylinePoints.remove();// I actually use iterator, here it is just demosnstration
}
}
}
// check how many points are left and decide if polyline is mostly covered
// if 90% of the points is removed - existing polylines covers new polyline
return (polylinePoints.length * 100 / initialSize) <= 10;
}
在这个算法中,一个PolylineDecoder类将一条折线传递给它所使用的方法,后来我发现这个类在多个链接上,但几乎在每个地方都接收到一个不是折线的字符串
public class PolylineDecoder {
public static ArrayList decodePoly(String encoded) {
ArrayList 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;
Location p = new Location((((double) lat / 1E5)),
(((double) lng / 1E5)));
poly.add(p);
}
return poly;
}
}
所以我把折线转换成字符串并将其传递给PolylineDecoder类
LatLng[] polylinePoints = PolylineDecoder.toLatLng(polyline.toString());
现在,当我运行程序时,它会在PolylineDecoder类中为第二行do while循环提供异常(字符串out of bound of exception)
b = encoded.charAt(index++) - 63;
我该如何处理此异常或者是否错过了一些中间步骤?感谢
答案 0 :(得分:0)
我们应该忽略在String poly line中找到的任何Java文字。可以使用
完成StringEscapeUtils.unescapeJava(polyline)
以上内容可以改为
polyline = StringEscapeUtils.unescapeJava(polyline.toString());
LatLng[] polylinePoints = PolylineDecoder.toLatLng(polyline);