最近在做項目中,需要用到最新版的百度地圖,這里是個小的Demo,希望能夠有用到的。
效果圖:先貼上。

一、直接到百度地圖的API官網(wǎng),自己搭建項目的百度API。手動添加即可。
二、在添加好百度API后,進行相應(yīng)的配置。
AppDelegate中進行添加:
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
// Override point for customization after application launch.
//要使用百度地圖,請先啟動BaiduMapManager
_mapManager= [[BMKMapManageralloc]init];
/**
*百度地圖SDK所有接口均支持百度坐標(biāo)(BD09)和國測局坐標(biāo)(GCJ02),用此方法設(shè)置您使用的坐標(biāo)類型.
*默認是BD09(BMK_COORDTYPE_BD09LL)坐標(biāo).
*如果需要使用GCJ02坐標(biāo),需要設(shè)置CoordinateType為:BMK_COORDTYPE_COMMON.
*/
if([BMKMapManagersetCoordinateTypeUsedInBaiduMapSDK:BMK_COORDTYPE_COMMON]) {
NSLog(@"經(jīng)緯度類型設(shè)置成功");
}else{
NSLog(@"經(jīng)緯度類型設(shè)置失敗");
}
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;//地理編碼主類,用來查詢、返回結(jié)果信息
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;//設(shè)置地圖為空白類型
_mapView.showsUserLocation=YES;//是否顯示定位圖層(即我的位置的小圓點)
[_mapViewsetZoomLevel:19.0];
//? ? _mapView.userTrackingMode = BMKUserTrackingModeFollow;
[self.viewaddSubview:_mapView];
//去除百度地圖定位后的藍色圓圈和定位藍點(精度圈)
BMKLocationViewDisplayParam*displayParam = [[BMKLocationViewDisplayParamalloc]init];
displayParam.isAccuracyCircleShow=false;//精度圈是否顯示
displayParam.locationViewOffsetX=0;//定位偏移量(經(jīng)度)
displayParam.locationViewOffsetY=0;//定位偏移量(緯度)
displayParam.locationViewImgName=@"icon";//定位圖標(biāo)名稱去除藍色的圈
[_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,否則影響內(nèi)存的釋放
_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");
}
//實現(xiàn)相關(guān)delegate處理位置信息更新
//處理方向變更信息
- (void)didUpdateUserHeading:(BMKUserLocation*)userLocation
{
//NSLog(@"heading is %@",userLocation.heading);
//? ? [_mapView updateLocationData:userLocation];
}
//處理位置坐標(biāo)更新
- (void)didUpdateBMKUserLocation:(BMKUserLocation*)userLocation
{
NSLog(@"獲取坐標(biāo)成功");
NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
//? ? //更新地圖上的位置
//? ? [_mapView updateLocationData:userLocation];
if(userLocation.location!=nil) {
//? ? ? ? [_locService stopUserLocationService];
//添加當(dāng)前位置的標(biāo)注
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];
}
}
//? ? //表示范圍的結(jié)構(gòu)體
//? ? BMKCoordinateRegion region;
//? ? region.center.latitude = userLocation.location.coordinate.latitude;
//? ? region.center.longitude = userLocation.location.coordinate.longitude;
//經(jīng)度范圍(設(shè)置為0.1表示顯示范圍為0.2的緯度范圍)
//? ? _mapView.centerCoordinate = userLocation.location.coordinate; //更新當(dāng)前位置到地圖中間
//
//? ? //地理反編碼
//
//? ? BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];
//
//? ? reverseGeocodeSearchOption.reverseGeoPoint = userLocation.location.coordinate;
//
//? ? BOOL flag = [_geocodesearch reverseGeoCode:reverseGeocodeSearchOption];
//
//? ? if(flag){
//
//? ? ? ? NSLog(@"反geo檢索發(fā)送成功");
////? ? ? ? _mapView.region = region;
//? ? ? ? [_locService stopUserLocationService];
//
//? ? }else{
//
//? ? ? ? NSLog(@"反geo檢索發(fā)送失敗");
//
//? ? }
//? ? _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:地址坐標(biāo)
//? poiList:地址周邊POI信息,成員類型為BMKPoiInfo
//? ? if (error==0) {
//? ? ? ? BMKPointAnnotation *item=[[BMKPointAnnotation alloc] init];
//? ? ? ? item.coordinate=result.geoPt;//地理坐標(biāo)
//? ? ? ? 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;//地區(qū)
//? ? ? ? }
//? ? }
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end