当前位置: 首页 > 知识库问答 >
问题:

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

苏彦君
2023-03-14
NSURL *url = [NSURL URLWithString:@"http://maps.google.com/?q=New+York"];
[[UIApplication sharedApplication] openURL:url];

现在有了新的苹果地图实现,这只是向谷歌地图开放了移动Safari。我如何用iOS 6完成同样的行为?我如何以编程方式打开地图应用程序,并让它指向特定的位置/地址/搜索/什么?

共有1个答案

宁侯林
2023-03-14

以下是苹果官方的方式:

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

您可以在if之后的els语句中保留原始的iOS5和更低版本的代码。注意,如果颠倒OpenMapsWithItems:数组中项的顺序,您将得到从坐标到当前位置的方向。通过传递构造的mkmapite而不是当前的位置映射项,您可能可以使用它来获取任意两个位置之间的方向。我没试过。

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

    }];
}
 类似资料:
  • 如果我们使用此代码,我们会看到一条消息:搜索GPS。然而,仅示出了GPS符号;GPS实际上不起作用: 为什么没用?以及如何使其正确工作?

  • 我想通过编程(从关联菜单)打开某个视图,我该怎么做? 我可以使用e3兼容层显示如下视图: e4的方法是什么?

  • 我正在尝试为具有特定扩展名的文件在“打开”菜单中添加eclipse编辑器。我在org.eclipse.ui.editors扩展点中使用launcher实现了这一点,在launcher中我使用了“open editor”方法,它需要传递编辑器ID。有没有什么方法可以让我们以编程方式打开一个编辑器,而不需要传递编辑器ID呢?我们可以通过传递实现编辑器的类的实例以编程方式打开编辑器吗?..

  • 下面给出的错误 process:com.example.rahul.maptask,pid:21986 java.lang.runtimeException:无法启动活动ComponentInfo{com.example.rahul.maptask/com.example.rahul.maptask.mainactivity}:java.lang.nullpointerException:试图在A

  • 我正在尝试使用我自己的应用程序实现或扩展的ResourceConfig或PackageResourceConfig来配置我的Jersey应用程序。因此,我的第一次尝试是将现有的web.xml(实际上,由于开发的库性质,我使用的是web-fragment.xml)配置移植到MyApplication实现。 当我使用第二个版本时,我会收到以下信息 正如您所看到的,是第一个注册的类,但由于它不是公共的,

  • 我可以使用EPartService打开视图或零件(请参见此处)。 这将把最后一个零件堆栈中的零件作为新选项卡显示。 我如何在分离模式下打开该部分?