#pragma mark
创建
actionSheet
-( void )navigationCompletion:( NSNotification *)notification {
// 接受 notification 的 userInfo ,可以把参数存进此变量
NSDictionary *theData = [notification userInfo ];
NSString *latitude = [theData objectForKey : @"latitude" ];
NSString *longitude = [theData objectForKey : @"longitude" ];
NSLog ( @"latitude===%@,longitude===%@" ,latitude,longitude);
[ self availableMapsApps :latitude longitude :longitude];
UIActionSheet *action = [[ UIActionSheet alloc ] init ];
[action addButtonWithTitle : @" 使用系统自带地图导航 " ];
for ( NSDictionary *dic in self . availableMaps ) {
[action addButtonWithTitle :[ NSString stringWithFormat : @" 使用 %@ 导航 " , dic[ @"name" ]]];
}
[action addButtonWithTitle : @" 取消 " ];
action. cancelButtonIndex = self . availableMaps . count + 1 ;
action. delegate = self ;
[action showInView : self . view ];
}
- ( void )availableMapsApps:( NSString *)latitude longitude:( NSString *)longitude{
[ self . availableMaps removeAllObjects ];
CLLocationCoordinate2D startCoor = self . mapView . userLocation . location . coordinate ;
NSLog ( @"startCoor.latitude===%f,startCoor.latitude===%f" ,startCoor. latitude ,startCoor. longitude );
CLLocationCoordinate2D endCoor = CLLocationCoordinate2DMake (latitude. floatValue + 0.01 , longitude. floatValue + 0.01 );
NSString *toName = @" AppName " ;
if ([[ UIApplication sharedApplication ] canOpenURL :[ NSURL URLWithString : @"baidumap://map/" ]]){
NSString *urlString = [ NSString stringWithFormat : @"baidumap://map/direction?origin=latlng:%f,%f|name: 我的位置 &destination=latlng:%f,%f|name:%@&mode=transit" ,
latitude. floatValue , longitude. floatValue , endCoor. latitude , endCoor. longitude , toName];
NSDictionary *dic = @{ @"name" : @" 百度地图 " ,
@"url" : urlString } ;
[ self . availableMaps addObject :dic];
}
if ([[ UIApplication sharedApplication ] canOpenURL :[ NSURL URLWithString : @"iosamap://" ]]) {
NSString *urlString = [ NSString stringWithFormat : @"iosamap://navi?sourceApplication=%@&backScheme=applicationScheme&poiname=fangheng&poiid=BGVIS&lat=%f&lon=%f&dev=0&style=3" ,
@"AppN ame " , endCoor. latitude , endCoor. longitude ];
NSDictionary *dic = @{ @"name" : @" 高德地图 " ,
@"url" : urlString } ;
[ self . availableMaps addObject :dic];
-( void )navigationCompletion:( NSNotification *)notification {
// 接受 notification 的 userInfo ,可以把参数存进此变量
NSDictionary *theData = [notification userInfo ];
NSString *latitude = [theData objectForKey : @"latitude" ];
NSString *longitude = [theData objectForKey : @"longitude" ];
NSLog ( @"latitude===%@,longitude===%@" ,latitude,longitude);
[ self availableMapsApps :latitude longitude :longitude];
UIActionSheet *action = [[ UIActionSheet alloc ] init ];
[action addButtonWithTitle : @" 使用系统自带地图导航 " ];
for ( NSDictionary *dic in self . availableMaps ) {
[action addButtonWithTitle :[ NSString stringWithFormat : @" 使用 %@ 导航 " , dic[ @"name" ]]];
}
[action addButtonWithTitle : @" 取消 " ];
action. cancelButtonIndex = self . availableMaps . count + 1 ;
action. delegate = self ;
[action showInView : self . view ];
}
- ( void )availableMapsApps:( NSString *)latitude longitude:( NSString *)longitude{
[ self . availableMaps removeAllObjects ];
CLLocationCoordinate2D startCoor = self . mapView . userLocation . location . coordinate ;
NSLog ( @"startCoor.latitude===%f,startCoor.latitude===%f" ,startCoor. latitude ,startCoor. longitude );
CLLocationCoordinate2D endCoor = CLLocationCoordinate2DMake (latitude. floatValue + 0.01 , longitude. floatValue + 0.01 );
NSString *toName = @" AppName " ;
if ([[ UIApplication sharedApplication ] canOpenURL :[ NSURL URLWithString : @"baidumap://map/" ]]){
NSString *urlString = [ NSString stringWithFormat : @"baidumap://map/direction?origin=latlng:%f,%f|name: 我的位置 &destination=latlng:%f,%f|name:%@&mode=transit" ,
latitude. floatValue , longitude. floatValue , endCoor. latitude , endCoor. longitude , toName];
NSDictionary *dic = @{ @"name" : @" 百度地图 " ,
@"url" : urlString } ;
[ self . availableMaps addObject :dic];
}
if ([[ UIApplication sharedApplication ] canOpenURL :[ NSURL URLWithString : @"iosamap://" ]]) {
NSString *urlString = [ NSString stringWithFormat : @"iosamap://navi?sourceApplication=%@&backScheme=applicationScheme&poiname=fangheng&poiid=BGVIS&lat=%f&lon=%f&dev=0&style=3" ,
@"AppN ame " , endCoor. latitude , endCoor. longitude ];
NSDictionary *dic = @{ @"name" : @" 高德地图 " ,
@"url" : urlString } ;
[ self . availableMaps addObject :dic];
}
/*
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]) {
NSString *urlString = [NSString stringWithFormat:@"comgooglemaps://?saddr=&daddr=%f,%f¢er=%f,%f&directionsmode=transit", endCoor.latitude, endCoor.longitude, startCoor.latitude, startCoor.longitude];
NSDictionary *dic = @{@"name": @"Google Maps",
@"url": urlString};
[self.availableMaps addObject:dic];
}
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]) {
NSString *urlString = [NSString stringWithFormat:@"comgooglemaps://?saddr=&daddr=%f,%f¢er=%f,%f&directionsmode=transit", endCoor.latitude, endCoor.longitude, startCoor.latitude, startCoor.longitude];
NSDictionary *dic = @{@"name": @"Google Maps",
@"url": urlString};
[self.availableMaps addObject:dic];
}
*/
}
#pragma mark -actionSheet Delegate
- ( void )actionSheet:( UIActionSheet *)actionSheet clickedButtonAtIndex:( NSInteger )buttonIndex {
if (buttonIndex == 0 ) {
CLLocationCoordinate2D startCoor = self . mapView . userLocation . location . coordinate ;
CLLocationCoordinate2D endCoor = CLLocationCoordinate2DMake (startCoor. latitude + 0.01 , startCoor. longitude + 0.01 );
MKMapItem *currentLocation = [ MKMapItem mapItemForCurrentLocation ];
MKPlacemark *placemark = [[ MKPlacemark alloc ] initWithCoordinate :endCoor addressDictionary : nil ];
MKMapItem *toLocation = [[ MKMapItem alloc ] initWithPlacemark :placemark];
toLocation. name = @"to name" ;
[ MKMapItem openMapsWithItems : @[ currentLocation, toLocation ]
launchOptions : @{ MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving , MKLaunchOptionsShowsTrafficKey : [ NSNumber numberWithBool : YES ] } ];
} else if (buttonIndex < self . availableMaps . count + 1 ) {
NSDictionary *mapDic = self . availableMaps [buttonIndex- 1 ];
NSString *urlString = mapDic[ @"url" ];
urlString = [urlString stringByAddingPercentEscapesUsingEncoding : NSUTF8StringEncoding ];
NSURL *url = [ NSURL URLWithString :urlString];
BTLog ( @"\n%@\n%@\n%@" , mapDic[ @"name" ], mapDic[ @"url" ], urlString);
[[ UIApplication sharedApplication ] openURL :url];
}
#pragma mark -actionSheet Delegate
- ( void )actionSheet:( UIActionSheet *)actionSheet clickedButtonAtIndex:( NSInteger )buttonIndex {
if (buttonIndex == 0 ) {
CLLocationCoordinate2D startCoor = self . mapView . userLocation . location . coordinate ;
CLLocationCoordinate2D endCoor = CLLocationCoordinate2DMake (startCoor. latitude + 0.01 , startCoor. longitude + 0.01 );
MKMapItem *currentLocation = [ MKMapItem mapItemForCurrentLocation ];
MKPlacemark *placemark = [[ MKPlacemark alloc ] initWithCoordinate :endCoor addressDictionary : nil ];
MKMapItem *toLocation = [[ MKMapItem alloc ] initWithPlacemark :placemark];
toLocation. name = @"to name" ;
[ MKMapItem openMapsWithItems : @[ currentLocation, toLocation ]
launchOptions : @{ MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving , MKLaunchOptionsShowsTrafficKey : [ NSNumber numberWithBool : YES ] } ];
} else if (buttonIndex < self . availableMaps . count + 1 ) {
NSDictionary *mapDic = self . availableMaps [buttonIndex- 1 ];
NSString *urlString = mapDic[ @"url" ];
urlString = [urlString stringByAddingPercentEscapesUsingEncoding : NSUTF8StringEncoding ];
NSURL *url = [ NSURL URLWithString :urlString];
BTLog ( @"\n%@\n%@\n%@" , mapDic[ @"name" ], mapDic[ @"url" ], urlString);
[[ UIApplication sharedApplication ] openURL :url];
}
}