UIKitiPhone硬件

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

2017-07-07  本文已影响44人  泼茶_

前言

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

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

CoreLocation库的头文件

CoreLocation库的所有头文件

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.
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, 或简书作者

上一篇 下一篇

猜你喜欢

热点阅读