IOS简单的定位
@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]}];