iOS 自带定位最新获取 街区名字 、街道名字、城市名字、省份等,并获取当经纬度。
2017-03-01 10:24
711 查看
首先,我们又带.h文件导入官方的头文件
#import <MapKit/MapKit.h>
接下来要遵守它的协议
<CLLocationManagerDelegate>
接下来在创建它的对象
@property (strong,
nonatomic) CLLocationManager* locationManager;
接下来我们要在.m文件中,写入以下代码
//开始定位
-(void)startLocation{
self.locationManager = [[CLLocationManager
alloc]
init];
self.locationManager.delegate
= self;
self.locationManager.desiredAccuracy
= kCLLocationAccuracyBest;
self.locationManager.distanceFilter
= 100.0f;
if ([[[UIDevice
currentDevice]systemVersion]doubleValue]
> 8.0){//iOS 8.0以后调用
[self.locationManager
requestWhenInUseAuthorization];
}
if ([[UIDevice
currentDevice].systemVersion
floatValue] >= 8.0) {//iOS 8.0以后调用
_locationManager.allowsBackgroundLocationUpdates =
YES;
}
[self.locationManager
startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
switch (status) {
case
kCLAuthorizationStatusNotDetermined:
if ([self.locationManager
respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[self.locationManager
requestWhenInUseAuthorization];
}break;
default:break;
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation
*> *)locations {
CLLocation *newLocation = locations[0];
CLLocationCoordinate2D oldCoordinate = newLocation.coordinate;
NSLog(@"旧的经度:%f,旧的纬度:%f",oldCoordinate.longitude,oldCoordinate.latitude);
[manager stopUpdatingLocation];
CLGeocoder *geocoder = [[CLGeocoder
alloc]init];
[geocoder reverseGeocodeLocation:newLocation
completionHandler:^(NSArray<CLPlacemark *> *
_Nullable placemarks,
NSError * _Nullable error) {
for (CLPlacemark *place
in placemarks) {
// NSLog(@"name,%@",place.name); //
位置名
//
// NSLog(@"thoroughfare,%@",place.thoroughfare); //
街道
//
// NSLog(@"subThoroughfare,%@",place.subThoroughfare); //
子街道
//
// NSLog(@"locality,%@",place.locality); //
市
//
// NSLog(@"subLocality,%@",place.subLocality); //
区
//
// NSLog(@"country,%@",place.country); //
国家
if ([JudgeIDAndBankCard
isEmptyOrNull:place.locality]) {
_gpsCityName=@"定位失败";
}
WRITE_DATA(place.locality,
@"CITY_JC_NAME");
}
[self.mytableview
reloadData];
}];
}
这些都完成后,我们还要做一件非常重要的事情,我们要在info.plist文件配置一下文件
info.plist文件也需要加两个字段:
Privacy - Location Always Usage Description:类型可以设置为字符串
Privacy - Location When In Use Usage Description :类型可以设置为字符串
#import <MapKit/MapKit.h>
接下来要遵守它的协议
<CLLocationManagerDelegate>
接下来在创建它的对象
@property (strong,
nonatomic) CLLocationManager* locationManager;
接下来我们要在.m文件中,写入以下代码
//开始定位
-(void)startLocation{
self.locationManager = [[CLLocationManager
alloc]
init];
self.locationManager.delegate
= self;
self.locationManager.desiredAccuracy
= kCLLocationAccuracyBest;
self.locationManager.distanceFilter
= 100.0f;
if ([[[UIDevice
currentDevice]systemVersion]doubleValue]
> 8.0){//iOS 8.0以后调用
[self.locationManager
requestWhenInUseAuthorization];
}
if ([[UIDevice
currentDevice].systemVersion
floatValue] >= 8.0) {//iOS 8.0以后调用
_locationManager.allowsBackgroundLocationUpdates =
YES;
}
[self.locationManager
startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
switch (status) {
case
kCLAuthorizationStatusNotDetermined:
if ([self.locationManager
respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[self.locationManager
requestWhenInUseAuthorization];
}break;
default:break;
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation
*> *)locations {
CLLocation *newLocation = locations[0];
CLLocationCoordinate2D oldCoordinate = newLocation.coordinate;
NSLog(@"旧的经度:%f,旧的纬度:%f",oldCoordinate.longitude,oldCoordinate.latitude);
[manager stopUpdatingLocation];
CLGeocoder *geocoder = [[CLGeocoder
alloc]init];
[geocoder reverseGeocodeLocation:newLocation
completionHandler:^(NSArray<CLPlacemark *> *
_Nullable placemarks,
NSError * _Nullable error) {
for (CLPlacemark *place
in placemarks) {
// NSLog(@"name,%@",place.name); //
位置名
//
// NSLog(@"thoroughfare,%@",place.thoroughfare); //
街道
//
// NSLog(@"subThoroughfare,%@",place.subThoroughfare); //
子街道
//
// NSLog(@"locality,%@",place.locality); //
市
//
// NSLog(@"subLocality,%@",place.subLocality); //
区
//
// NSLog(@"country,%@",place.country); //
国家
if ([JudgeIDAndBankCard
isEmptyOrNull:place.locality]) {
_gpsCityName=@"定位失败";
}
WRITE_DATA(place.locality,
@"CITY_JC_NAME");
}
[self.mytableview
reloadData];
}];
}
这些都完成后,我们还要做一件非常重要的事情,我们要在info.plist文件配置一下文件
info.plist文件也需要加两个字段:
Privacy - Location Always Usage Description:类型可以设置为字符串
Privacy - Location When In Use Usage Description :类型可以设置为字符串
相关文章推荐
- iOS定位获取当前所在经纬度,进而获得具体地址(省市地区街道)
- iOS定位获取当前所在经纬度,进而获得具体地址(省市地区街道)
- iOS定位获取当前所在经纬度,进而获得具体地址(省市地区街道)
- Android GSP定位获取经纬度并显示所在城市的名字(不用引入各种SDK)
- iOS 自带定位CLLocationManager获取经纬度以及城市名称
- IOS 地图定位,标记位置,获取经纬度,MKMapView的使用
- 通过经纬度获取城市yahoo天气预报(最新2013-4-17)
- ios获取当前城市 ,位置,坐标,经纬度
- ios 定位经纬度获取位置信息,地理位置编码与反编码
- iOS定位获取城市名(ios自学笔记)
- iOS定位获取经纬度以及计算位置
- windows phone 7 定位(获取经纬度),然后找到经纬度所在的位置(城市信息)
- iOS定位服务与地图开发(1)---获取经纬度
- ios项目开发(天气预报项目):通过经纬度获取当前城市名称
- iOS开发——定位 获取经纬度
- iOS 开发 如何获得用户当前位置(通过经纬度获得城市、省份等信息)
- ios 根据经纬度定位城市
- iOS开发:通过经纬度获得城市、省份等信息 推荐
- IOS 地图定位,标记位置,获取经纬度,MKMapView的使用
- ios 通过经纬度定位城市,通过城市名定位经纬度