以编程方式在iOS 6中打开地图应用

Tom*_*ing 159 objective-c mapkit ios ios6

在iOS 6之前,打开这样的URL会打开(Google)地图应用:

NSURL *url = [NSURL URLWithString:@"http://maps.google.com/?q=New+York"];
[[UIApplication sharedApplication] openURL:url];
Run Code Online (Sandbox Code Playgroud)

现在有了新的Apple Maps实现,这只会将Mobile Safari打开到Google Maps.如何在iOS 6中实现相同的行为?如何以编程方式打开地图应用并将其指向特定位置/地址/搜索/其他?

nev*_*ing 281

这是官方的Apple方式:

// Check for iOS 6
Class mapItemClass = [MKMapItem class];
if (mapItemClass && [mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) 
{
    // Create an MKMapItem to pass to the Maps app
    CLLocationCoordinate2D coordinate = 
                CLLocationCoordinate2DMake(16.775, -3.009);
    MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:coordinate 
                                            addressDictionary:nil];
    MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
    [mapItem setName:@"My Place"];
    // Pass the map item to the Maps app
    [mapItem openInMapsWithLaunchOptions:nil];
}
Run Code Online (Sandbox Code Playgroud)

如果你想获得驾车或步行指示的位置,你可以包括mapItemForCurrentLocationMKMapItem在数组中+openMapsWithItems:launchOptions:,并设置适当的启动选项.

// Check for iOS 6
Class mapItemClass = [MKMapItem class];
if (mapItemClass && [mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) 
{
    // Create an MKMapItem to pass to the Maps app
    CLLocationCoordinate2D coordinate = 
                CLLocationCoordinate2DMake(16.775, -3.009);
    MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:coordinate 
                                            addressDictionary:nil];
    MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
    [mapItem setName:@"My Place"];

    // Set the directions mode to "Walking"
    // Can use MKLaunchOptionsDirectionsModeDriving instead
    NSDictionary *launchOptions = @{MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeWalking};
    // Get the "Current User Location" MKMapItem
    MKMapItem *currentLocationMapItem = [MKMapItem mapItemForCurrentLocation];
    // Pass the current location and destination map items to the Maps app
    // Set the direction mode in the launchOptions dictionary
    [MKMapItem openMapsWithItems:@[currentLocationMapItem, mapItem] 
                    launchOptions:launchOptions];
}
Run Code Online (Sandbox Code Playgroud)

之后,您可以在else声明中保留原始iOS 5及更低版本的代码if.请注意,如果您反转openMapsWithItems:数组中项目的顺序,您将获得从坐标当前位置的路线.您可以通过传递构造MKMapItem而不是当前位置图项来使用它来获取任意两个位置之间的方向.我没试过.

最后,如果你有一个地址(字符串)要方向,使用地理编码器来创建MKPlacemark,通过的方式CLPlacemark.

// Check for iOS 6
Class mapItemClass = [MKMapItem class];
if (mapItemClass && [mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)])
{
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder geocodeAddressString:@"Piccadilly Circus, London, UK" 
        completionHandler:^(NSArray *placemarks, NSError *error) {

        // Convert the CLPlacemark to an MKPlacemark
        // Note: There's no error checking for a failed geocode
        CLPlacemark *geocodedPlacemark = [placemarks objectAtIndex:0];
        MKPlacemark *placemark = [[MKPlacemark alloc]
                                  initWithCoordinate:geocodedPlacemark.location.coordinate
                                  addressDictionary:geocodedPlacemark.addressDictionary];

        // Create a map item for the geocoded address to pass to Maps app
        MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
        [mapItem setName:geocodedPlacemark.name];

        // Set the directions mode to "Driving"
        // Can use MKLaunchOptionsDirectionsModeWalking instead
        NSDictionary *launchOptions = @{MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving};

        // Get the "Current User Location" MKMapItem
        MKMapItem *currentLocationMapItem = [MKMapItem mapItemForCurrentLocation];

        // Pass the current location and destination map items to the Maps app
        // Set the direction mode in the launchOptions dictionary
        [MKMapItem openMapsWithItems:@[currentLocationMapItem, mapItem] launchOptions:launchOptions];

    }];
}
Run Code Online (Sandbox Code Playgroud)

  • 如何从地图应用导航到我当前的应用程序? (2认同)

Tom*_*ing 80

找到了我自己的问题的答案.Apple 在此处记录其地图URL格式.它看起来像你基本上可以代替maps.google.commaps.apple.com.

更新:事实证明在iOS 6上的MobileSafari中也是如此; 点击链接以http://maps.apple.com/?q=...使用该搜索打开地图应用,与http://maps.google.com/?q=...以前的版本相同.这有效,并记录在上面链接的页面中.

更新:这回答了我关于URL格式的问题.但是nevan king 在这里的答案(见下文)是对实际Maps API的一个很好的总结.


zvo*_*cek 41

最好的方法是调用新的iOS 6方法 MKMapItem openInMapsWithLaunchOptions:launchOptions

例:

CLLocationCoordinate2D endingCoord = CLLocationCoordinate2DMake(40.446947, -102.047607);
MKPlacemark *endLocation = [[MKPlacemark alloc] initWithCoordinate:endingCoord addressDictionary:nil];
MKMapItem *endingItem = [[MKMapItem alloc] initWithPlacemark:endLocation];

NSMutableDictionary *launchOptions = [[NSMutableDictionary alloc] init];
[launchOptions setObject:MKLaunchOptionsDirectionsModeDriving forKey:MKLaunchOptionsDirectionsModeKey];

[endingItem openInMapsWithLaunchOptions:launchOptions];
Run Code Online (Sandbox Code Playgroud)

这将启动从当前位置驾驶的导航.


Fil*_*lic 7

我看到你发现了maps.apple.com url"scheme".这是一个不错的选择,因为它会自动将旧设备重定向到maps.google.com.但对于iOS 6,您可能希望利用一个新类:MKMapItem.

您感兴趣的两种方法:

  1. -openInMapsWithLaunchOptions: - 在MKMapItem实例上调用它以在Maps.app中打开它
  2. + openMapsWithItems:launchOptions: - 在MKMapItem类上调用它以打开MKMapItem实例的数组.

  • @ Mr.Jefferson设置MKMapItem的name属性 (5认同)

PJe*_*ouf 5

这是一个使用在Swift中完成的nevan king的解决方案的类:

class func openMapWithCoordinates(theLon:String, theLat:String){

            var coordinate = CLLocationCoordinate2DMake(CLLocationDegrees(theLon), CLLocationDegrees(theLat))

            var placemark:MKPlacemark = MKPlacemark(coordinate: coordinate, addressDictionary:nil)

            var mapItem:MKMapItem = MKMapItem(placemark: placemark)

            mapItem.name = "Target location"

            let launchOptions:NSDictionary = NSDictionary(object: MKLaunchOptionsDirectionsModeDriving, forKey: MKLaunchOptionsDirectionsModeKey)

            var currentLocationMapItem:MKMapItem = MKMapItem.mapItemForCurrentLocation()

            MKMapItem.openMapsWithItems([currentLocationMapItem, mapItem], launchOptions: launchOptions)
}
Run Code Online (Sandbox Code Playgroud)