GPSLocation

1 篇文章 0 订阅
  • locationManager:didUpdateLocations:代理方法拿到的location不是准确的,偏移后的地理位置
  • MKMapViewDelegate方法mapView: didUpdateUserLocation:拿到的userLocation是准确的,没有经过偏移处理

详情参考https://www.v2ex.com/t/11666http://blog.csdn.net/qxuewei/article/details/51611627

// 从给定地名返回经纬度坐标
+ (CLLocation *)getCoordinate2DWithAddressString:(NSString *)string
{
    CLGeocoder *encoder = [[CLGeocoder alloc] init];
    __block CLLocation* location;
    [encoder geocodeAddressString:string completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        if (error != nil || placemarks.count == 0) {
            // 如果有错误信息或者数组中获取地名元素数量为0
            // 说明没有找到
            NSLog(@"error:%@", [error localizedDescription]);
        }else {
            //编码成功,找到了具体位置

            //去除地理信息数组中的第一个CLPlacemark
            NSLog(@"count=%ld", placemarks.count);
            location = [placemarks firstObject].location;
            for (CLPlacemark*mark in placemarks) {

                NSLog(@"纬度:%f, 经度:%f", mark.location.coordinate.latitude, mark.location.coordinate.longitude);

            }

        }
    }];

    return location;
}

华水- - -纬度:34.778751, 经度:113.795870
34.7798580000,113.7943800000
大圣- - -纬度34.765516,经度113.600688
广州塔- - -纬度:23.106487, 经度:113.324587
天安门- - -纬度:39.908692, 经度:116.397477

// 从给定经纬度坐标返回地名
+ (NSDictionary *)getAddressStringWithCoordinate2D:(CLLocation *)location
{
    CLLocationCoordinate2D coordinate;
    // 天安门的经纬度
    coordinate.latitude = 39.908692;
    coordinate.longitude = 116.397477;
    CLLocation *newLocation=[[CLLocation alloc]initWithLatitude:coordinate.latitude longitude: coordinate.longitude];

    CLGeocoder *encoder = [[CLGeocoder alloc] init];
//    if (Coordinate2D == NULL) return NULL;
    __block NSDictionary *addressDic;
    [encoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

        if (error != nil || placemarks.count == 0) {
            // 如果有错误信息或者数组中获取地名元素数量为0
            // 说明没有找到
        }else {

            CLPlacemark *mark = [placemarks firstObject];
            addressDic = mark.addressDictionary;
            NSLog(@"Santa Clara%@", mark.addressDictionary);
        }

    }];

    return addressDic;

}

返回结果

{
    City = Beijing;
    Country = China;
    CountryCode = CN;
    FormattedAddressLines =     (
        "East Chang'an Avenue Dongcheng",
        "Beijing China"
    );
    Name = "Tian'anmen";
    State = Beijing;
    Street = "East Chang'an Avenue";
    SubLocality = Dongcheng;
    Thoroughfare = "East Chang'an Avenue";
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值