Widok mapy rysuj Kierunki za pomocą Google Directions API-dekodowanie polylines

Próbuję użyć Google directions API, aby pokazać kierunki na moim mapview, ale mam problemy z uzyskaniem danych z odpowiedzi JSON. Mogę uzyskać ciągi "poziomów" i "punktów", ale nie mogę rozszyfrować ich do punktów na mapie.

Każda pomoc będzie bardzo mile widziana.

Author: Sheena, 2011-07-15

3 answers

Mam klasę, która może je za Ciebie dekodować, Dodaj klasę poniżej, a następnie wywołaj swój kod w następujący sposób:

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

Gdzie points i levels są danymi wyodrębnionymi z odpowiedzi JSON. Następnie możesz przejść przez tablicę geopunktów, rysując linię między nimi, aby wyświetlić wskazówki.

Mam nadzieję, że to pomoże! Kenny

EDIT: wydaje się, że google directions API nie zwraca już ciągu poziomów powiększenia jako części odpowiedzi JSON, nie martw się jednak, wszystko, czego używaliśmy, to sprawdzanie liczby punktów, więc możemy po prostu umieścić je na liście w stylu:

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;
}

EDIT: STARY KOD

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;

}
}
 19
Author: Kenny,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-06-28 03:55:33

Możesz użyć Google Maps Android Api Utility Library . Proponuje PolyUtil.decode(String encoded) metoda, która robi dokładnie to, czego potrzebujesz !

 6
Author: nicopico,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-05-23 12:51:07

GeoPoint nie działa dla mnie, nie mogę znaleźć biblioteki, która go używa. Tutaj jest funkcja, która zwraca wartości LatLng zamiast.

public static ArrayList<LatLng> decodePolyPoints(String encodedPath){
    int len = encodedPath.length();

    final ArrayList<LatLng> path = new ArrayList<LatLng>();
    int index = 0;
    int lat = 0;
    int lng = 0;

    while (index < len) {
        int result = 1;
        int shift = 0;
        int b;
        do {
            b = encodedPath.charAt(index++) - 63 - 1;
            result += b << shift;
            shift += 5;
        } while (b >= 0x1f);
        lat += (result & 1) != 0 ? ~(result >> 1) : (result >> 1);

        result = 1;
        shift = 0;
        do {
            b = encodedPath.charAt(index++) - 63 - 1;
            result += b << shift;
            shift += 5;
        } while (b >= 0x1f);
        lng += (result & 1) != 0 ? ~(result >> 1) : (result >> 1);

        path.add(new LatLng(lat * 1e-5, lng * 1e-5));
    }

    return path;
}

Chwycił go z Google Maps Android API utility library . Kod można znaleźć tutaj

Niektóre przypomnienia podczas testowania tego z mocno zakodowanymi łańcuchami w kodzie, Java nie może poprawnie odczytać

"\"

Musisz dodać kolejny ukośnik, aby został poprawnie odczytany przez Javę.

"\\"
/ Align = "left" / ciągi zawierają dziwne znaki.
 0
Author: Marlon,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-29 08:00:33