ios定位到具体道路

ios自带定位系统,使用系统定位到具体的经纬度,然后根据反地理编码编译出对应的城市区以及道路等,废话不多说,直接上代码。
 1.先导入定位类库  CoreLocation.frameWork
 2.然后在需要定位的类导入头文件
 #import 
3.实现代理
4.调用方法开始定位
//初始化定位类,直接viewDidLoad调用,然后实现代理
-(void)location{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy =   kCLLocationAccuracyBest;
}

#pragma mark - 定位方法,如果一开始就需要定位就调用,否则就点击事件调用
- (void)findMe{
/** 由于IOS8中定位的授权机制改变 需要进行手动授权
 * 获取授权认证,两个方法:

 */
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager requestAlwaysAuthorization];

if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
    
    NSLog(@"requestAlwaysAuthorization");
    
    [self.locationManager requestAlwaysAuthorization];
}
//开始定位,不断调用其代理方法
[self.locationManager startUpdatingLocation];   
}

//定位的代理方法
- (void)locationManager:(CLLocationManager *)manager
 didUpdateLocations:(NSArray *)locations{
// 最后一个值为最新位置
CLLocation *currentLocation = [locations lastObject];
self.coordinate = currentLocation.coordinate;
CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
// 根据经纬度反向得出位置城市信息
[geoCoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
    if (placemarks.count > 0) {
        CLPlacemark *placeMark = placemarks[0];
        self.locationCurrentCity = placeMark.locality;
        
        if (!self.locationCurrentCity) {
            //四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)
            self.locationCurrentCity = placeMark.administrativeArea;
        }
        self.locationCurrentSubLocality = placeMark.subLocality;
        NSLog(@"name = %@",placeMark.name);
        NSLog(@"Country = %@", placeMark.country);
        NSLog(@"Postal Code = %@", placeMark.postalCode);
        NSLog(@"locality = %@", placeMark.locality);
        NSLog(@"subLocality = %@", placeMark.subLocality);
        NSLog(@"address = %@",placeMark.name);
        NSLog(@"administrativeArea = %@",placeMark.administrativeArea);
        NSLog(@"subAdministrativeArea = %@",placeMark.subAdministrativeArea);
        NSLog(@"ISOcountryCode = %@",placeMark.ISOcountryCode);
        NSLog(@"thoroughfare = %@", placeMark.thoroughfare);
        NSLog(@"subThoroughfare = %@",placeMark.subThoroughfare);
        
    } else if (error == nil && placemarks.count == 0) {
        NSLog(@"No location and error returned");
    } else if (error) {
        NSLog(@"Location error: %@", error);
    }
    
    return ;
    
}];

//停止定位
[self.locationManager stopUpdatingLocation];

}
//错误方法
- (void)locationManager:(CLLocationManager *)manager
   didFailWithError:(NSError *)error{
   if (error.code == kCLErrorDenied) {
    // 提示用户出错原因,可按住Option键点击 KCLErrorDenied的查看更多出错信息,可打印error.code值查找原因所在
   }
}

然后别忘了顶部描述一下几个全局属性
@property (nonatomic, assign) CLLocationCoordinate2D     coordinate;//定位信息
@property (nonatomic,retain) NSString*locationCurrentCity;//当前城市
@property (nonatomic,retain) NSString*locationCurrentSubLocality;//当前城市的哪个区
基本定位功能就就实现了,如果需要重新定位就在点击事件调用开始定位方法即可:
[self.locationManager startUpdatingLocation];   
另外需要注意的是需要在infoPlist文件中开启地理位置授权的几个权限,模拟器是无法定位的,需要手动给一个经纬度,在debug里面最后一个location默认选择的None,改为custom location 然后填写一个经纬度即可。

你可能感兴趣的:(ios定位到具体道路)