GPSLocation

来源:互联网 发布:cctv网络直播电视 编辑:程序博客网 时间:2024/05/17 01:29
  • locationManager:didUpdateLocations:代理方法拿到的location不是准确的,偏移后的地理位置
  • MKMapViewDelegate方法mapView: didUpdateUserLocation:拿到的userLocation是准确的,没有经过偏移处理

详情参考https://www.v2ex.com/t/11666和http://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