0

google direction api のレスポンスについては以下のリファレンスを参照してください。

https://developers.google.com/maps/documentation/directions/?csw=1#JSON

ご覧のとおり、各step タグには という名前のタグが含まれていますpolyline。そして、このタグには という名前のサブタグが含まれていpointsます。私が理解しているように、このタグには、この方向のステップを地図上に描くために必要なすべてのポイントが含まれています。ご覧のとおり、値はエンコードされています。何がエンコードされているかはわかりませんが、Google では以下の記事でアルゴリズムについて説明しています。

https://developers.google.com/maps/documentation/utilities/polylinealgorithm?csw=1

List<LatLng>この値をモノンドリッドで使用するためにデコードするためのコードを持っている人はいますか?

4

1 に答える 1

0

何度も検索して答えを見つけたので、このトピックを共有しました。以下の記事の Saboor Awan は、c# でエンコードする方法を説明しています。

http://www.codeproject.com/Tips/312248/Google-Maps-Direction-API-V3-Polyline-Decoder

モノドロイドで使用するためのコードは次のとおりです。

private List<LatLng > DecodePolylinePoints(string encodedPoints) 
{
    if (encodedPoints == null || encodedPoints == "") return null;
    List<LatLng> poly = new List<LatLng>();
    char[] polylinechars = encodedPoints.ToCharArray();
    int index = 0;
    int currentLat = 0;
    int currentLng = 0;
    int next5bits;
    int sum;
    int shifter;
    try
    {
        while (index < polylinechars.Length)
        {
            // calculate next latitude
            sum = 0;
            shifter = 0;
            do
            {
                next5bits = (int)polylinechars[index++] - 63;
                sum |= (next5bits & 31) << shifter;
                shifter += 5;
            } while (next5bits >= 32 && index < polylinechars.Length);
                if (index >= polylinechars.Length)
                break;
                currentLat += (sum & 1) == 1 ? ~(sum >> 1) : (sum >> 1);
                //calculate next longitude
            sum = 0;
            shifter = 0;
            do
            {
                next5bits = (int)polylinechars[index++] - 63;
                sum |= (next5bits & 31) << shifter;
                shifter += 5;
            } while (next5bits >= 32 && index < polylinechars.Length);
                if (index >= polylinechars.Length && next5bits >= 32)
                break;
                currentLng += (sum & 1) == 1 ? ~(sum >> 1) : (sum >> 1);
            LatLng p = new LatLng(Convert.ToDouble(currentLat) / 100000.0,
                Convert.ToDouble(currentLng) / 100000.0);
            poly.Add(p);
        } 
    }
    catch (Exception ex)
    {
        //log
    }
    return poly;
}

locationに置き換えるだけですLatLng

于 2014-02-09T08:09:01.257 に答える