2014-10-20 4 views
1

Я хочу проверить, нарисована ли новая ломаная линия на карте Google или перекрывается с любой из ранее нарисованных полилиний (B, C, D ...). Во время поиска решения я получил алгоритм следующего образомДекодирование полилинии в android

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 для второго делать во время цикла в строке

b = encoded.charAt(index++) - 63; 

как я могу обработать это исключение или я пропустил некоторые промежуточный шаг? Спасибо

ответ

0

Мы должны исключить любые литералы Java, найденные в полилинии String. Это может быть сделано с помощью

StringEscapeUtils.unescapeJava(polyline) 

выше может быть изменен в

polyline = StringEscapeUtils.unescapeJava(polyline.toString()); 
LatLng[] polylinePoints = PolylineDecoder.toLatLng(polyline); 
Смежные вопросы