IOS自动定位使用的是高德地图SDK

在高德开放平台http://lbs.amap.com/api/ios-sdk/down/ 下载2D地图SDK和搜索SDK

将SDK导入工程内 按照高德的配置说明进行配置项目

最后需要提醒 在高德的SDK中有些文件夹使用的是快捷方式, 如果你在你本地的工程编译通过, 但可能在你的服务端自动打包的时候不能自动编译通过

需要将那些快捷方式的文件夹用真实文件夹替换掉就可以了。

在工程中导入

#import "MAMapKit/MAMapKit.h"

#import "AMapSearchKit/AMapCommonObj.h"

#import "AMapSearchKit/AMapSearchAPI.h"

在Controller中使用初始化地图服务

  1. #pragma mark MAMAP init AND 定位回调
  2. - (void)initMap
  3. {
  4.  
  5. if (![CLLocationManager locationServicesEnabled]) {
  6. [PXAlertView showAlertWithTitle:@"提示" message:[NSString stringWithFormat:@"请开启定位:设置 > 隐私 > 位置 > 定位服务"] completion:^(BOOL cancelled, NSInteger buttonIndex) {
  7. }];
  8. return;
  9. }
  10. else if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied){
  11. [PXAlertView showAlertWithTitle:@"提示" message:[NSString stringWithFormat:@"定位失败,请开启定位:设置 > 隐私 > 位置 > 定位服务 下 应用"] completion:^(BOOL cancelled, NSInteger buttonIndex) {
  12. }];
  13. return;
  14. }
  15. [MAMapServices sharedServices].apiKey = MAMAP_APPKEY;//高德KEY
  16. _mapView = [[MAMapView alloc] init];
  17. _mapView.delegate = self;
  18. _mapView.showsUserLocation = YES;
  19. //[_mapView setUserTrackingMode:MAUserTrackingModeFollowWithHeading animated:YES];
  20.  
  21. point = [[AMapGeoPoint alloc] init];
  22. _search = [[AMapSearchAPI alloc] initWithSearchKey:MAMAP_APPKEY
  23. Delegate:self];
  24. regeoRequest = [[AMapReGeocodeSearchRequest alloc] init];
  25. regeoRequest.searchType = AMapSearchType_ReGeocode;
  26. regeoRequest.radius = 50;
  27. regeoRequest.requireExtension = YES;
  28. }
  29.  
  30. // 定位回调
  31. - (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation
  32. {
  33. if (updatingLocation) {
  34.  
  35. point.latitude = userLocation.coordinate.latitude;
  36. point.longitude = userLocation.coordinate.longitude;
  37.  
  38. regeoRequest.location = [AMapGeoPoint locationWithLatitude:point.latitude
  39. longitude:point.longitude];
  40. // 发起逆地理编码
  41. [_search AMapReGoecodeSearch:regeoRequest];
  42. _mapView.showsUserLocation =NO;
  43.  
  44. }
  45. }
  46. // 逆地理编码回调
  47. - (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response
  48. {
  49. if (response.regeocode != nil) {
  50.  
  51. NSString *province=response.regeocode.addressComponent.province;
  52. NSString *city=response.regeocode.addressComponent.city;
  53. NSString *district=response.regeocode.addressComponent.district;
  54. NSString *value=nil;
  55. NSString *key=nil;
  56.  
  57. NSMutableArray *cityArrayTemp=nil;
  58. for (AreaModel *model in provinceArray) {
  59. if ([model.value isEqualToString:province]) {
  60. cityArrayTemp=[NSMutableArray arrayWithArray:model.children];
  61. }
  62. }
  63. NSMutableArray *districtArryTemp=nil;
  64. if (cityArrayTemp) {
  65. if (city.length>0) {
  66. for (AreaModel *cityModel in cityArrayTemp) {
  67. if ([cityModel.value isEqualToString:city]) {
  68. districtArryTemp=[NSMutableArray arrayWithArray:cityModel.children];
  69. }
  70. }
  71. }
  72. else{
  73. //直辖市
  74. for (AreaModel *cityModel in cityArrayTemp) {
  75. if ([cityModel.value isEqualToString:district]) {
  76. value=cityModel.value;
  77. key=cityModel.key;
  78. }
  79. }
  80. }
  81. }
  82. if (districtArryTemp) {
  83. for (AreaModel *provinceModel in districtArryTemp) {
  84. if ([provinceModel.value isEqualToString:district]) {
  85. value=provinceModel.value;
  86. key=provinceModel.key;
  87. }
  88. }
  89. }
  90. self.keyCode=key;
  91. [self.checkCityButton setTitle:[NSString stringWithFormat:@"%@%@%@",province,city,district] forState:UIControlStateNormal];
  92.  
  93. }
  94. }

封装省市县 三级选择控件

  1. #import <UIKit/UIKit.h>
  2.  
  3. @interface AreaPickerCheckView : UIView<UIPickerViewDataSource,UIPickerViewDelegate>
  4.  
  5. - (instancetype)initWithFrame:(CGRect)frame andDataSource:(NSArray *)dataSource dismissCompletion:(void (^)(BOOL isCancelClick , NSString *area , NSString *code))completion;
  6.  
  7. @end
  8.  
  9. #import "AreaPickerCheckView.h"
  10. #import "AreaModel.h"
  11.  
  12. @interface AreaPickerCheckView()
  13. {
  14. NSArray *provinceArray;
  15. NSArray *cityArray;
  16. NSArray *areaArray;
  17. void (^completionBlock)(BOOL isCancelClick , NSString *area , NSString *code);
  18. }
  19.  
  20. @property(nonatomic,strong)NSArray *dataSource;
  21. @property(nonatomic,strong)UIPickerView *pickerView;
  22.  
  23. @end
  24.  
  25. @implementation AreaPickerCheckView
  26.  
  27. - (instancetype)initWithFrame:(CGRect)frame andDataSource:(NSArray *)dataSource dismissCompletion:(void (^)(BOOL, NSString *, NSString *))completion
  28. {
  29. self = [super initWithFrame:frame];
  30. if (self) {
  31. completionBlock = [completion copy];
  32. self.dataSource = dataSource;
  33. [self createUI];// Initialization code
  34. }
  35. return self;
  36. }
  37.  
  38. - (void)createUI {
  39.  
  40. provinceArray = self.dataSource;
  41.  
  42. AreaModel *model = [self.dataSource firstObject];
  43.  
  44. cityArray = model.children;
  45.  
  46. AreaModel *model1 = [model.children firstObject];
  47.  
  48. areaArray = model1.children;
  49.  
  50. self.pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 25, self.frame.size.width, self.frame.size.height-25)];
  51. self.pickerView.dataSource = self;
  52. self.pickerView.delegate = self;
  53. [self addSubview:self.pickerView];
  54.  
  55. UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  56. confirmBtn.frame = CGRectMake(Main_Screen_Width - 50, 5, 40, 20);
  57. [confirmBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  58. [confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
  59. [confirmBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
  60. confirmBtn.tag = 100;
  61. [self addSubview:confirmBtn];
  62.  
  63. UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  64. cancelBtn.frame = CGRectMake(10, 5, 40, 20);
  65. cancelBtn.tag = 101;
  66. [cancelBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
  67. [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
  68. [cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  69. [self addSubview:cancelBtn];
  70.  
  71. }
  72.  
  73. #pragma mark dataSouce
  74. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
  75. return 3;
  76. }
  77. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
  78. if (component == 0) {
  79. return provinceArray.count;
  80. }else if (component == 1) {
  81. return cityArray.count;
  82. }
  83. return areaArray.count;
  84. }
  85. #pragma mark delegate
  86. - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
  87. AreaModel *model = nil;
  88. if (component == 0) {
  89. model = provinceArray[row];
  90. }else if (component == 1) {
  91. model = cityArray[row];
  92. }else if (component == 2) {
  93. model = areaArray[row];
  94. }
  95. return model.value;
  96. }
  97.  
  98. - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
  99. if (component == 0) {
  100. AreaModel *model = provinceArray[row];
  101. cityArray = model.children;
  102. areaArray = [[cityArray firstObject] children];
  103.  
  104. [self.pickerView reloadComponent:1];
  105. [self.pickerView reloadComponent:2];
  106.  
  107. }else if (component == 1) {
  108. AreaModel *model = cityArray[row];
  109. areaArray = model.children;
  110. [self.pickerView reloadComponent:2];
  111. }
  112. }
  113.  
  114. - (void)btnClick:(UIButton *)btn {
  115. BOOL isCancel = NO;
  116. switch (btn.tag) {
  117. case 100:
  118.  
  119. break;
  120. case 101:
  121. isCancel = YES;
  122. completionBlock(YES,nil,nil);
  123. return;
  124. break;
  125.  
  126. default:
  127. break;
  128. }
  129.  
  130. NSString *str = nil;
  131. NSString *codeStr = nil;
  132.  
  133. AreaModel *model = provinceArray[[self.pickerView selectedRowInComponent:0]];
  134. str = model.value;
  135. AreaModel *model1 = cityArray[[self.pickerView selectedRowInComponent:1]];
  136. str = [str stringByAppendingString:model1.value];
  137. codeStr = model1.key;
  138. if (areaArray.count > 0) {
  139. AreaModel *model2 = areaArray[[self.pickerView selectedRowInComponent:2]];
  140. str = [str stringByAppendingString:model2.value];
  141. codeStr = model2.key;
  142. }
  143.  
  144. completionBlock(isCancel,str,codeStr);
  145. }
  146.  
  147. @end

在Controller中使用地理位置选择控件

@interface CityCheckController ()<CLLocationManagerDelegate,MAMapViewDelegate,AMapSearchDelegate>

{

NSString *_sessionKey;

NSString *_code;

AMapGeoPoint *point;

AMapReGeocodeSearchRequest *regeoRequest;

NSArray *provinceArray;

}

@property (weak, nonatomic) IBOutlet UITextField *areaTF;

@property(nonatomic,strong) MAMapView *mapView;

@property(nonatomic,strong) AMapSearchAPI *search;

  1. - (void)viewDidLoad {
  2. [super viewDidLoad];
  3.  
  4. NSArray *models = [AreaModel objectArrayWithFilename:@"area.plist"];
  5. provinceArray=models;
  6. AreaPickerCheckView *picker = [[AreaPickerCheckView alloc]initWithFrame:CGRectMake(0, 0, Main_Screen_Width, 216) andDataSource:models dismissCompletion:^(BOOL isCancelClick, NSString *area, NSString *code) {
  7. [self.areaTF resignFirstResponder];
  8. if (isCancelClick) {
  9. return;
  10. }else{
  11. self.areaTF.text = area;
  12. _code = code;
  13. }
  14. }];
  15. self.areaTF.inputView = picker;
  16. [self initMap];
  17.  
  18. }

IOS开发技术交流QQ群:491355147 欢迎加入一起讨论技术哦

IOS实现自动定位和手动选择城市功能的更多相关文章

  1. JS中调用android和ios系统手机打开相机并可选择相册功能

    编写不易,如有转载,请声明出处: 梦回河口:http://blog.csdn.net/zxc514257857/article/details/57626154 实现android手机打开相机选择相册 ...

  2. android EditText长按屏蔽ActionMode context菜单但保留选择工具功能

    最近项目要求屏蔽EditText 长按出来的ActionMode菜单,但是要保留选择文本功能.这个屏蔽百度会出现各种方法,这里说一下我的思路: 1.屏蔽百度可知setCustomSelectionAc ...

  3. IOS开发-OC学习-常用功能代码片段整理

    IOS开发-OC学习-常用功能代码片段整理 IOS开发中会频繁用到一些代码段,用来实现一些固定的功能.比如在文本框中输入完后要让键盘收回,这个需要用一个简单的让文本框失去第一响应者的身份来完成.或者是 ...

  4. iOS 8 中如何集成 Touch ID 功能

    2013年9月,苹果为当时发布的最新iPhone产品配备了一系列硬件升级方案.在iPhone 5s当中,最具创新特性的机制无疑要数围绕Home按钮设计的超薄金属圈,也就是被称为Touch ID的指纹传 ...

  5. iOS AudioSession详解 Category选择 听筒扬声器切换

    在你读这篇文章之前,如果你不嫌读英文太累,推荐阅读下苹果iOS Human Interface Guidelines中Sound这一章.   选择一个Category AVAudioSessionCa ...

  6. h5手机端下拉选择城市

    <!doctype html><html>    <head>            <meta http-equiv="Content-Type& ...

  7. 美团HD(5)-选择城市

    DJSelectCityViewController.m #import "DJSelectCityViewController.h" #import "DJConsta ...

  8. jquery实现输入框聚焦,键盘上下键选择城市

    在最近有个项目中 需要实现当文本框聚焦的时候,可以键盘上下键选择内容,按enter键的时候,把内容传到输入框中,如图所示: 实现代码如下: /** *输入框聚焦,键盘上下键选择城市 */ ;(func ...

  9. 【实用技巧】取消Win7开机账户的手动选择

    因为前面碰到的一些事情,稍有感慨. 关于win7的一些小技巧都不是什么很有技术含量东西,或者说很浅显.我说一个技巧,也许很多人都知道,也许也早有人说过.但我想说的是我不是在炫耀什么,我只是想分享一些我 ...

随机推荐

  1. ifconfig结果说明

  2. 通过shodan搜索相同favicon.ico的网站

    0x01 根据favicon.ico生成hash python2,想改python3折腾了半天不得 import mmh3 import requests response = requests.ge ...

  3. kali 系列学习12-使用Wifite破解无线网络

    一些破解无线网络程序是使用Aircrack-ng工具集,并添加了一个图形界面或使用文本菜单的形式来破解无线网络.这使得用户使用它们更容易,而且不需要记住任何命令.本节将介绍使用命令行工具Wifite, ...

  4. 其实SQL优化调优,就跟吃饭喝水一样简单,教你抓住SQL的本质!

    前言 SOL 优化并不简单,做好 SOL 优化需要掌握数据库体系结构.表和索引设计.高效 SOL法.高级 SOL 语法.多种优化工具等知识,甚至还得分析业务特点,以及了解优化器的缺点.只有建立 SOL ...

  5. 头秃了,使用@AutoConfigureBefore指定配置类顺序竟没生效?

    持续原创输出,点击上方蓝字关注我 前言 日常工作中对于Spring Boot 提供的一些启动器可能已经足够使用了,但是不可避免的需要自定义启动器,比如整合一个陌生的组件,也想要达到开箱即用的效果. 在 ...

  6. 「CSP-S 2020」动物园

    description luogu loj(暂无数据) solution 这道题作为T2,对选手们考试开始后先通看一遍所有题目的好习惯,以及判断究竟谁才是真正的签到题的重要能力进行了较好的锻炼, 特别 ...

  7. Jenkins中如何自定义构造结果

    jenkis中的触发邮件机制是根据构建成功与否来发邮件,实际上我们在jenkis上定时执行脚本时是需要根据用例的执行结果来触发邮件预警 本文讲叙如何根据用例的执行结果来触发邮件预警 1.在任务配置的b ...

  8. CollectionUtils和StringUtils

    1.StringUtils(常用-用来操作String的字符串)1.1 判断某字符串是否为空isEmpty StringUtils.isEmpty(null) = true StringUtils.i ...

  9. [Docker]Docker与Linux ip_forward数据包转发

    背景 今天在一台新虚拟机上需要临时启动一个consul服务,安装Docker后使用docker启动,但是在执行启动命令后发现docker有一个警告: WARNING: IPv4 forwarding ...

  10. 交换机Access、Trunk和Hybrid 接口类型及区别

    交换机接口的类型可以是 Access.Trunk和Hybrid. Access类型的接口仅属于一个VLAN,只能接收.转发相应VLAN的帧: Trunk类型接口则默认属于所有VLAN,任何 Tagge ...