CoreLocation+MapKit系统定位(含坐标以及详细地址)

iOS8 之后出现一些新的配置

[self.manager requestWhenInUseAuthorization];
 并且在info.plist文件中增加
 NSLocationWhenInUseUsageDescription  BOOL YES
 NSLocationAlwaysUsageDescription         string “提示描述”
记得加依赖库
CoreLocation.framework MapKit.framework

创建MapView

if (_mapView == nil) {
    _mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 0, 0.5, 0.5)];
    //        _mapView.userTrackingMode = MKUserTrackingModeFollow;
    _mapView.delegate = self;
    _mapView.showsUserLocation = YES;
    [self.view addSubview:_mapView];
}

创建locationManager

if ([CLLocationManager locationServicesEnabled] == YES) {//判断定位是否可用
    _locationManager = [[CLLocationManager alloc]init];
    _locationManager.delegate = self;
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;//定位精确度
    _locationManager.distanceFilter = 10; //位置变化10米更新位置信息
    //NSLog(@"定位");
   
    if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
        [_locationManager requestWhenInUseAuthorization];
}else{
    //NSLog(@"定位不可用");
}

 [_locationManager startUpdatingLocation];

CLLocationManager Delegate

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
    //NSLog(@"%s",__FUNCTION__);
    [manager stopUpdatingLocation];
    NSLog(@"newLocation = %@",newLocation);
    NSLog(@"oldLocation = %@",oldLocation);
    //获取经度和纬度的结构体
    CLLocationCoordinate2D coordinate = newLocation.coordinate;
    //纬度信息,CLLocationDegrees类型实际上就是double型
    CLLocationDegrees latitude = coordinate.latitude;
    //经度信息,CLLocationDegrees类型实际上就是double型
    CLLocationDegrees longitude = coordinate.longitude;
    NSLog(@"%f,%f",latitude,longitude);
    
    [self.latitude setText:[NSString stringWithFormat:@"纬度:%lf",latitude]];
    [self.longitude setText:[NSString stringWithFormat:@"经度:%lf",longitude]];
  /*
    
    // 获取当前所在的城市名
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    //根据经纬度反向地理编译出地址信息
    [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *array, NSError *error)
     {
         if (array.count > 0)
         {
             CLPlacemark *placemark = [array objectAtIndex:0];
             //详细信息
             [self.address setText:[NSString stringWithFormat:@"地址:%@",placemark.name]];
             //获取城市
             NSString *city = placemark.locality;
             if (!city) {
             //四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)
                 city = placemark.administrativeArea;
             }
             [self.city setText:[NSString stringWithFormat:@"城市:%@",city]];
         }
         else if (error == nil && [array count] == 0)
         {
             NSLog(@"No results were returned.");
         }
         else if (error != nil)
         {
             NSLog(@"An error occurred = %@", error);
         }
     }];
    */
}

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    NSLog(@"定位失败  ,%@",error);
}

MapView获得详细地址

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
    
    
    CLLocation * newLocation = userLocation.location;
    self.lastCoordinate=mapView.userLocation.location.coordinate;
    
    NSUserDefaults *standard = [NSUserDefaults standardUserDefaults];
    
    [standard setObject:@(self.lastCoordinate.longitude) forKey:MMLastLongitude];
    [standard setObject:@(self.lastCoordinate.latitude) forKey:MMLastLatitude];
    
    CLGeocoder *clGeoCoder = [[CLGeocoder alloc] init];
    CLGeocodeCompletionHandler handle = ^(NSArray *placemarks,NSError *error)
    {
        for (CLPlacemark * placeMark in placemarks)
        {
            NSDictionary *addressDic=placeMark.addressDictionary;
            
            NSString *City=[addressDic objectForKey:@"City"];
//            NSString *subLocality=[addressDic objectForKey:@"SubLocality"];
//            NSString * adressName  = [NSString stringWithFormat:@"%@%@",subLocality,street];
            [self.city setText:[NSString stringWithFormat:@"城市:%@",City]];
            
             [self.address setText:[NSString stringWithFormat:@"地址:%@",placeMark.name]];
            [self stopLocation];
        }
        
    };
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    [clGeoCoder reverseGeocodeLocation:newLocation completionHandler:handle];
    
}

停止定位

-(void)stopLocation
{
    if (_mapView) {
        NSLog(@"关闭");
        _mapView.showsUserLocation = NO;
        [_mapView removeFromSuperview];
        _mapView = nil;
    }
}

  

 

来源: http://www.cnblogs.com/spaceID/p/4992167.html

 

转载于:https://www.cnblogs.com/spaceID/p/4992167.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值