IOS实现自动定位和手动选择城市功能
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中使用初始化地图服务
#pragma mark MAMAP init AND 定位回调
- (void)initMap
{ if (![CLLocationManager locationServicesEnabled]) {
[PXAlertView showAlertWithTitle:@"提示" message:[NSString stringWithFormat:@"请开启定位:设置 > 隐私 > 位置 > 定位服务"] completion:^(BOOL cancelled, NSInteger buttonIndex) {
}];
return;
}
else if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied){
[PXAlertView showAlertWithTitle:@"提示" message:[NSString stringWithFormat:@"定位失败,请开启定位:设置 > 隐私 > 位置 > 定位服务 下 应用"] completion:^(BOOL cancelled, NSInteger buttonIndex) {
}];
return;
}
[MAMapServices sharedServices].apiKey = MAMAP_APPKEY;//高德KEY
_mapView = [[MAMapView alloc] init];
_mapView.delegate = self;
_mapView.showsUserLocation = YES;
//[_mapView setUserTrackingMode:MAUserTrackingModeFollowWithHeading animated:YES]; point = [[AMapGeoPoint alloc] init];
_search = [[AMapSearchAPI alloc] initWithSearchKey:MAMAP_APPKEY
Delegate:self];
regeoRequest = [[AMapReGeocodeSearchRequest alloc] init];
regeoRequest.searchType = AMapSearchType_ReGeocode;
regeoRequest.radius = 50;
regeoRequest.requireExtension = YES;
} // 定位回调
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation
{
if (updatingLocation) { point.latitude = userLocation.coordinate.latitude;
point.longitude = userLocation.coordinate.longitude; regeoRequest.location = [AMapGeoPoint locationWithLatitude:point.latitude
longitude:point.longitude];
// 发起逆地理编码
[_search AMapReGoecodeSearch:regeoRequest];
_mapView.showsUserLocation =NO; }
}
// 逆地理编码回调
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response
{
if (response.regeocode != nil) { NSString *province=response.regeocode.addressComponent.province;
NSString *city=response.regeocode.addressComponent.city;
NSString *district=response.regeocode.addressComponent.district;
NSString *value=nil;
NSString *key=nil; NSMutableArray *cityArrayTemp=nil;
for (AreaModel *model in provinceArray) {
if ([model.value isEqualToString:province]) {
cityArrayTemp=[NSMutableArray arrayWithArray:model.children];
}
}
NSMutableArray *districtArryTemp=nil;
if (cityArrayTemp) {
if (city.length>0) {
for (AreaModel *cityModel in cityArrayTemp) {
if ([cityModel.value isEqualToString:city]) {
districtArryTemp=[NSMutableArray arrayWithArray:cityModel.children];
}
}
}
else{
//直辖市
for (AreaModel *cityModel in cityArrayTemp) {
if ([cityModel.value isEqualToString:district]) {
value=cityModel.value;
key=cityModel.key;
}
}
}
}
if (districtArryTemp) {
for (AreaModel *provinceModel in districtArryTemp) {
if ([provinceModel.value isEqualToString:district]) {
value=provinceModel.value;
key=provinceModel.key;
}
}
}
self.keyCode=key;
[self.checkCityButton setTitle:[NSString stringWithFormat:@"%@%@%@",province,city,district] forState:UIControlStateNormal]; }
}
封装省市县 三级选择控件
#import <UIKit/UIKit.h> @interface AreaPickerCheckView : UIView<UIPickerViewDataSource,UIPickerViewDelegate> - (instancetype)initWithFrame:(CGRect)frame andDataSource:(NSArray *)dataSource dismissCompletion:(void (^)(BOOL isCancelClick , NSString *area , NSString *code))completion; @end #import "AreaPickerCheckView.h"
#import "AreaModel.h" @interface AreaPickerCheckView()
{
NSArray *provinceArray;
NSArray *cityArray;
NSArray *areaArray;
void (^completionBlock)(BOOL isCancelClick , NSString *area , NSString *code);
} @property(nonatomic,strong)NSArray *dataSource;
@property(nonatomic,strong)UIPickerView *pickerView; @end @implementation AreaPickerCheckView - (instancetype)initWithFrame:(CGRect)frame andDataSource:(NSArray *)dataSource dismissCompletion:(void (^)(BOOL, NSString *, NSString *))completion
{
self = [super initWithFrame:frame];
if (self) {
completionBlock = [completion copy];
self.dataSource = dataSource;
[self createUI];// Initialization code
}
return self;
} - (void)createUI { provinceArray = self.dataSource; AreaModel *model = [self.dataSource firstObject]; cityArray = model.children; AreaModel *model1 = [model.children firstObject]; areaArray = model1.children; self.pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 25, self.frame.size.width, self.frame.size.height-25)];
self.pickerView.dataSource = self;
self.pickerView.delegate = self;
[self addSubview:self.pickerView]; UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
confirmBtn.frame = CGRectMake(Main_Screen_Width - 50, 5, 40, 20);
[confirmBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
[confirmBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
confirmBtn.tag = 100;
[self addSubview:confirmBtn]; UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
cancelBtn.frame = CGRectMake(10, 5, 40, 20);
cancelBtn.tag = 101;
[cancelBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
[cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self addSubview:cancelBtn]; } #pragma mark dataSouce
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 3;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (component == 0) {
return provinceArray.count;
}else if (component == 1) {
return cityArray.count;
}
return areaArray.count;
}
#pragma mark delegate
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
AreaModel *model = nil;
if (component == 0) {
model = provinceArray[row];
}else if (component == 1) {
model = cityArray[row];
}else if (component == 2) {
model = areaArray[row];
}
return model.value;
} - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (component == 0) {
AreaModel *model = provinceArray[row];
cityArray = model.children;
areaArray = [[cityArray firstObject] children]; [self.pickerView reloadComponent:1];
[self.pickerView reloadComponent:2]; }else if (component == 1) {
AreaModel *model = cityArray[row];
areaArray = model.children;
[self.pickerView reloadComponent:2];
}
} - (void)btnClick:(UIButton *)btn {
BOOL isCancel = NO;
switch (btn.tag) {
case 100: break;
case 101:
isCancel = YES;
completionBlock(YES,nil,nil);
return;
break; default:
break;
} NSString *str = nil;
NSString *codeStr = nil; AreaModel *model = provinceArray[[self.pickerView selectedRowInComponent:0]];
str = model.value;
AreaModel *model1 = cityArray[[self.pickerView selectedRowInComponent:1]];
str = [str stringByAppendingString:model1.value];
codeStr = model1.key;
if (areaArray.count > 0) {
AreaModel *model2 = areaArray[[self.pickerView selectedRowInComponent:2]];
str = [str stringByAppendingString:model2.value];
codeStr = model2.key;
} completionBlock(isCancel,str,codeStr);
} @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;
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *models = [AreaModel objectArrayWithFilename:@"area.plist"];
provinceArray=models;
AreaPickerCheckView *picker = [[AreaPickerCheckView alloc]initWithFrame:CGRectMake(0, 0, Main_Screen_Width, 216) andDataSource:models dismissCompletion:^(BOOL isCancelClick, NSString *area, NSString *code) {
[self.areaTF resignFirstResponder];
if (isCancelClick) {
return;
}else{
self.areaTF.text = area;
_code = code;
}
}];
self.areaTF.inputView = picker;
[self initMap];
}
IOS开发技术交流QQ群:491355147 欢迎加入一起讨论技术哦
IOS实现自动定位和手动选择城市功能的更多相关文章
- JS中调用android和ios系统手机打开相机并可选择相册功能
编写不易,如有转载,请声明出处: 梦回河口:http://blog.csdn.net/zxc514257857/article/details/57626154 实现android手机打开相机选择相册 ...
- android EditText长按屏蔽ActionMode context菜单但保留选择工具功能
最近项目要求屏蔽EditText 长按出来的ActionMode菜单,但是要保留选择文本功能.这个屏蔽百度会出现各种方法,这里说一下我的思路: 1.屏蔽百度可知setCustomSelectionAc ...
- IOS开发-OC学习-常用功能代码片段整理
IOS开发-OC学习-常用功能代码片段整理 IOS开发中会频繁用到一些代码段,用来实现一些固定的功能.比如在文本框中输入完后要让键盘收回,这个需要用一个简单的让文本框失去第一响应者的身份来完成.或者是 ...
- iOS 8 中如何集成 Touch ID 功能
2013年9月,苹果为当时发布的最新iPhone产品配备了一系列硬件升级方案.在iPhone 5s当中,最具创新特性的机制无疑要数围绕Home按钮设计的超薄金属圈,也就是被称为Touch ID的指纹传 ...
- iOS AudioSession详解 Category选择 听筒扬声器切换
在你读这篇文章之前,如果你不嫌读英文太累,推荐阅读下苹果iOS Human Interface Guidelines中Sound这一章. 选择一个Category AVAudioSessionCa ...
- h5手机端下拉选择城市
<!doctype html><html> <head> <meta http-equiv="Content-Type& ...
- 美团HD(5)-选择城市
DJSelectCityViewController.m #import "DJSelectCityViewController.h" #import "DJConsta ...
- jquery实现输入框聚焦,键盘上下键选择城市
在最近有个项目中 需要实现当文本框聚焦的时候,可以键盘上下键选择内容,按enter键的时候,把内容传到输入框中,如图所示: 实现代码如下: /** *输入框聚焦,键盘上下键选择城市 */ ;(func ...
- 【实用技巧】取消Win7开机账户的手动选择
因为前面碰到的一些事情,稍有感慨. 关于win7的一些小技巧都不是什么很有技术含量东西,或者说很浅显.我说一个技巧,也许很多人都知道,也许也早有人说过.但我想说的是我不是在炫耀什么,我只是想分享一些我 ...
随机推荐
- php递归无限查询上级或者下级
$this->get_array($user['uid'],1); function get_array($user_id,$top=0){ $sql = 'SELECT * FROM ' . ...
- RayFire的下载与安装方法
RayFire的下载与安装方法 发布时间:2020/10/12 近几年,电影中融入了越来越多的动画元素,其中的爆炸场景更是十分吸引眼球.小编不禁好奇,什么样的插件能做出来如此好玩的特效,上网搜索一番发 ...
- CorelDRAW 里面如何将文字调整成半透明的颜色
早在几年前,平面设计师在做设计时会遇到关于印刷的难题,那就是为了降低印刷成本,必须减少他们的颜色数量.随着印刷方法的进步,特别是数字出版物的兴起,我们生活在一个主要是通过屏幕观看图形的时代,一个可以显 ...
- 思维导图MindManager属性功能怎么合理使用
在MindManager中为主题添加相应的标注可以使读者更好的理解主题内容,增强导图的可读性,因此,如何在MindManager中为主题添加标注也就成了我们的关注点. 巧妙使用这款思维导图软件的属性功 ...
- Sonar检测Math.abs(new Random().nextInt()) “Use the original value instead”
今天早上旁边同事喊我看一个Sonar检测出的问题: 当时看了好几眼没觉得这个有太大问题,于是又看了下Sonar建议: 这是说Math.abs()方法使用在数字上面可能返回最小值,觉得这个挺有意思的,于 ...
- Python多线程join和setDaemon区别与用法
一直没有太搞清楚join和setDaemon有什么区别,总是对于它们两个的概念很模糊,需要做个实验然后记录一下. 先说结论: join: 子线程合并到主线程上来的作用,就是当主线程中有子线程join的 ...
- 在EXCEL带有字母的数字下拉如何能自动排序
在excel中0,1,2,3,4,5,6,7,8,9会自动排序,a,b,c,d,e,f,g.....会自动排序,所以可以分布来实现. 例如排序:fish1a.png,fish1b.png,fish1c ...
- 测试开发工程必备技能之一:Mock的使用
1. 背景 在实际产品开发过程中,某个服务或前端依赖一个服务接口,该接口可能依赖多个底层服务或模块,或第三方接口,比如说服务 A 依赖服务B,服务B又依赖服务 C,如下图所示: 这种依赖的问题会导致原 ...
- Mac MySQL 8.0 (免安装版) 主从集群搭建
一.下载解压包 打开 MySQL 官网地址:https://dev.mysql.com/downloads/mysql/ ,选择面安装版本. 二.解压文件 下载到合适文件夹,解压压缩包. 解压 mys ...
- ElementUI el-date-picker 限制选中日期前后30天,大于当天不可选
<template> <el-date-picker v-model="date" type="daterange" range-separa ...