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

2020-01-14 15:18:03于丽

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

如果你需要在地图上做一些请求,可以实现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];
}