美文网首页
IOS-定位(显示城市)

IOS-定位(显示城市)

作者: DoubleMoonBird | 来源:发表于2017-11-16 15:58 被阅读0次

1.导入框架

Xcode中添加 CoreLocation.framework

2.info.plist文件添加描述

3.导入头文件

#import <CoreLocation/CoreLocation.h>

4.声明管理者和城市

{    

NSString * currentCity; //当前城市

}

@property (nonatomic, strong) CLLocationManager *locationManager;

5.初始化

self.locationManager= [[CLLocationManageralloc]init];

self.locationManager.delegate=self;

[_locationManagerrequestWhenInUseAuthorization];

currentCity= [[NSStringalloc]init];

6.开始定位

判断定位是否打开

- (void)locate {    

//判断定位功能是否打开    

if ([CLLocationManager locationServicesEnabled]) {         NSLog(@"开始定位");        

[self.locationManager startUpdatingLocation];    

}    

定位未开启,去打开定位

UIAlertController* alertVC = [UIAlertControlleralertControllerWithTitle:@"允许\"定位\"提示"message:@"请在设置中打开定位"preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction* ok = [UIAlertActionactionWithTitle:@"打开定位"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*_Nonnullaction) {

//打开定位设置

NSURL*settingsURL = [NSURLURLWithString:UIApplicationOpenSettingsURLString];

[[UIApplicationsharedApplication]openURL:settingsURL];

}];

UIAlertAction* cancel = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:^(UIAlertAction*_Nonnullaction) {

}];

[alertVCaddAction:cancel];

[alertVCaddAction:ok];

[selfpresentViewController:alertVCanimated:YEScompletion:nil];

7.代理方法

        -(void)locationManager:(CLLocationManager*)manager didUpdateLocations:(NSArray *)locations

    1.获取经纬度

CLLocation*currentLocation = [locationslastObject];

CLLocationCoordinate2Dcoordinate = currentLocation.coordinate;

NSLog(@"纬度%f 经度%f",coordinate.latitude,coordinate.longitude);

    2.获取城市

CLGeocoder* geoCoder = [[CLGeocoderalloc]init];

//反编码

[geoCoderreverseGeocodeLocation:currentLocationcompletionHandler:^(NSArray *_Nullableplacemarks,NSError*_Nullableerror) {

if(placemarks.count>0) {

CLPlacemark*placeMark = placemarks[0];

currentCity= placeMark.locality;

if(!currentCity) {

currentCity=@"无法定位当前城市";

}

//NSLog(@"%@",currentCity); //这就是当前的城市

//NSLog(@"%@",placeMark.name);//具体地址:xx市xx区xx街道

_cityLabel.text=currentCity;

_placeLabel.text= placeMark.name;

}

elseif(error ==nil&& placemarks.count==0) {

NSLog(@"No location and error return");

}

elseif(error) {

NSLog(@"location error: %@ ",error);

}

}];

8.Git地址

github.com/baipeng1990/BPMapLocationDemo.git

相关文章

  • IOS-定位(显示城市)

    1.导入框架 Xcode中添加 CoreLocation.framework 2.info.plist文件添加描述...

  • JFCitySelector轻量、灵活、可自定义的三级城市选择器

    简介 JFCitySelector最开始只是自己学习时写的demo(请看:iOS-(仿美团)城市选择器+自动定位+...

  • iOS-定位当前城市

    1.导入框架 import 2.定义对象 @prope...

  • iOS-定位

    使用地图的CLLocationManager完成定位效果.例子:定位当前所在位置,把所在城市的城市名显示在labe...

  • 02 - 微信小程序实例开发 - 天气情况

    本文章来自【知识林】欢迎访问【微信小程序专题】 实例主要功能 自动定位所在城市 根据所定位的城市获取天气信息 显示...

  • iOS-定位

    一、前言 二、定位权限 1、iOS8.* -前台定位 -需要在info.plist配置NSLocationWhen...

  • iOS 百度地图方便Mac下打开

    本篇文章记录了: 引入百度地图API 如何显示地图并定位 如何定位获取经纬度 如何通过定位得到城市,国家,街道等信...

  • iOS-定位服务

    过年后第一次来上班,那么我们来说说iOS上的定位服务首先说定位共分三种方法,第一利用WiFi,第二是移动蜂窝网络,...

  • iOS-定位功能

  • 10.CSS定位

    显示形式 示例图片 显示图层 示例图片 相对定位 运行图片 绝对定位 示例图片 固定定位 示例图片 浮动定位 图1...

网友评论

      本文标题:IOS-定位(显示城市)

      本文链接:https://www.haomeiwen.com/subject/omglvxtx.html