繁体   English   中英

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

[英]Programmatically open Maps app in iOS 6

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

NSURL *url = [NSURL URLWithString:@"http://maps.google.com/?q=New+York"];
[[UIApplication sharedApplication] openURL:url];

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

这是官方的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];
}

如果您想获得驾驶或步行指令,您可以在+openMapsWithItems:launchOptions:中的数组中包含带有mapItemForCurrentLocationMKMapItem ,并相应地设置启动选项。

// 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];
}

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

最后,如果您有想要路线的地址(作为字符串),请使用地理编码器通过CLPlacemark创建MKPlacemark

// 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];

    }];
}

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

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

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

最好的方法是在MKMapItem openInMapsWithLaunchOptions:launchOptions上调用新的iOS 6方法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];

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

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

您感兴趣的两种方法:

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

这是一个使用在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)
}

我发现使用http://maps.apple.com?q= ...链接设置首先在较旧的设备上打开Safari浏览器很烦人。

因此,对于iOS 5设备,通过引用maps.apple.com打开您的应用程序,步骤如下:

  1. 你点击应用程序中的内容,它指的是maps.apple.com网址
  2. safari打开链接
  3. maps.apple.com服务器重定向到maps.google.com网址
  4. maps.google.com网址会被解释并打开Goog​​le地图应用。

我认为(非常明显和令人困惑的)第2步和第3步令用户烦恼。 因此,我检查操作系统版本,并在设备上运行maps.google.com或maps.apple.com(对于分别为ios 5或ios 6 OS版本)。

我对这个问题的研究使我得出以下结论:

  1. 如果您使用maps.google.com,那么它将在safari中为每个ios打开地图。
  2. 如果您使用maps.apple.com,那么它将在ios 6的地图应用程序中打开地图,并且还可以使用ios 5进行操作,而在ios 5中,它会在safari中正常打开地图。

如果你想打开谷歌地图,而不是(或提供作为第二选项),你可以使用comgooglemaps://comgooglemaps-x-callback://记录URL方案在这里

在启动url之前,从网址中删除任何特殊字符,并用+替换空格。 这将为您省去一些麻烦:

    NSString *mapURLStr = [NSString stringWithFormat: @"http://maps.apple.com/?q=%@",@"Limmattalstrasse 170, 8049 Zürich"];

    mapURLStr = [mapURLStr stringByReplacingOccurrencesOfString:@" " withString:@"+"];
    NSURL *url = [NSURL URLWithString:[mapURLStr stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
    if ([[UIApplication sharedApplication] canOpenURL:url]){
            [[UIApplication sharedApplication] openURL:url];
        }
NSString *address = [NSString stringWithFormat:@"%@ %@ %@ %@"
                             ,[dataDictionary objectForKey:@"practice_address"]
                             ,[dataDictionary objectForKey:@"practice_city"]
                             ,[dataDictionary objectForKey:@"practice_state"]
                             ,[dataDictionary objectForKey:@"practice_zipcode"]];


        NSString *mapAddress = [@"http://maps.apple.com/?q=" stringByAppendingString:[address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

        NSLog(@"Map Address %@",mapAddress);

        [objSpineCustomProtocol setUserDefaults:mapAddress :@"webSiteToLoad"];

        [self performSegueWithIdentifier: @"provider_to_web_loader_segue" sender: self];

// VKJ

根据@ PJeremyMalouf的回答更新到Swift 4:

private func navigateUsingAppleMaps(to coords:CLLocation, locationName: String? = nil) {
    let placemark = MKPlacemark(coordinate: coords.coordinate, addressDictionary:nil)
    let mapItem = MKMapItem(placemark: placemark)
    mapItem.name = locationName
    let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]
    let currentLocationMapItem = MKMapItem.forCurrentLocation()

    MKMapItem.openMaps(with: [currentLocationMapItem, mapItem], launchOptions: launchOptions)
}

不使用地图,只是以编程方式使用UiButton动作,这对我来说非常有用。

// Button triggers the map to be presented.

@IBAction func toMapButton(sender: AnyObject) {

//Empty container for the value

var addressToLinkTo = ""

//Fill the container with an address

self.addressToLinkTo = "http://maps.apple.com/?q=111 Some place drive, Oak Ridge TN 37830"

self.addressToLinkTo = self.addressToLinkTo.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!

let url = NSURL(string: self.addressToLinkTo)
UIApplication.sharedApplication().openURL(url!)

                }

您可以稍微传播一些代码。 例如,我将变量作为类级变量,让另一个函数填充它,然后按下按钮时,只需获取变量中的内容并将其擦除即可在URL中使用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM