ios - 从带有方向的 ios 应用程序打开苹果 map 应用程序

标签 ios iphone objective-c maps apple-maps

如标题所述,我想通过按下按钮从我自己的应用程序中打开 ios 设备中的 native map 应用程序。目前,我使用了一个 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;
}

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

抱歉,这是一个愚蠢的问题,但这是我的第一个 ios 应用程序,Obj-c 对我来说是一门全新的语言。

提前感谢所有回复。

最佳答案

以下是打开带方向的 map 应用程序的代码:

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)
}

请注意,saddrdaddr 是位置名称或位置坐标的urlencoded 字符串(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"

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

关于ios - 从带有方向的 ios 应用程序打开苹果 map 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21983559/

相关文章:

ios - 无法理解 Objective-C block 文档

ios - 类型 '()' 不能符合 'View' ;只有结构/枚举/类类型可以符合协议(protocol)

iOS: "Constants.h"错误

ios - Objective-c,对于从 URL 播放视频,AVPlayer 或 MPMovieplayer 哪个更好?

iphone - Apple 是否会修改提交到 App Store 的应用程序的 iOS 应用程序可执行文件?

objective-c - 将 MKMapRect 转换为 CGRect

ios - 使方法在对象上工作

iphone - 确定 iPad 上 UITableView 的大小

ios - 在 RubyMotion 中链接 AdMob SDK 时出现重复符号错误

iphone - 通过 ASIHTTPRequest 下载视频时崩溃?