欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

ios百度地图的使用(普通定位、反地理编码)

程序员文章站 2022-03-23 13:11:37
ios定位 - 普通定位(没有地图) - 反地理编码(得到具体位置),下面通过代码给大家详解,代码如下: #import

ios定位 - 普通定位(没有地图) - 反地理编码(得到具体位置),下面通过代码给大家详解,代码如下:

#import <corelocation/corelocation.h> 使用到的头文件 要引入corelocation这个包
<cllocationmanagerdelegate>    使用的代理名称
//1.使用定位服务
 //设置app有访问定位服务的权限
 //在使用应用期间 / 始终(app在后台)
 //info.plist文件添加以下两条(或者其中一条):
 //nslocationwheninuseusagedescription 在使用应用期间
 //nslocationalwaysusagedescription 始终
 //2.locationmanager 对象管理相关的定位服务
 _manager = [[cllocationmanager alloc] init];
 //manager判断: 手机是否开启定位 / app是否有访问定位的权限
 //[cllocationmanager locationservicesenabled]; //手机是否开启定位
 //[cllocationmanager authorizationstatus]; //app访问定位的权限的状态
 if (![cllocationmanager locationservicesenabled] || [cllocationmanager authorizationstatus] != kclauthorizationstatusauthorizedwheninuse) {
  [_manager requestwheninuseauthorization]; //向用户请求访问定位服务的权限
 }
 _manager.delegate = self;
 _manager.desiredaccuracy = kcllocationaccuracybest;
 _manager.distancefilter = 1.0f;
 [_manager startupdatinglocation];
//定位代理经纬度回调
-(void)locationmanager:(cllocationmanager *)manager didupdatetolocation:(cllocation *)newlocation fromlocation:(cllocation *)oldlocation {
 [_manager stopupdatinglocation];
 clgeocoder * geocoder = [[clgeocoder alloc] init];
 [geocoder reversegeocodelocation:newlocation completionhandler:^(nsarray *placemarks, nserror *error) {
  for (clplacemark * placemark in placemarks) {
   nsdictionary *test = [placemark addressdictionary];
   // country(国家) state(城市) sublocality(区) name全称
   nslog(@"%@", [test objectforkey:@"name"]);
  }
 }];
}

ios百度地图的使用(普通定位、反地理编码)

1.首先接受基本的地图功能

新建一个地图类,xib拖也行,我这边是代码实现的。

 

_mapview = [[bmkmapview alloc]initwithframe:cgrectmake(0, 0,self.view.frame.size.width, self.view.frame.size.height)];//添加mapview
 [self.view addsubview:_mapview];
#pragma mark - 设置mapview属性
-(void)setmapviewproperty
{
 _mapview.maptype = bmkusertrackingmodefollowwithheading;
 _mapview.showsuserlocation = yes; //是否显示定位图层(即我的位置的小圆点)
 _mapview.zoomlevel = 16;//地图显示比例
 _mapview.rotateenabled = no; //设置是否可以旋转
  
 [self passlocationvalue];
}
#pragma mark -传入定位坐标 
//设置定位到得用户的位置,这里是简单的应用方法(必须打开程序时已经获取到地理位置坐标,为了解决地图定位时总是先显示*)
-(void)passlocationvalue
{
 bmkcoordinateregion viewregion = bmkcoordinateregionmake([userlocationmanager sharedinstance].clloction.coordinate, bmkcoordinatespanmake(0.02f,0.02f));
 bmkcoordinateregion adjustedregion = [_mapview regionthatfits:viewregion];
 [_mapview setregion:adjustedregion animated:yes];
  
}
#pragma mark -设置定位圆点属性
-(void)setuserimage
{
 //用户位置类
 bmklocationviewdisplayparam* param = [[bmklocationviewdisplayparam alloc] init];
 param.locationviewoffsety = 0;//偏移量
 param.locationviewoffsetx = 0;
 param.isaccuracycircleshow =no;//设置是否显示定位的那个精度圈
 param.isrotateanglevalid = no;
 [_mapview updatelocationviewwithparam:param];
}

