iOS地图定位可以定位到的信息

前言

现在我们的项目中都不可避免的需要使用定位功能, 如果你的App只是需要简单的定位, 那么只需要使用原生库CoreLocation就足够了

下面就简单介绍一下通过CoreLocation库可以获得什么地理信息?

CoreLocation库的头文件

  • 下图是CoreLocation库的所有头文件.
CoreLocation库的所有头文件
  • 其中我们可以通过 CLPlacemake.h 暴露出来的属性来获取想要的地理信息.

CLPlacemake.h的一些地理信息的属性

这些属性包含在一个addressDictionary字典里面

/*
 *  addressDictionary
 *  
 *  Discussion:
 *    This dictionary can be formatted as an address using ABCreateStringWithAddressDictionary,
 *    defined in the AddressBookUI framework.
 */
@property (nonatomic, readonly, copy, nullable) NSDictionary *addressDictionary;

下面是各个属性

Apple公司 地址为:

Apple Inc      :公司
Infinite Loop   :街
1               :苹果公司所在街道的门牌号码
Cupertino       :加州圣塔克拉拉县的一个城市 (被誉为硅谷的心脏)
Mission Distric :邻域
CA              :加利福尼亚州
Santa Clara     :县城
95014           :邮政编码
US              :国家简称
United States   :国家名字
Lake Tahoe      :湖 
Pacific Ocean   :太平洋
Golden Gate Park:金门公园

我之前公司所在地为:
中国 北京市 海淀区 海淀街道 北四环西路 001号

@property (nonatomic, readonly, copy, nullable) NSString *name; // eg. Apple Inc.
  • 每一行下面是中国地址各个属性, 可能由于定位的精度问题 所以有些只能po出地址
name; // eg. Apple Inc.
~~北四环西路~~
thoroughfare; // street name, eg. Infinite Loop
北四环西路
subThoroughfare; // eg. 1
001
locality; // city, eg. Cupertino
北京市
subLocality; // neighborhood, common name, eg. Mission District
海淀区
administrativeArea; // state, eg. CA
北京市
subAdministrativeArea; // county, eg. Santa Clara
地址0x0000000000000000
postalCode; // zip code, eg. 95014
地址0x0000000000000000
ISOcountryCode; // eg. US
CN
country; // eg. United States
中国
inlandWater; // eg. Lake Tahoe
地址0x0000000000000000
ocean; // eg. Pacific Ocean
地址0x0000000000000000
*areasOfInterest; // eg. Golden Gate Park
地址0x0000000000000000

使用定位的属性

下面代码只使用了其中的省级(州级)的属性locality

#pragma mark - ``````````````CLLocationManagerDelegate代理方法
// 定位成功后调用这个方法
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
    CLLocation *location = locations.firstObject;
    
    // 纬度
    CLLocationDegrees latitude = location.coordinate.latitude;
    self.latitude = latitude;
    // 经度
    CLLocationDegrees longitude = location.coordinate.longitude;
    self.longitude = longitude;
    
    [self.locationManager stopUpdatingLocation];
    
    CLGeocoder *geocoder = [[CLGeocoder alloc]init];
    
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        
        for (CLPlacemark *place in placemarks) {
            if (! place.locality) {
                
                self.localCity = place.locality;
                if (self.localCity) {
                    self.localCity = [self.localCity substringWithRange:NSMakeRange(0, self.localCity.length)];
                }
            } else {
                self.localCity = place.locality;
                if (self.localCity) {
                    self.localCity = [self.localCity substringWithRange:NSMakeRange(0, self.localCity.length)];
                }
            }
            
            self.areaLbl.text = self.localCity;                
        }
    }];
}

说在后面

如果有疑问, 请联系 1070582397@qq.com, 或简书作者

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容