2

私はドキュメントに少し混乱しています。いくつかの調査と実験の後、これが私が持っているものです。

if ([self canUseMKMapItem]) {
            [self iosTheMap];
        } else {
            [self googleTheMap];
        }

これを使用して、IOS6 マッピング機能を使用できるかどうかを検出します。

- (BOOL) canUseMKMapItem {
    Class itemClass = [MKMapItem class];
    return (itemClass && [itemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]);
}

これは、Google マップを使用した IOS5 用です。現在の住所 (ユーザーが許可している場合) から目的地までの道順のリストが表示された画面が自動的に表示されます。

- (void)googleTheMap
{
    NSNumber *userLat = [[NSNumber alloc]initWithDouble:mapView.userLocation.coordinate.latitude];
    NSNumber *userLong = [[NSNumber alloc]initWithDouble:mapView.userLocation.coordinate.longitude];

    NSMutableString *queryString = [NSMutableString stringWithFormat:@"http://maps.google.com/?saddr=%@,%@&daddr=",userLat,userLong];
    NSString *address = [partnerObject valueForKey:ATTRIBUTE_ADDRESS];
    [queryString appendString:address];
    NSString *escaped = [queryString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:escaped]];
}

ここがトリッキーな部分です-これは、Apple Mapsを使用するために私がやろうとしていることです

- (void)iosTheMap {
    NSNumber * latitude = [partnerObject valueForKey:ATTRIBUTE_LATITUDE];
    NSNumber * longitude = [partnerObject valueForKey:ATTRIBUTE_LONGITUDE];
    CLLocationCoordinate2D coordinate;
    coordinate.latitude = latitude.doubleValue;
    coordinate.longitude = longitude.doubleValue;
    NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];
    [addressDictionary setValue:[partnerObject valueForKey:ATTRIBUTE_ADDRESS] forKey:kABPersonAddressStreetKey];
    MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:addressDictionary];
    MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
    [mapItem openInMapsWithLaunchOptions:nil];
}

これは一種の「機能」です。ユーザーは、住所を示すピンが表示された地図画面に移動します。ユーザーはそれをタップしてルートを取得できます。ただし、このアプローチにはいくつかの留保があります。

  1. kABPersonAddressStreetKey を設定すると、コンパイラの警告が表示されます。
  2. 私が使用している文字列値は完全なアドレスです。住所の値は、より原子的 (通り、都市、州) であることを意図していますが、私はその値を番地に使用します。うまくいくようですが、これは正しい方法ではないのではないかと心配しています。
  3. Apple Maps に住所だけでなく、お店や目的地の名前が表示されると便利です。
  4. 目的地の地図ではなく、Apple Maps が自動的に道案内を表示し、ユーザーが数回タップする手間を省くことができれば素晴らしいと思います。

アプローチを改善する方法について何か提案はありますか? 機能しているように見えますが、正しい方法ではないと思います。

4

1 に答える 1

8

あなたはかなり近いです。MKLaunchOptionsDirectionsModeDriving、MKLaunchOptionsDirectionsModeKey の値とキーを使用して、openInMapsWithLaunchOptions 関数の起動オプション ディクショナリを指定する必要があります。

    Class itemClass = [MKMapItem class];
    if (itemClass && [itemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) {
        // Use iOS 6 maps
        CLLocationCoordinate2D coordinate = [((LocationAnnotation*)[map.annotations objectAtIndex:0]) coordinate];
        MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil];
        MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
        [mapItem openInMapsWithLaunchOptions:[NSDictionary dictionaryWithObjectsAndKeys:MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsDirectionsModeKey, nil]];
    } else {
        //Fall back on google
        ...
    }
于 2012-10-10T02:35:41.363 に答える