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]}];
@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]}];
相关文章推荐
- iOS - 解决短时间多次点击按钮,多次响应的问题
- iOS绘图 - 自定义柱状图绘制
- iOS8中的动态文本
- (转载)iOS开发系列--音频播放、录音、视频播放、拍照、视频录制
- iOS 打包ipa之Missing iOS Distribution signing identity问题
- iOS OC KVC KVO 实现原理详解
- iOS绘图 - 基本线条的绘制
- iOS开发:协议与代理
- iOS SDK开发
- iOS开源弹幕库BarrageRenderer
- iOS 证书的事
- iOS图片裁剪和小图看大图动画
- IOS delegate 实例
- iOS 获取通讯录里边的电话号码AddressBook
- iOS解决键盘弹出遮挡输入框问题
- YouKu iOS笔试题二
- YouKu iOS笔试题一
- RNCryptor IOS文件加密解密
- iOS部分知识点小结
- 关于iOS的强引用,弱引用及strong,retain,copy,weak,assignd的关系