最近项目中要写一个微信那种发送位置的功能。具体功能在于:
- 定位到当前位置
- 可定位当前位置附近的poi
- 可自行搜索目标位置,并展示附近的poi
- 选择当前位置或者选择目标位置进行发送
一.准备工作
1.首先去高德地图官网下载相关的sdk(如下图):
2.按照高德地图官网的步骤继续添加所需要的依赖库
3.根据项目的bundleid前往高德地图api中创建引用并申请相关的key
到这一步前期的准备工作基本差不多了,当然,我这写的不是很具体,详细的引入高德地图sdk还需要耐心按照高德地图官网sdk一步步的去操作
二.代码部分
1.初始化高德地图sdk
在appledelegate中引入相关头文件,从高德地图应用管理中找到该应用对用的key值,进行高德地图sdk的初始化操作。直接贴代码部分:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
#import "appdelegate.h"
#import "viewcontroller.h"
#import <amaplocationkit/amaplocationkit.h>
#import <amapfoundationkit/amapfoundationkit.h>
static nsstring *apikey = @ "a1500980e29b7ca7612a46c19e0d2e3a" ;
@interface appdelegate ()
@end
@implementation appdelegate
- ( bool )application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions {
self.window = [[uiwindow alloc] initwithframe:[uiscreen mainscreen].bounds];
[self.window makekeyandvisible];
self.window.rootviewcontroller = [[uinavigationcontroller alloc] initwithrootviewcontroller:[viewcontroller new ]];
[amapservices sharedservices].apikey = apikey;
return yes;
}
|
2.定位到用户当前位置
在你需要定位的类中引入与地图相关的头文件
初始化地图view:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
- ( void )initmapview{
self.mapview = [[mamapview alloc] initwithframe:cgrectmake(0, 64 + 44, screen_width, 300)];
self.mapview.delegate = self;
self.mapview.maptype = mamaptypestandard;
self.mapview.showsscale = no;
self.mapview.showscompass = no;
self.mapview.showsuserlocation = yes;
[self.view addsubview:self.mapview];
uibutton *localbutton = [uibutton buttonwithtype:uibuttontypecustom];
localbutton.backgroundcolor = [uicolor redcolor];
localbutton.frame = cgrectmake(screen_width - 60, 240, 50, 50);
[localbutton addtarget:self action:@selector(localbuttonaction) forcontrolevents:uicontroleventtouchupinside];
localbutton.layer.cornerradius = 25;
localbutton.clipstobounds = yes;
[localbutton setimage:[uiimage imagenamed:@ "定位" ] forstate:uicontrolstatenormal];
[self.mapview addsubview:localbutton];
}
|
1
2
3
4
5
6
7
8
9
10
|
// 定位sdk
- ( void )configlocationmanager {
self.locationmanager = [[amaplocationmanager alloc] init];
[self.locationmanager setdelegate:self];
[self.locationmanager setdesiredaccuracy:kcllocationaccuracyhundredmeters];
//单次定位超时时间
[self.locationmanager setlocationtimeout:6];
[self.locationmanager setregeocodetimeout:3];
}
|
开启定位操作:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
- ( void )locateaction {
[self showhudinview:self.view hint:@ "正在定位..." ];
//带逆地理的单次定位
[self.locationmanager requestlocationwithregeocode:yes completionblock:^(cllocation *location, amaplocationregeocode *regeocode, nserror *error) {
if (error) {
[self showhint:@ "定位错误" yoffset:-180];
nslog(@ "locerror:{%ld - %@};" ,( long )error.code,error.localizeddescription);
if (error.code == amaplocationerrorlocatefailed) {
return ;
}
}
//定位信息
nslog(@ "location:%@" , location);
if (regeocode)
{
[self hidehud];
self.currentlocationcoordinate = cllocationcoordinate2dmake(location.coordinate.latitude, location.coordinate.longitude);
self.city = regeocode.city;
[self showmappoint];
[self setcenterpoint];
self.request.location = [amapgeopoint locationwithlatitude:location.coordinate.latitude longitude:location.coordinate.longitude];
[self.mapsearch amappoiaroundsearch:self.request];
}
}];
}
|
定位成功之后展示大头针到当前位置(如果一直不显示大头针,检查一下自己是否导入了高德地图的资源文件):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
- ( void )showmappoint{
[_mapview setzoomlevel:15.1 animated:yes];
[_mapview setcentercoordinate:self.currentlocationcoordinate animated:yes];
}
- ( void )setcenterpoint{
mapointannotation * centerannotation = [[mapointannotation alloc] init]; //初始化注解对象
centerannotation.coordinate = self.currentlocationcoordinate; //定位经纬度
centerannotation.title = @ "" ;
centerannotation.subtitle = @ "" ;
[self.mapview addannotation:centerannotation]; //添加注解
}
#pragma mark - mamapview delegate
- (maannotationview *)mapview:(mamapview *)mapview
viewforannotation:(id<maannotation>)annotation {
if ([annotation iskindofclass:[mapointannotation class ]]) {
static nsstring *pointreuseindentifier = @ "pointreuseindentifier" ;
mapinannotationview*annotationview = (mapinannotationview*)[mapview dequeuereusableannotationviewwithidentifier:pointreuseindentifier];
if (annotationview == nil)
{
annotationview = [[mapinannotationview alloc] initwithannotation:annotation reuseidentifier:pointreuseindentifier];
}
annotationview.canshowcallout= yes; //设置气泡可以弹出,默认为no
annotationview.animatesdrop = yes; //设置标注动画显示,默认为no
annotationview.draggable = yes; //设置标注可以拖动,默认为no
annotationview.pincolor = mapinannotationcolorred;
return annotationview;
}
return nil;
}
|
地图的代理方法等:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
- ( void )mapview:(mamapview *)mapview regiondidchangeanimated:( bool )animated{
[self.mapview removeannotations:self.mapview.annotations];
cllocationcoordinate2d centercoordinate = mapview.region.center;
self.currentlocationcoordinate = centercoordinate;
mapointannotation * centerannotation = [[mapointannotation alloc] init];
centerannotation.coordinate = centercoordinate;
centerannotation.title = @ "" ;
centerannotation.subtitle = @ "" ;
[self.mapview addannotation:centerannotation];
//主动选择地图上的地点
if (!self.isselectedaddress) {
[self.tableview setcontentoffset:cgpointmake(0,0) animated:no];
self.selectedindexpath=[nsindexpath indexpathforrow:0 insection:0];
self.request.location = [amapgeopoint locationwithlatitude:centercoordinate.latitude longitude:centercoordinate.longitude];
self.currentpage = 1;
self.request.page = self.currentpage;
[self.mapsearch amappoiaroundsearch:self.request];
}
self.isselectedaddress = no;
}
|
包括主动选择地图上的点然后进行附近的搜索,可自定义搜索的内容,定位成功之后用户可以获取到当前的经纬度地址等一系列信息。详细的代码有点多就不一一贴出来了,需要的同学可以点击下面的github地址去下载体验。
github地址:https://github.com/xuzzzzzzzz/xclocation
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://www.jianshu.com/p/c0ba4a06cdb8