在用户点击“方向”按钮后,我正在使用MKMapItem
从我的应用程序中启动地图应用程序。地图应用程序很好地显示了从当前位置到地址的方向。但是,如果我不想再查看路线,如何返回我的应用程序?下面是我附加到的代码IBAction
。
代码:
- (IBAction)pressDirectionsButton:(id)sender {
Class mapItemClass = [MKMapItem class];
if (mapItemClass && [mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)])
{
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
NSString *addressString = [NSString stringWithFormat:@"%@,%@,%@,%@,%@",
self.currentlySelectedTemple.houseNumber,
self.currentlySelectedTemple.street,
self.currentlySelectedTemple.city,
self.currentlySelectedTemple.state,
self.currentlySelectedTemple.country];
[geocoder geocodeAddressString:addressString completionHandler:^(NSArray *placemarks, NSError *error) {
//Convert CLPlacemark to an MKPlacemark
CLPlacemark *geocodedPlacemark = [placemarks objectAtIndex:0];
MKPlacemark *placemark = [[MKPlacemark alloc]
initWithCoordinate:geocodedPlacemark.location.coordinate addressDictionary:geocodedPlacemark.addressDictionary];
//Create a map item for geocoded address to pass to Maps app
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
[mapItem setName:geocodedPlacemark.name];
//Set Directions mode to Driving
NSDictionary *launchOptions = @{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving};
//Get the "Current User Locations" MKMapItem
MKMapItem *currentLocationMapItem = [MKMapItem mapItemForCurrentLocation];
//Pass the current location and destination map items to Maps app
[MKMapItem openMapsWithItems:@[currentLocationMapItem, mapItem] launchOptions:launchOptions];
}];
}
}