Open Maps app from Code - Where/How to find the “Current Location”?

后端 未结 4 1452
故里飘歌
故里飘歌 2020-12-15 01:18

I am opening Maps app to show directions from user\'s Current Location to a destination coordinate, from my code. I am using the following code to open the Maps app. I am ca

4条回答
  •  猫巷女王i
    2020-12-15 02:19

    You can use the new MKMapItem class for iOS 6. See the Apple API docs here

    Basically, you will use something like this, if routing to destination coordinates (destCoordinate):

        MKPlacemark* place = [[MKPlacemark alloc] initWithCoordinate: destCoordinate addressDictionary: nil];
        MKMapItem* destination = [[MKMapItem alloc] initWithPlacemark: place];
        destination.name = @"Name Here!";
        NSArray* items = [[NSArray alloc] initWithObjects: destination, nil];
        NSDictionary* options = [[NSDictionary alloc] initWithObjectsAndKeys:
                                     MKLaunchOptionsDirectionsModeDriving, 
                                     MKLaunchOptionsDirectionsModeKey, nil];
        [MKMapItem openMapsWithItems: items launchOptions: options];
    

    In order to support both iOS 6+ and pre iOS 6 in the same code, I'd recommend using something like this code that Apple has on the MKMapItem API doc page:

    Class itemClass = [MKMapItem class];
    if (itemClass && [itemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) {
       // iOS 6 MKMapItem available
    } else {
       // use pre iOS 6 technique
    }
    

    This would assume that your Xcode Base SDK is iOS 6 (or Latest iOS).

    In this other answer, I offer a robust technique for iOS 5.1 and lower

提交回复
热议问题