正如标题所述,我想通过按下按钮在我自己的应用程序中打开ios设备中的本机地图应用程序。目前我使用MKmapview
显示一个简单的引脚,其中lat / long取自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对我来说是一种全新的语言。
提前感谢所有回复。
答案 0 :(得分:39)
您初始化按钮并向按钮添加操作:
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)
}
请注意,saddr
,daddr
可以是位置名称或位置坐标。所以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
答案 1 :(得分:15)
您可以使用以下代码打开带方向的地图:(假设您的id&lt; MKAnnotation&gt;类具有名为“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"]];
答案 2 :(得分:8)
以下是在Apple地图上显示路线的工作代码。它适用于您目的地的当前位置,您只需要通过lat&amp; amp;目的地长。
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];
}
此外,如果发现任何问题或者我们需要找到另一种方法,请与我分享。