从ios app打开带有路线的苹果地图应用程序 [英] Opens apple maps app from ios app with directions
问题描述
正如标题所述,我想通过按下按钮在我自己的应用程序中打开ios设备中的本机地图应用程序。目前我使用 MKmapview
显示一个简单的引脚,其中lat / long取自 json
文件。
As the title states I would like to open the native maps app in the ios device from within my own app, by pressing a button. currently I have used an MKmapview
that displays a simple pin with lat/long taken from a json
file.
代码如下:
- (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];
}`
当你触摸将显示带有标题和信息按钮的信息框。
When you touch the pin an info box appears with a title and an info button.
这是代码:
#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;
}
我想创建一个方法,打开地图应用程序,其中包含来自Current的指示用户在上面的mapPoint上的位置,当点击上面信息框中的按钮时。
这可能吗?我也可以自定义这个按钮的外观吗? (我的意思是喜欢在按钮上放置一个不同的图像,使其看起来像按我指示方向)。
I want to make a method that opens the maps app with directions from the Current users location to the mapPoint above , when clicking the button in the info box above. Is that possible? Also Can I customize the look of this button? (i mean like putting a different image to the button to make it look like "press me for direction kinda" ).
对不起,如果这是一个愚蠢的问题,但这是我的第一个ios应用程序和Obj-c对我来说是一种全新的语言。
Sorry if this a stupid question, but this is my first ios app and Obj-c is a totally new language to me.
感谢所有的回复。
推荐答案
您初始化按钮并向按钮添加操作:
You init a button and add action to button:
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是您要前往的地方。
with the mapPoint is where you want to direction to.
Swift2代码
if let url = NSURL(string: directionsURL) {
UIApplication.sharedApplication().openURL(url)
}
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)
}
请注意 saddr
, daddr
可以是位置名称或位置坐标。所以 directionsURL
可以是:
Note that saddr
, daddr
can be location name or location coordinate. So directionsURL
can be:
// 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"
更多选项参数(如传输类型,地图类型......)看看这里
More options parameters (like transport type, map type ...) look at here
这篇关于从ios app打开带有路线的苹果地图应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!