这样基本的地图界面就出来了

如果你需要在地图上做一些请求,可以实现bmkmapviewdelegate,以下是mapview的一些协议方法

**
 *地图区域即将改变时会调用此接口
 *@param mapview 地图view
 *@param animated 是否动画
 */
- (void)mapview:(bmkmapview *)mapview regionwillchangeanimated:(bool)animated
{
 //todo
}
 
/**
 *地图区域改变完成后会调用此接口
 *@param mapview 地图view
 *@param animated 是否动画
 */
- (void)mapview:(bmkmapview *)mapview regiondidchangeanimated:(bool)animated
{
 //todo
}
/**
 *地图状态改变完成后会调用此接口
 *@param mapview 地图view
 */
- (void)mapstatusdidchanged:(bmkmapview *)mapview
{
 //todo
}

2.地图定位

我这边是将定位封装了一个独立的manager类来管理定位和地图上滑动到的位置,是将定位功能和地图mapview独立开来,管理地理移动位置的变化

#import <foundation/foundation.h>
#import "bmapkit.h"
@interface userlocationmanager : nsobject <bmkmapviewdelegate,bmklocationservicedelegate>
{
 cllocation *cllocation;
 bmkreversegeocodeoption *reversegeocodeoption;//逆地理编码
}
@property (strong,nonatomic) bmklocationservice *locservice;
//城市名
@property (strong,nonatomic) nsstring *cityname;
//用户纬度
@property (nonatomic,assign) double userlatitude;
//用户经度
@property (nonatomic,assign) double userlongitude;
//用户位置
@property (strong,nonatomic) cllocation *clloction;
//初始化单例
+ (userlocationmanager *)sharedinstance;
//初始化百度地图用户位置管理类
- (void)initbmkuserlocation;
//开始定位
-(void)startlocation;
//停止定位
-(void)stoplocation;
@end
#import "userlocationmanager.h"
@implementation userlocationmanager
+ (userlocationmanager *)sharedinstance
{
 static userlocationmanager *_instance = nil;
 @synchronized (self) {
  if (_instance == nil) {
   _instance = [[self alloc] init];
  }
 }
 return _instance;
}
-(id)init
{
 if (self == [super init])
 {
  [self initbmkuserlocation];
 }
 return self;
}
#pragma 初始化百度地图用户位置管理类
/**
 * 初始化百度地图用户位置管理类
 */
- (void)initbmkuserlocation
{
 _locservice = [[bmklocationservice alloc]init];
 _locservice.delegate = self;
 [self startlocation];
}
#pragma 打开定位服务
/**
 * 打开定位服务
 */
-(void)startlocation
{
 [_locservice startuserlocationservice];
}
#pragma 关闭定位服务
/**
 * 关闭定位服务
 */
-(void)stoplocation
{
 [_locservice stopuserlocationservice];
}
#pragma bmklocationservicedelegate
/**
 *用户位置更新后,会调用此函数
 *@param userlocation 新的用户位置
 */
- (void)didupdateuserlocation:(bmkuserlocation *)userlocation
{
  cllocation = userlocation.location;
 _clloction = cllocation;
 _userlatitude = cllocation.coordinate.latitude;
 _userlongitude = cllocation.coordinate.longitude;
 [self stoplocation];(如果需要实时定位不用停止定位服务)
}
/**
 *在停止定位后,会调用此函数
 */
- (void)didstoplocatinguser
{
;
}
/**
 *定位失败后,会调用此函数
 *@param error 错误号
 */
- (void)didfailtolocateuserwitherror:(nserror *)error
{
 [self stoplocation];
}

以上代码就是本文ios百度地图的使用(普通定位、反地理编码),希望对大家今后的工作和学习有所帮助。