从ios应用程序打开苹果地图应用程序的方向

作为标题状态,我想通过按下button在我自己的应用程序中打开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]; 

}`

当您触摸该针时,信息框将显示一个标题和一个信息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; } 

我想制作一个方法,当点击上面的信息框中的button时,打开地图应用程序,从当前用户位置到上面的mapPoint的方向。 那可能吗? 我也可以自定义这个button的外观? (我的意思是把一个不同的图像button,使它看起来像“按我的方向有点”)。

对不起,如果这是一个愚蠢的问题,但这是我的第一个iOS应用程序和Obj – C是一个全新的语言给我。

感谢所有的提前回复。

你初始化一个button并添加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是你想要的方向。

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

请注意, saddrdaddr可以是位置名称或位置坐标。 所以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" 

更多的选项参数(如运输types,地图types…)在这里看看

你可以使用下面的代码打开地图:(假设你的id <MKAnnotation>类有一个名为“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]; 

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

 [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 

但是您可以使用图片或标签来分配您的自定义button:

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

这里是在苹果地图上显示方向的工作代码。 它将工作到目前的地方到目的地,你只需要传递拉特和目的地的长。

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

另外,在这里分享我,如果发现任何问题,或者我们需要find另一种方式来做到这一点。