iOS开发-开发百度地图2-iOS SDK 3.0.0-定位及geo检索服务
2016-09-07 13:57
281 查看
四、定位功能
由于系统原因,iOS不允许使用第三方定位,因此地图SDK中的定位方法,本质上是对原生定位的二次封装。通过封装,开发者可更便捷的使用。此外,地图SDK中还提供了相应的定位图层(支持定位三态效果),帮助开发者显示当前位置信息。
注:自iOS8起,系统定位功能进行了升级,SDK为了实现最新的适配,自v2.5.0起也做了相应的修改,开发者在使用定位功能之前,需要在info.plist里添加(以下二选一,两个都添加默认使用NSLocationWhenInUseUsageDescription):
NSLocationWhenInUseUsageDescription ,允许在前台使用时获取GPS的描述
NSLocationAlwaysUsageDescription ,允许永久使用GPS的描述
1、签协议,声明属性
@interface
YMMapViewController
<BMKLocationServiceDelegate,BMKMapViewDelegate>
@property (nonatomic,retain)BMKLocationService
*locService;
@property (nonatomic,retain)BMKMapView
*mapView;
2、具体实现
- (void)viewDidLoad {
[superviewDidLoad];
_mapView = [[BMKMapViewalloc]initWithFrame:CGRectMake(0,0,
320,480)];
_mapView.trafficEnabled =YES;
// 设置路况信息
self.view =_mapView;
// 设置地图的比例
_mapView.zoomLevel =20;
// 设置地图旋转角度
_mapView.rotation =50;
_locService = [[BMKLocationServicealloc]init];
_locService.delegate =self;
//启动LocationService
[_locServicestartUserLocationService];
}
//实现相关delegate处理位置信息更新
//定位成功处理方法
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
_mapView.showsUserLocation =YES;//显示定位图层
[_mapViewupdateLocationData:userLocation];
_mapView.centerCoordinate = userLocation.location.coordinate;
// 设置地图比例
_mapView.zoomLevel =20;
}
//定位失败处理方法
- (void)didFailToLocateUserWithError:(NSError *)error{
UIAlertView* alert = [[UIAlertViewalloc]
initWithTitle:@"提示"
message:@"定位失败,请设置允许软件开始定位"
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
}
-(void)viewWillDisappear:(BOOL)animated {
[superviewWillDisappear:animated];
[_mapViewviewWillDisappear];
_mapView.delegate =nil;
// // 此处记得不用的时候需要置nil,否则影响内存的释放
_locService.delegate =nil;
}
- (void)viewWillAppear:(BOOL)animated {
[superviewWillAppear:animated];
[_mapViewviewWillAppear];
_mapView.delegate =self;
// 此处记得不用的时候需要置nil,否则影响内存的释放
_locService.delegate =self;
}
五、geo检索服务
1、签协议,声明属性
<BMKGeoCodeSearchDelegate>{
BMKGeoCodeSearch* _geocodesearch;
}
- (void)viewDidLoad {
_geocodesearch = [[BMKGeoCodeSearchalloc]init];
_geocodesearch.delegate = self;
}
2、在定位成功的方法里调用此方法
//定位成功方法
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation;
- (void)nowPlace{
isGeoSearch =false;
CLLocationCoordinate2D pt = (CLLocationCoordinate2D){0,0};
pt = (CLLocationCoordinate2D){_mapView.centerCoordinate.latitude
,_mapView.centerCoordinate.longitude};
BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOptionalloc]init];
reverseGeocodeSearchOption.reverseGeoPoint = pt;
BOOL flag = [_geocodesearchreverseGeoCode:reverseGeocodeSearchOption];
if(flag)
{
NSLog(@"反geo检索发送成功");
}
else
{
NSLog(@"反geo检索发送失败");
}
}
#pragma mark - BMKGeoCodeSearchDelegate协议方法
-(void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult
*)result errorCode:(BMKSearchErrorCode)error
{
BMKAddressComponent *addressComponent = result.addressDetail;
NSString *title = [NSStringstringWithFormat:@"%@%@%@%@",
addressComponent.city, addressComponent.district, addressComponent.streetName, addressComponent.streetNumber];
NSLog(@"%s -- %@",__func__, title);
}
由于系统原因,iOS不允许使用第三方定位,因此地图SDK中的定位方法,本质上是对原生定位的二次封装。通过封装,开发者可更便捷的使用。此外,地图SDK中还提供了相应的定位图层(支持定位三态效果),帮助开发者显示当前位置信息。
注:自iOS8起,系统定位功能进行了升级,SDK为了实现最新的适配,自v2.5.0起也做了相应的修改,开发者在使用定位功能之前,需要在info.plist里添加(以下二选一,两个都添加默认使用NSLocationWhenInUseUsageDescription):
NSLocationWhenInUseUsageDescription ,允许在前台使用时获取GPS的描述
NSLocationAlwaysUsageDescription ,允许永久使用GPS的描述
1、签协议,声明属性
@interface
YMMapViewController
<BMKLocationServiceDelegate,BMKMapViewDelegate>
@property (nonatomic,retain)BMKLocationService
*locService;
@property (nonatomic,retain)BMKMapView
*mapView;
2、具体实现
- (void)viewDidLoad {
[superviewDidLoad];
_mapView = [[BMKMapViewalloc]initWithFrame:CGRectMake(0,0,
320,480)];
_mapView.trafficEnabled =YES;
// 设置路况信息
self.view =_mapView;
// 设置地图的比例
_mapView.zoomLevel =20;
// 设置地图旋转角度
_mapView.rotation =50;
_locService = [[BMKLocationServicealloc]init];
_locService.delegate =self;
//启动LocationService
[_locServicestartUserLocationService];
}
//实现相关delegate处理位置信息更新
//定位成功处理方法
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
_mapView.showsUserLocation =YES;//显示定位图层
[_mapViewupdateLocationData:userLocation];
_mapView.centerCoordinate = userLocation.location.coordinate;
// 设置地图比例
_mapView.zoomLevel =20;
}
//定位失败处理方法
- (void)didFailToLocateUserWithError:(NSError *)error{
UIAlertView* alert = [[UIAlertViewalloc]
initWithTitle:@"提示"
message:@"定位失败,请设置允许软件开始定位"
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
}
-(void)viewWillDisappear:(BOOL)animated {
[superviewWillDisappear:animated];
[_mapViewviewWillDisappear];
_mapView.delegate =nil;
// // 此处记得不用的时候需要置nil,否则影响内存的释放
_locService.delegate =nil;
}
- (void)viewWillAppear:(BOOL)animated {
[superviewWillAppear:animated];
[_mapViewviewWillAppear];
_mapView.delegate =self;
// 此处记得不用的时候需要置nil,否则影响内存的释放
_locService.delegate =self;
}
五、geo检索服务
1、签协议,声明属性
<BMKGeoCodeSearchDelegate>{
BMKGeoCodeSearch* _geocodesearch;
}
- (void)viewDidLoad {
_geocodesearch = [[BMKGeoCodeSearchalloc]init];
_geocodesearch.delegate = self;
}
2、在定位成功的方法里调用此方法
//定位成功方法
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation;
- (void)nowPlace{
isGeoSearch =false;
CLLocationCoordinate2D pt = (CLLocationCoordinate2D){0,0};
pt = (CLLocationCoordinate2D){_mapView.centerCoordinate.latitude
,_mapView.centerCoordinate.longitude};
BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOptionalloc]init];
reverseGeocodeSearchOption.reverseGeoPoint = pt;
BOOL flag = [_geocodesearchreverseGeoCode:reverseGeocodeSearchOption];
if(flag)
{
NSLog(@"反geo检索发送成功");
}
else
{
NSLog(@"反geo检索发送失败");
}
}
#pragma mark - BMKGeoCodeSearchDelegate协议方法
-(void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult
*)result errorCode:(BMKSearchErrorCode)error
{
BMKAddressComponent *addressComponent = result.addressDetail;
NSString *title = [NSStringstringWithFormat:@"%@%@%@%@",
addressComponent.city, addressComponent.district, addressComponent.streetName, addressComponent.streetNumber];
NSLog(@"%s -- %@",__func__, title);
}
相关文章推荐
- iOS开发-开发百度地图1-iOS SDK 3.0.0-基本地图的配置及实现
- 百度地图 iOS SDK - iOS8下使用百度地图SDK定位的问题
- 百度地图 Android 定位SDK 开发指南4.2
- iOS开发 - 解决百度地图模拟器无法定位问题
- 百度地图、定位、导航SDK开发问题汇总及解决办法
- ios百度导航SDK,iOS开发之百度地图导航
- iOS 百度地图开发(一) sdk添加
- iOS百度地图poi检索(基于百度地图2.3.0SDK开发)
- 百度地图iOS SDK开发教程(珍藏版)
- 百度地图Android sdk开发笔记四sdk定位
- Android定位SDK_百度地图开发
- iOS开发-百度地图基本定位和罗盘跟随的使用,附加地理编码和反地理编码
- iOS开发 - 百度地图后台持续定位
- iOS开发(第三方使用)——百度地图的简单使用(定位与当前位置的显示)
- iOS开发----百度地图定位之KCLErrorDomain Code = 0的解决方法
- iOS开发 - 百度地图后台持续定位
- iOS开发 - 百度地图后台持续定位
- iOS定位服务与地图开发(7)---使用本地化的百度地图
- IOS 调用百度地图(SDK) 进行定位以及自定义位置弹出框(气泡)
- 百度地图Android SDK 2.0开发指南2-结合百度定位SDK在百度地图上显示当前位置