iOS 定位坐标

2019-05-27  本文已影响0人  码农耕

1.CoreLocation:CLLocationManager

/**
 *  懒加载
 */
- (CLLocationManager *)locationManager
{
    if (!_locationManager) {
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.delegate = self;
        // 定位精准度
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        // 重新定位的距离
        _locationManager.distanceFilter = 50.0f;
    }
    return _locationManager;
}

/**
 *  开始加载
 */
- (void)startLocation
{
    // 判断定位操作是否被允许
    if (![CLLocationManager locationServicesEnabled]) {
        return;
    }
    // ios8后需要向用户请求权限
    [self.locationManager requestWhenInUseAuthorization];
    [self.locationManager requestAlwaysAuthorization];
    // 开始定位
    [self.locationManager startUpdatingLocation];
}


/**
 *  代理方法获取到地理位置
 */
- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray<CLLocation *> *)locations
{
        CLLocation *location = [locations lastObject];
    CLLocationCoordinate2D coor = location.coordinate;
    //    NSLog(@"纬度:%.6f 经度%.6f", coor.latitude, coor.longitude);
    NSString *x1 = [NSString stringWithFormat:@"%f", coor.longitude];
    NSString *y1 = [NSString stringWithFormat:@"%f", coor.latitude];
    
    [self getAddressWithCoordinate: coor];
}

#pragma mark - 经纬度转地址
- (void)getAddressWithCoordinate:(CLLocationCoordinate2D)coor
{
    if (coor.latitude == 0 || coor.longitude == 0){
        NSLog(@"经纬度为空");
        return;
    }
    
    // 获取当前所在的城市名
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    CLLocation *newLocation = [[CLLocation alloc] initWithLatitude:coor.latitude longitude:coor.longitude];
    //根据经纬度反向地理编译出地址信息
    [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *array, NSError *error)
     {
         if (array.count > 0)
         {
             CLPlacemark *placemark = array.firstObject;
             
             NSLog(@"%@",placemark);
             
             //获取城市
             NSString *city = placemark.locality;
             if (!city) {
                 //四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)
                 city = placemark.administrativeArea;
             }
         }
         
     }];
}

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

注意:国际经纬度坐标标准为WGS-84,国内必须至少使用国测局制定的GCJ-02,对地理位置进行首次加密。百度坐标在此基础上,进行了BD-09二次加密措施,更加保护了个人隐私。百度对外接口的坐标系并不是GPS采集的真实经纬度,需要通过坐标转换接口进行转换。

2.MKMapView<MKMapViewDelegate>

-(void)startLocation
{
    if (_mapView) {
        _mapView = nil;
    }
    
    _mapView = [[MKMapView alloc] init];
    _mapView.delegate = self;
    _mapView.showsUserLocation = YES;
}

-(void)stopLocation
{
    _mapView.showsUserLocation = NO;
    _mapView = nil;
}

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    CLLocation * newLocation = userLocation.location;

    CLGeocoder *clGeoCoder = [[CLGeocoder alloc] init];
    CLGeocodeCompletionHandler handle = ^(NSArray *placemarks,NSError *error)
    {
        for (CLPlacemark * placeMark in placemarks)
        {
            NSDictionary *addressDic=placeMark.addressDictionary;
            
            NSString *state=[addressDic objectForKey:@"State"];
            NSString *city=[addressDic objectForKey:@"City"];
            NSString *subLocality=[addressDic objectForKey:@"SubLocality"];
            NSString *street=[addressDic objectForKey:@"Street"];
            
            [self stopLocation];
        }
        
    };
    
    [clGeoCoder reverseGeocodeLocation:newLocation completionHandler:handle];
}

- (void)mapView:(MKMapView *)mapView didFailToLocateUserWithError:(NSError *)error
{
    [self stopLocation];
}

实测使用MKMapView获取到的地址是比较精准的。

最后别忘记添加隐私权限:
requestAlwaysAuthorization : 请求获得应用一直使用定位服务授权,注意使用此方法前要在info.plist中配置NSLocationAlwaysUsageDescription
requestWhenInUseAuthorization : 请求获得应用使用时的定位服务授权,注意使用此方法前在要在info.plist中配置NSLocationWhenInUseUsageDescription

上一篇下一篇

猜你喜欢

热点阅读