【问题标题】:how to check if Mapkit is available or not?如何检查 Mapkit 是否可用?
【发布时间】:2012-12-05 16:12:14
【问题描述】:

我正在构建一个使用 Mapkit 的应用程序。我知道这仅在IOS6中可用。 所以我应该检查这是否可用。我正在使用以下代码。

  if(NSClassFromString(@"MKMapKit")) {
        // MKMapKit is available in this OS
        CLLocationCoordinate2D coords =
        CLLocationCoordinate2DMake(51.097185,5.621653);

        NSDictionary *address = @{
        (NSString *)kABPersonAddressStreetKey: @"Weg naar oqdffds 59",
        (NSString *)kABPersonAddressCityKey: @"Msfsf",
        (NSString *)kABPersonAddressStateKey: @"Limbusqfqsdf",
        (NSString *)kABPersonAddressZIPKey: @"3670",
        (NSString *)kABPersonAddressCountryCodeKey: @"BE",
        (NSString *)kABPersonPhoneMainLabel:@"04741234567"
        };
        MKPlacemark *place = [[MKPlacemark alloc]
                              initWithCoordinate:coords addressDictionary:address];

        MKMapItem *mapItem = [[MKMapItem alloc]initWithPlacemark:place];
        mapItem.phoneNumber = @"0141343252";

        //current location
        MKMapItem *mapItem2 = [MKMapItem mapItemForCurrentLocation];


        NSArray *mapItems = @[mapItem, mapItem2];

        NSDictionary *options = @{
            MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,
            MKLaunchOptionsMapTypeKey:
            [NSNumber numberWithInteger:MKMapTypeStandard],
            MKLaunchOptionsShowsTrafficKey:@YES
        };

        [MKMapItem openMapsWithItems:mapItems launchOptions:options];


    }else {
        NSLog(@"tot hier");
        // MKMapKit is not available in this OS
        locationController = [[MyCLController alloc] init];
        locationController.delegate = self;
        [locationController.locationManager startUpdatingLocation];
    }

但由于某种原因,它总是使用 google 方法。

谁能帮忙!

【问题讨论】:

    标签: iphone objective-c ios google-maps mkmapview


    【解决方案1】:

    如前所述,MapKit 在 iOS 6 之前就已经可用。

    您要检查的是MKMapItem(不是“MKMapKit”)。

    但是,作为documentation for MKMapItem explains(带有代码示例):

    在给定的 iOS 中确定一个类在运行时是否可用 发布时,您通常会检查该类是否为 nil。很遗憾, 这个测试对于 MKMapItem 来说并不完全准确。 虽然这门课 从 iOS 6.0 开始公开可用,正在开发中 在此之前。 虽然该类存在于早期版本中,但您 不应尝试在这些版本中使用它。

    在运行时确定您是否可以在您的 应用程序,测试类和 openMapsWithItems:launchOptions: 类方法存在。那个方法是 直到 iOS 6.0 才添加到课程中。代码可能看起来像 以下:

    Class itemClass = [MKMapItem class]; 
    if (itemClass && [itemClass 
        respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) {   
        // Use class 
    }
    

    所以这个检查:

    if(NSClassFromString(@"MKMapKit")) {
    

    应该是:

    Class itemClass = [MKMapItem class]; 
    if (itemClass && [itemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) {
    

    或:

    Class itemClass = NSClassFromString(@"MKMapItem"); 
    if (itemClass && [itemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) {
    

    【讨论】:

      【解决方案2】:

      MkMapKit 在 ios 4.3 中也可用,可能在 3.x 中也可用! 什么是新的,是(就像在所有版本中一样),MkMapKit 的一些新方法:

      您应该更好地检查您需要的特定方法 (地理编码?=

      查看您正在导入的 MkMapKit 的标头(如果我没记错的话:MkMapKit.h),有定义可用性的宏 具体方法,取决于ios版本。

      【讨论】:

        猜你喜欢
        • 2011-12-02
        • 2013-04-03
        • 1970-01-01
        • 1970-01-01
        • 2017-10-10
        • 1970-01-01
        • 1970-01-01
        • 2012-10-12
        • 1970-01-01
        相关资源
        最近更新 更多