5

MapKit 上の 2 つの場所の間のルートを描画する簡単なアプリケーションを作成しました。Google マップ API を使用しています。オンラインで見つけたリソースを使用しました。Google にリクエストするために使用しているコードは次のとおりです。

_httpClient = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://maps.googleapis.com/"]];
    [_httpClient registerHTTPOperationClass: [AFJSONRequestOperation class]];
    [_httpClient setDefaultHeader:@"Accept" value:@"application/json"];

    NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
    [parameters setObject:[NSString stringWithFormat:@"%f,%f", coordinate.latitude, coordinate.longitude] forKey:@"origin"];
    [parameters setObject:[NSString stringWithFormat:@"%f,%f", endCoordinate.latitude, endCoordinate.longitude] forKey:@"destination"];
    [parameters setObject:@"true" forKey:@"sensor"];

    NSMutableURLRequest *request = [_httpClient requestWithMethod:@"GET" path: @"maps/api/directions/json" parameters:parameters];
    request.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];    
    [operation  setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject){
        NSInteger statusCode = operation.response.statusCode;

        if (statusCode == 200)
        {
            NSLog(@"Success: %@", operation.responseString);
        }
        else
        {
            NSLog(@"Status code = %d", statusCode);
        }
    }
                                      failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                          NSLog(@"Error: %@",  operation.responseString);

                                      }
     ];

    [_httpClient enqueueHTTPRequestOperation:operation];

これは問題なく動作します。これを実行して LA とシカゴの間のルートを表示しようとすると、次のようになります。

LA - シカゴ ルートのズームアウト

しかし。マップをストリート レベルにズームすると、ルートは次のようになります。

LA - シカゴ ルートのズームイン

マップがズームされているときに、私が描いているルートが通りをたどる方法を知っている人はいますか? 通りの正確な経路を表示するルートが必要です。Google へのリクエストにパラメータを追加する必要があるかどうかわかりません。

どんな助けやアドバイスも素晴らしいでしょう。よろしくお願いします!


[編集 #1: リクエスト URL と Google からのレスポンスを追加]

上記のコードから操作オブジェクトを作成した後のリクエスト URL は次のようになります。

http://maps.googleapis.com/maps/api/directions/json?sensor=true&destination=34%2E052360,-118%2E243560&origin=41%2E903630,-87%2E629790

その URL をブラウザーに貼り付けるだけで、Google が応答として送信する JSON データが表示されます。このデータは、私のコードでも取得されます。


[編集 #2: Google からの回答の解析とパスの構築]

- (void)parseResponse:(NSData *)response
{
    NSDictionary *dictResponse = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableContainers error:nil];
    NSArray *routes = [dictResponse objectForKey:@"routes"];
    NSDictionary *route = [routes lastObject];

    if (route)
    {
        NSString *overviewPolyline = [[route objectForKey: @"overview_polyline"] objectForKey:@"points"];
        _path = [self decodePolyLine:overviewPolyline];
    }
}

- (NSMutableArray *)decodePolyLine:(NSString *)encodedStr
{
    NSMutableString *encoded = [[NSMutableString alloc] initWithCapacity:[encodedStr length]];
    [encoded appendString:encodedStr];
    [encoded replaceOccurrencesOfString:@"\\\\" withString:@"\\"
                                options:NSLiteralSearch
                                  range:NSMakeRange(0, [encoded length])];
    NSInteger len = [encoded length];
    NSInteger index = 0;
    NSMutableArray *array = [[NSMutableArray alloc] init];
    NSInteger lat=0;
    NSInteger lng=0;

    while (index < len)
    {
        NSInteger b;
        NSInteger shift = 0;
        NSInteger result = 0;

        do
        {
            b = [encoded characterAtIndex:index++] - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);

        NSInteger dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
        lat += dlat;
        shift = 0;
        result = 0;

        do
        {
            b = [encoded characterAtIndex:index++] - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);

        NSInteger dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
        lng += dlng;
        NSNumber *latitude = [[NSNumber alloc] initWithFloat:lat * 1e-5];
        NSNumber *longitude = [[NSNumber alloc] initWithFloat:lng * 1e-5];

        CLLocation *location = [[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]];
        [array addObject:location];
    }

    return array;
}
4

2 に答える 2

1

Google がすべてのポイントを提供していないか、すべてのポイントを見ていないようです。実際には、あなたが持っているように見える目印だけでなく、目印の間にポリラインがあると思います(直線で)。

  • 応答の DirectionsStatus をチェックして、制限されているかどうかを確認します
  • Google が送り返す json データを提供します。

Google が使用しているものとは根本的に異なるメルカトル図法を使用しているとは確信が持てません。

于 2012-10-22T20:39:30.683 に答える
0

MapKit で使用される投影法は、Google マップで使用される投影法とは異なると思います。 MapKit は Cylindrical Mercator使用しますが、Google は Mercator Projection のバリアントを使用します

座標系間の変換 通常は緯度と経度の値を使用してマップ上のポイントを指定しますが、他の座標系との間で変換する必要がある場合があります。たとえば、通常はオーバーレイの形状を指定するときにマップ ポイントを使用します。

アップルから引用:

于 2012-10-22T16:03:08.930 に答える