最近在做项目中,需要用到最新版的百度地图,这里是个小的Demo,希望能够有用到的。
效果图:先贴上。
一、直接到百度地图的API官网,自己搭建项目的百度API。手动添加即可。
二、在添加好百度API后,进行相应的配置。
AppDelegate中进行添加:
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
// Override point for customization after application launch.
//要使用百度地图,请先启动BaiduMapManager
_mapManager= [[BMKMapManageralloc]init];
/**
*百度地图SDK所有接口均支持百度坐标(BD09)和国测局坐标(GCJ02),用此方法设置您使用的坐标类型.
*默认是BD09(BMK_COORDTYPE_BD09LL)坐标.
*如果需要使用GCJ02坐标,需要设置CoordinateType为:BMK_COORDTYPE_COMMON.
*/
if([BMKMapManagersetCoordinateTypeUsedInBaiduMapSDK:BMK_COORDTYPE_COMMON]) {
NSLog(@"经纬度类型设置成功");
}else{
NSLog(@"经纬度类型设置失败");
}
BOOLret = [_mapManagerstart:@"你自己的API_Key"generalDelegate:self];
if(!ret) {
NSLog(@"manager start failed!");
}
returnYES;
}
三、进行直接上手即可。
注意:要在用到百度地图的地方,进行释放dealloc。。。
//
// ViewController.m
// BaiduDemo
//
// Created by张建on 17/5/9.
// Copyright © 2017年zhangjian. All rights reserved.
//
#import"ViewController.h"
@interfaceViewController()<BMKMapViewDelegate, BMKLocationServiceDelegate, BMKGeoCodeSearchDelegate>
{
//地图
BMKMapView* _mapView;
//定位
BMKLocationService* _locService;
BMKGeoCodeSearch*_geocodesearch;//地理编码主类,用来查询、返回结果信息
BMKPointAnnotation*_pointAnnotation;
CLLocationCoordinate2Dcoord;
NSString*address;
}
@end
@implementationViewController
- (void)dealloc{
if(_mapView) {
_mapView=nil;
}
}
- (void)viewDidLoad {
[superviewDidLoad];
//添加地图
[selfaddBaiduMap];
_geocodesearch= [[BMKGeoCodeSearchalloc]init];
// _geocodesearch.delegate = self;
//开始定位
[selfstartLocation];
}
- (void)addBaiduMap{
_mapView= [[BMKMapViewalloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height)];
_mapView.mapType=BMKMapTypeStandard;//设置地图为空白类型
_mapView.showsUserLocation=YES;//是否显示定位图层(即我的位置的小圆点)
[_mapViewsetZoomLevel:19.0];
// _mapView.userTrackingMode = BMKUserTrackingModeFollow;
[self.viewaddSubview:_mapView];
//去除百度地图定位后的蓝色圆圈和定位蓝点(精度圈)
BMKLocationViewDisplayParam*displayParam = [[BMKLocationViewDisplayParamalloc]init];
displayParam.isAccuracyCircleShow=false;//精度圈是否显示
displayParam.locationViewOffsetX=0;//定位偏移量(经度)
displayParam.locationViewOffsetY=0;//定位偏移量(纬度)
displayParam.locationViewImgName=@"icon";//定位图标名称去除蓝色的圈
[_mapViewupdateLocationViewWithParam:displayParam];
}
- (void)startLocation{
//初始化BMKLocationService
_locService= [[BMKLocationServicealloc]init];
// _locService.delegate = self;
// _locService.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
// _locService.distanceFilter = 100.f;
//启动LocationService
[_locServicestartUserLocationService];
//解决定位用户的位置,进来直接显示天安门
// BMKCoordinateRegion region;
// region = BMKCoordinateRegionMake(coord, BMKCoordinateSpanMake(0.02f, 0.02f));
// BMKCoordinateRegion adjustRegion = [_mapView regionThatFits:region];
// [_mapView setRegion:adjustRegion animated:YES];
}
-(void)viewWillAppear:(BOOL)animated
{
[_mapViewviewWillAppear];
_mapView.delegate=self;//此处记得不用的时候需要置nil,否则影响内存的释放
_locService.delegate=self;
_geocodesearch.delegate=self;
}
-(void)viewWillDisappear:(BOOL)animated
{
[_mapViewviewWillDisappear];
_mapView.delegate=nil;//不用时,置nil
_locService.delegate=nil;
_geocodesearch.delegate=nil;
}
- (void)willStartLocatingUser
{
NSLog(@"start locate");
}
//实现相关delegate处理位置信息更新
//处理方向变更信息
- (void)didUpdateUserHeading:(BMKUserLocation*)userLocation
{
//NSLog(@"heading is %@",userLocation.heading);
// [_mapView updateLocationData:userLocation];
}
//处理位置坐标更新
- (void)didUpdateBMKUserLocation:(BMKUserLocation*)userLocation
{
NSLog(@"获取坐标成功");
NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
// //更新地图上的位置
// [_mapView updateLocationData:userLocation];
if(userLocation.location!=nil) {
// [_locService stopUserLocationService];
//添加当前位置的标注
CLLocationCoordinate2Dpt;
pt = userLocation.location.coordinate;
pt.latitude= userLocation.location.coordinate.latitude;
pt.longitude= userLocation.location.coordinate.longitude;
_pointAnnotation= [[BMKPointAnnotationalloc]init];
_pointAnnotation.coordinate= pt;
[_mapViewsetCenterCoordinate:ptanimated:true];
[_mapViewaddAnnotation:_pointAnnotation];
//反编码地理位置
BMKReverseGeoCodeOption*reverseGeocodeSearchOption = [[BMKReverseGeoCodeOptionalloc]init];
reverseGeocodeSearchOption.reverseGeoPoint= pt;
if([_geocodesearchreverseGeoCode:reverseGeocodeSearchOption]) {
[_locServicestopUserLocationService];
}
}
// //表示范围的结构体
// BMKCoordinateRegion region;
// region.center.latitude = userLocation.location.coordinate.latitude;
// region.center.longitude = userLocation.location.coordinate.longitude;
//经度范围(设置为0.1表示显示范围为0.2的纬度范围)
// _mapView.centerCoordinate = userLocation.location.coordinate; //更新当前位置到地图中间
//
// //地理反编码
//
// BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];
//
// reverseGeocodeSearchOption.reverseGeoPoint = userLocation.location.coordinate;
//
// BOOL flag = [_geocodesearch reverseGeoCode:reverseGeocodeSearchOption];
//
// if(flag){
//
// NSLog(@"反geo检索发送成功");
//// _mapView.region = region;
// [_locService stopUserLocationService];
//
// }else{
//
// NSLog(@"反geo检索发送失败");
//
// }
// _mapView.region = region;
// CLLocationCoordinate2D pt=(CLLocationCoordinate2D){0,0};
// pt=(CLLocationCoordinate2D){coord.latitude,coord.longitude};
// dispatch_async(dispatch_get_main_queue(), ^{
// [_mapView removeOverlays:_mapView.overlays];
// [_mapView setCenterCoordinate:coord animated:true];
// [_mapView addAnnotation:_pointAnnotation];
//
// });
}
#pragma mark -------------地理反编码的delegate---------------
-(void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch*)searcher result:(BMKReverseGeoCodeResult*)result errorCode:(BMKSearchErrorCode)error
{
NSLog(@"address:%@----%@-----%@",result.addressDetail, result.address,result.sematicDescription);
if(error ==0) {
[_locServicestopUserLocationService];
address= result.sematicDescription;
_pointAnnotation.title= result.address;
_pointAnnotation.subtitle=address;
}else{
NSLog(@"address:定位失败+++++");
}
//addressDetail:层次化地址信息
//address:地址名称
//businessCircle:商圈名称
// location:地址坐标
// poiList:地址周边POI信息,成员类型为BMKPoiInfo
// if (error==0) {
// BMKPointAnnotation *item=[[BMKPointAnnotation alloc] init];
// item.coordinate=result.geoPt;//地理坐标
// item.title=result.strAddr;//地理名称
// [_mapView addAnnotation:item];
// _mapView.centerCoordinate=result.geoPt;
//
// self.lalAddress.text=[result.strAddr stringByReplacingOccurrencesOfString:@"-" withString:@""];
// if (![self.lalAddress.text isEqualToString:@""]) {
// strProvince=result.addressComponent.province;//省份
// strCity=result.addressComponent.city;//城市
// strDistrict=result.addressComponent.district;//地区
// }
// }
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end