1.CLLocationManager的使用
1.首先Xcode导入一个自己带的官方头文件在任意一个类.h
#import<MapKit/MapKit.h>
并遵守两协议
<CLLocationManagerDelegate,UIActionSheetDelegate>
在创建
@property (strong,nonatomic)CLLocationManager* locationManager;
double currentLatitude;//定位坐标
double currentLongitude;
double end_currentLongitude;//终点坐标
double end_currentLatitude;
NSString *address;//想去地点的名字
在该类的.m文件直接创建一个方法直接调用创建:
self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = 100.0f;
[self.locationManager startUpdatingLocation];
iOS8.0后加抢了对安全隐私访问权限,所以需要在加上
//请求权限[self.locationManager requestWhenInUseAuthorization];
//定位代理经纬度回调
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
// [self.locationManager stopUpdatingLocation];
NSLog(@"=========================================%@",[NSStringstringWithFormat:@"经度:%3.5f纬度:%3.5f",newLocation.coordinate.latitude,newLocation.coordinate.longitude]);
currentLatitude=newLocation.coordinate.latitude;//纬度
currentLongitude=newLocation.coordinate.longitude;//经度
}
info.plist文件也需要加两个字段
Privacy - Location Always Usage Description:
//
这个在前台、后台都时时定位 如果不使用后台定位,可不添加这个字段,否则
App Store 审核或有问题,如果需要请坐声明描述:GPS在后台持续运行,可以大大降低电池的寿命。
Privacy - Location When In Use Usage Description 类型可以设置为字符串
//
这个在前台时 时时定位
接下来便是应用高德,百度,苹果地图URI的使用
2.
//首先判断定义:
BOOL hasBaiduMap ;//判断是否存在百度地图
BOOL hasGaodeMap ;//判断是否存在高德地图
NSString *address;
//初始化
hasBaiduMap =NO;
hasGaodeMap =NO;
判断存在各种地图需要在info.plist中添加相应的字段:
百度添加:baidumap
高德添加:iosamap
直接调用方法判断或者在点击时间中进行下面判断
if ([[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:@"baidumap://map/"]]){
hasBaiduMap =YES;
}
if ([[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:@"iosamap://"]]){
hasGaodeMap =YES;
}
//判断完成后执行
[self getAllKindsOFMaps];
-(void)getAllKindsOFMaps{
if (hasBaiduMap&&hasGaodeMap ) {
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:address
delegate:self
cancelButtonTitle:@"取消"
destructiveButtonTitle:nil
otherButtonTitles:@"高德地图中导航",@"百度地图中导航",@"苹果地图中导航",nil];
actionSheet.actionSheetStyle =UIActionSheetStyleBlackOpaque;
[actionSheet showInView:self.view];
}elseif (hasBaiduMap){
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:address
delegate:self
cancelButtonTitle:@"取消"
destructiveButtonTitle:nil
otherButtonTitles:@"百度地图中导航",@"苹果地图中导航",nil];
actionSheet.actionSheetStyle =UIActionSheetStyleBlackOpaque;
[actionSheet showInView:self.view];
} else if (hasGaodeMap){
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:address
delegate:self
cancelButtonTitle:@"取消"
destructiveButtonTitle:nil
otherButtonTitles:@"高德地图中导航",@"苹果地图中导航",nil];
actionSheet.actionSheetStyle =UIActionSheetStyleBlackOpaque;
[actionSheet showInView:self.view];
}else{
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:address
delegate:self
cancelButtonTitle:@"取消"
destructiveButtonTitle:nil
otherButtonTitles:@"苹果地图中导航",nil];
actionSheet.actionSheetStyle =UIActionSheetStyleBlackOpaque;
[actionSheet showInView:self.view];
}
}
#pragma mark 打开地图导航
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if (hasBaiduMap&&hasGaodeMap ) {
if (buttonIndex ==0) {
//高德地图中查看
[self openGaoDeMap];
}elseif(buttonIndex ==1) {
//百度地图中查看
[self openBaiDuMap];
} else if(buttonIndex ==2) {
//苹果地图中查看
[self openAppleMap];
}
} else if (hasBaiduMap){
if (buttonIndex ==0) {
//百度地图中查看
[self openBaiDuMap];
} else if(buttonIndex ==1) {
//苹果地图中查看
[self openAppleMap];
}
} else if (hasGaodeMap){
if (buttonIndex ==0) {
[self openGaoDeMap];
//高德地图中查看
} else if(buttonIndex ==1) {
//苹果地图中查看
[self openAppleMap];
}
} else {
if (buttonIndex ==0){
//苹果地图中查看
[self openAppleMap];
}
}
}
- (void)openBaiDuMap{
NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin=latlng:%f,%f|name:我的位置&destination=latlng:%f,%f|name:终点&mode=driving",currentLatitude,currentLongitude,end_currentLatitude,end_currentLongitude]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ;
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlString]];
}
- (void)openGaoDeMap{
NSString *urlString = [[NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&poiname=%@&lat=%f&lon=%f&dev=1&style=2",@"app name", @"EasyToLearn_Coach",@"终点",end_currentLatitude,end_currentLongitude]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication]openURL:[NSURLURLWithString:urlString]];
}
- (void)openAppleMap{
//起点
NSLog(@"%f %f",currentLatitude,currentLongitude);
CLLocationCoordinate2D coords1 =CLLocationCoordinate2DMake(currentLatitude,currentLongitude);
MKMapItem *currentLocation = [[MKMapItem alloc]initWithPlacemark:[[MKPlacemark alloc]initWithCoordinate:coords1addressDictionary:nil]];
//目的地的位置
CLLocationCoordinate2D coords2 =CLLocationCoordinate2DMake(end_currentLatitude,end_currentLongitude);
MKMapItem *toLocation = [[MKMapItem alloc]initWithPlacemark:[[MKPlacemark alloc]initWithCoordinate:coords2addressDictionary:nil]];
toLocation.name =address;
NSArray *items = [NSArray arrayWithObjects:currentLocation, toLocation,nil];
NSDictionary *options =@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsMapTypeKey: [NSNumber numberWithInteger:MKMapTypeStandard],MKLaunchOptionsShowsTrafficKey:@YES};
//打开苹果自身地图应用,并呈现特定的item
[MKMapItem openMapsWithItems:itemslaunchOptions:options];
}
代码实现效果