您的位置:首页 > 移动开发 > IOS开发

IOS简单的定位

2016-04-07 14:24 316 查看
@property (nonatomic,strong) CLLocationManager *locationManager;

@property (nonatomic,strong) CLGeocoder *geoCoder;

if([CLLocationManager locationServicesEnabled]){

self.locationManager = [[CLLocationManager alloc]init];

_locationManager.desiredAccuracy = kCLLocationAccuracyBest;

_locationManager.delegate =self;

//ios8.0以上

if ([[UIDevice currentDevice].systemVersion floatValue]>=8.0) {

[_locationManager requestAlwaysAuthorization];

}

//开始获取位置

[_locationManager startUpdatingLocation];

//获取当前的时间

NSDate *nowDate = [NSDate date];

NSDateFormatter *formatter = [[NSDateFormatter alloc]init];

formatter.dateFormat = @"yy/MMM/dd EE aa H:mm;ss";

NSString *formatDateString = [formatter stringFromDate:nowDate];

NSLog(@"%@",formatDateString);

}

}

//定位失败

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(nonnull NSError *)error{

}

//定位在一直更新

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{

//暂停位置更新

[_locationManager stopUpdatingLocation];

NSLog(@"%@",locations);

//获取最新的location

CLLocation *newLocation = [locations lastObject];

//将location 反编码

self.geoCoder = [[CLGeocoder alloc]init];

[_geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

//获取placeMark

CLPlacemark *placeMark = [placemarks lastObject];

NSLog(@"%@",placeMark.name);

}];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

地图导航“
CLLocationCoordinate2D endCoor = CLLocationCoordinate2DMake(self.latitude, self.longitude);//目的地经纬度

MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];//用户当前位置

MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:endCooraddressDictionary:nil]];

toLocation.name = self.titleOnMark;//目的地名称

[MKMapItem openMapsWithItems:@[currentLocation, toLocation];

launchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: