【问题标题】:Opens apple maps app from ios app with directions从带有方向的 ios 应用程序打开苹果地图应用程序
【发布时间】:2014-03-25 20:49:19
【问题描述】:

正如标题所述,我想通过按一个按钮从我自己的应用程序中打开 ios 设备中的本机地图应用程序。目前我使用了一个MKmapview,它显示了一个简单的针脚,其纬度/经度取自json 文件。

代码是这样的:

- (void)viewDidLoad {
    [super viewDidLoad]; 
}


// We are delegate for map view
self.mapView.delegate = self;

// Set title
self.title = self.location.title;

// set texts...
self.placeLabel.text = self.location.place;


self.telephoneLabel.text = self.location.telephone;
self.urlLabel.text = self.location.url;


**// Make a map annotation for a pin from the longitude/latitude points
MapAnnotation *mapPoint = [[MapAnnotation alloc] init];
mapPoint.coordinate = CLLocationCoordinate2DMake([self.location.latitude doubleValue], [self.location.longitude doubleValue]);
mapPoint.title = self.location.title;**

// Add it to the map view
[self.mapView addAnnotation:mapPoint];

// Zoom to a region around the pin
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(mapPoint.coordinate, 500, 500);
[self.mapView setRegion:region];

}`

当您触摸图钉时,会出现一个带有标题和信息按钮的信息框。

这是代码:

    #pragma mark - MKMapViewDelegate

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    MKPinAnnotationView *view = nil;
    static NSString *reuseIdentifier = @"MapAnnotation";

    // Return a MKPinAnnotationView with a simple accessory button
    view = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIdentifier];
    if(!view) {
        view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
        view.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        view.canShowCallout = YES;
        view.animatesDrop = YES;
    }

    return view;
}

我想创建一个方法,当单击上面信息框中的按钮时,打开地图应用程序,其中包含从当前用户位置到上面 mapPoint 的路线。 那可能吗?我还可以自定义此按钮的外观吗? (我的意思是在按钮上放一张不同的图片,让它看起来像“按我的方向”)。

对不起,如果这是一个愚蠢的问题,但这是我的第一个 ios 应用程序,Obj-c 对我来说是一种全新的语言。

提前感谢所有回复。

【问题讨论】:

标签: ios iphone objective-c maps apple-maps


【解决方案1】:

这是打开地图应用程序的代码:

Objective-C 代码

NSString* directionsURL = [NSString stringWithFormat:@"http://maps.apple.com/?saddr=%f,%f&daddr=%f,%f",self.mapView.userLocation.coordinate.latitude, self.mapView.userLocation.coordinate.longitude, mapPoint.coordinate.latitude, mapPoint.coordinate.longitude];
if ([[UIApplication sharedApplication] respondsToSelector:@selector(openURL:options:completionHandler:)]) {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString: directionsURL] options:@{} completionHandler:^(BOOL success) {}];
} else {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString: directionsURL]];
}

mapPoint 是你想要去的地方。

Swift3 或更高版本

let directionsURL = "http://maps.apple.com/?saddr=35.6813023,139.7640529&daddr=35.4657901,139.6201192"
guard let url = URL(string: directionsURL) else {
    return
}
if #available(iOS 10.0, *) {
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
    UIApplication.shared.openURL(url)
}

注意saddrdaddrurlencoded位置名称或位置坐标(about encode URL look at here)的字符串。

directionsURL 示例:

// directions with location coordinate
"http://maps.apple.com/?saddr=35.6813023,139.7640529&daddr=35.4657901,139.6201192"
// or directions with location name
"http://maps.apple.com/?saddr=Tokyo&daddr=Yokohama"
// or directions from current location to destination location
"http://maps.apple.com/?saddr=Current%20Location&daddr=Yokohama"

更多选项参数(如传输类型、地图类型...)看here

【讨论】:

  • 它会是 maps.apple.com,因为 ios6 (maps.google.com 可能会工作!苹果不想刹车。)anwyays 我会说最好使用提到的 api michael
  • 我试图这样做。我在详细信息视图中的按钮上添加了一个 -(IBAction)。地图应用程序打开,但我收到一条消息“方向不可用”。我使用了这段代码: - (IBAction)navigate{ NSString* url = [NSString stringWithFormat:@"maps.apple.com/…, self.mapView.userLocation.coordinate.longitude, self.location.latitude, self.location.longitude]; [[ UIApplication sharedApplication] openURL: [NSURL URLWithString: url]]; }
  • 实际上它有效,但我需要用 %@ 更改 %f。非常感谢!
  • 很好的答案,通过短信或邮件发送位置很有用
  • 为我工作。但请注意,Current%20Location 应该是百分之一。否则 URL 将无法正确解析。
【解决方案2】:

这是在 Apple 地图上显示方向的工作代码。它适用于当前地点到您的目的地,您只需要传递目的地的纬度和经度。

double destinationLatitude, destinationLongitude;
destinationLatitude=// Latitude of destination place.
destinationLongitude=// Longitude of destination place.

Class mapItemClass = [MKMapItem class];

if (mapItemClass && [mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)])
{
    // Create an MKMapItem to pass to the Maps app
    CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(destinationLatitude,destinationLongitude);
    MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil];

    MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
    [mapItem setName:@"Name/text on destination annotation pin"];

    // Set the directions mode to "Driving"
    // Can use MKLaunchOptionsDirectionsModeDriving 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];
}

另外,如果发现任何问题或者我们需要找到另一种方法来解决问题,请在此处与我分享。

【讨论】:

    【解决方案3】:

    您可以使用以下代码打开带方向的地图:(假设您的 id 类有一个名为“coordinate”的 CLLocationCoordinate2D 公共属性)

    MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:[annotation coordinate] addressDictionary:nil];
    MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
    [mapItem setName:"WhereIWantToGo"]];
    NSDictionary *options = @{MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving};
    [mapItem openInMapsWithLaunchOptions:options];
    

    您也可以更改按钮,实际上您使用的是标准样式按钮:

    [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    

    但您可以使用图像或标签分配自定义按钮:

    [[UIButton alloc] initWithImage:[UIImage imageNamed:"directionIcon.png"]];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-23
      相关资源
      最近更新 更多