iOS-去掉定位地址中不准确的楼层信息

最近项目中使用苹果自带的定位,但是解析完,得到的楼层信息不准确,所以想去掉楼层信息,

系统提供的API楼层信息直接包含在街道信息里,既想要街道信息又不想要楼层信息,就只能找偏方了,

于是我就投机取巧, 直接截串了,把后面带xx层的部分截掉. 

下面是我的实现方案


 
- (void )getDetailAddressWithLocation:(CLLocation *)location{
    
    // 反地理编码
    //    CLLocation * loc1 = [[CLLocation alloc] initWithLatitude:39.8 longitude:116.7];
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error)
     {
         
         if (placemarks.count)
         {
             CLPlacemark *placemark = placemarks.firstObject;
             
             NSString *address  = [placemark.addressDictionary[@"FormattedAddressLines"] firstObject];
             
             // 如果是中国 去掉国家信息
             if ([address hasPrefix:@"中国"]) {
                 address = [address substringFromIndex:2];
             }
             
             // 去掉楼层信息
             NSString *lastChar = [address substringFromIndex:address.length-1];
             
             if ([lastChar isEqualToString:@"层"]) {
                 
                 // 去掉层
                 address = [address substringToIndex:address.length-1];
                 lastChar = [address substringFromIndex:address.length-1];
                 // 匹配数字
                 NSString *regex = @"\\d$";
                 NSPredicate *regExPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
                 // 如果最后一个字符是数字(例如xxx3号楼2层)
                 while ((![lastChar isEqualToString:@"楼"]) && address.length > 1 && [regExPredicate evaluateWithObject:lastChar])
                 {
                     address = [address substringToIndex:address.length-1];
                     lastChar = [address substringFromIndex:address.length-1];
                 }
             }
             _addressLabel.text = address;
         }
     }];
}


如果大家有什么比较好的办法,还请多多指教

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值