前言:如何定位到自己的位置,并且在地圖上標(biāo)記自己的位置弱睦,方法如下
打開百度開放平臺(tái)
- 創(chuàng)建應(yīng)用塘匣,把圖所圈的根據(jù)自己的項(xiàng)目填入
打開xcode創(chuàng)建一個(gè)新的工程
- 開始集成到工程 按開發(fā)文檔進(jìn)行集成有兩種方式一種是
cocoapods
净当,一種是普通的集成,如果是oc
就按oc
的環(huán)境配置祈坠,swift
就按swift
環(huán)境配置,在此為了方便我采用cocoapods
集成
-
如圖所示已成功下載了百度的包矢劲,然后打開工程編譯一下看是否報(bào)錯(cuò)赦拘,如果編譯成功,恭喜你以成功把百度的包集成到了自己的工程中芬沉,下面就要加入必要代碼躺同,以及一些權(quán)限
- ViewController內(nèi)的代碼
#import <BaiduMapAPI_Map/BMKMapComponent.h>
#import <BaiduMapAPI_Location/BMKLocationComponent.h>
#import <BaiduMapAPI_Search/BMKGeocodeSearch.h>
@interface ViewController ()<BMKMapViewDelegate,BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate>{
BMKLocationService* _locService;
BMKMapView* _mapView;
BMKGeoCodeSearch *_geoCodeSearch;
}
@end
@implementation ViewController
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
_locService.delegate = self;
_mapView.delegate = self; // 此處記得不用的時(shí)候需要置nil,否則影響內(nèi)存的釋放
}
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
_locService.delegate = nil;
_mapView.delegate = nil; // 不用時(shí)丸逸,置nil
}
- (void)viewDidLoad {
[super viewDidLoad];
// 設(shè)置地圖定位
[self setupBMKLocation];
}
- (void)setupBMKLocation {
_mapView = [[BMKMapView alloc]init];
_mapView.frame = self.view.bounds;
[self.view addSubview:_mapView];
//初始化BMKLocationService
_locService = [[BMKLocationService alloc]init];
_locService.delegate = self;
// 初始化編碼服務(wù)
_geoCodeSearch = [[BMKGeoCodeSearch alloc] init];
_geoCodeSearch.delegate = self;
//啟動(dòng)LocationService
[_locService startUserLocationService];
_mapView.showsUserLocation = YES;//顯示定位圖層
_mapView.userTrackingMode = BMKUserTrackingModeFollow;//設(shè)置定位的狀態(tài)為普通定位模式
}
#pragma mark - BMKLocationServiceDelegate 實(shí)現(xiàn)相關(guān)delegate 處理位置信息更新
/**
*在地圖View將要啟動(dòng)定位時(shí)蹋艺,會(huì)調(diào)用此函數(shù)
*@param mapView 地圖View
*/
- (void)willStartLocatingUser
{
NSLog(@"start locate");
}
/**
*用戶方向更新后,會(huì)調(diào)用此函數(shù)
*@param userLocation 新的用戶位置
*/
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
NSLog(@"heading is %@",userLocation.heading);
}
/**
*用戶位置更新后黄刚,會(huì)調(diào)用此函數(shù)
*@param userLocation 新的用戶位置
*/
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
BMKCoordinateRegion region;
region.center.latitude = userLocation.location.coordinate.latitude;
region.center.longitude = userLocation.location.coordinate.longitude;
region.span.latitudeDelta = 0.2;
region.span.longitudeDelta = 0.2;
if (_mapView)
{
_mapView.region = region;
}
[_mapView setZoomLevel:19.0];
[_locService stopUserLocationService];//定位完成停止位置更新
//添加當(dāng)前位置的標(biāo)注
CLLocationCoordinate2D coord;
coord.latitude = userLocation.location.coordinate.latitude;
coord.longitude = userLocation.location.coordinate.longitude;
BMKPointAnnotation *_pointAnnotation = [[BMKPointAnnotation alloc] init];
_pointAnnotation.coordinate = coord;
//反地理編碼出地理位置
CLLocationCoordinate2D pt=(CLLocationCoordinate2D){0,0};
pt=(CLLocationCoordinate2D){coord.latitude,coord.longitude};
BMKReverseGeoCodeOption *reverseGeoCodeOption = [[BMKReverseGeoCodeOption alloc] init];
reverseGeoCodeOption.reverseGeoPoint = pt;
//發(fā)送反編碼請(qǐng)求.并返回是否成功
BOOL flag = [_geoCodeSearch reverseGeoCode:reverseGeoCodeOption];
if (flag) {
NSLog(@"反geo檢索發(fā)送成功");
} else {
NSLog(@"反geo檢索發(fā)送失敗");
}
dispatch_async(dispatch_get_main_queue(), ^{
[_mapView removeOverlays:_mapView.overlays];
[_mapView setCenterCoordinate:coord animated:true];
[_mapView addAnnotation:_pointAnnotation];
});
}
/**
*在地圖View停止定位后捎谨,會(huì)調(diào)用此函數(shù)
*@param mapView 地圖View
*/
- (void)didStopLocatingUser
{
NSLog(@"stop locate");
}
/**
*定位失敗后,會(huì)調(diào)用此函數(shù)
*@param mapView 地圖View
*@param error 錯(cuò)誤號(hào),參考CLError.h中定義的錯(cuò)誤號(hào)
*/
- (void)didFailToLocateUserWithError:(NSError *)error
{
NSLog(@"location error");
// NSString *city = [[NSUserDefaults standardUserDefaults] objectForKey:@"cityNmae"];
// [self.cityBtn setTitle:city forState:UIControlStateNormal];
}
// 反地理編碼
- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error {
if (error == 0) {
NSString *cityName = [result.poiList.firstObject city];
NSLog(@"%@, %@", [result.poiList.firstObject city], result.address);
// // 定位的city
// [[NSUserDefaults standardUserDefaults] setObject:[result.poiList.firstObject city] forKey:@"city"];
// [[NSUserDefaults standardUserDefaults] synchronize];
//
// // 保存定位的街道地址
// [[NSUserDefaults standardUserDefaults] setObject:result.addressDetail.streetName forKey:@"street"];
// [[NSUserDefaults standardUserDefaults] synchronize];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
-
這樣僅僅是把地圖顯示出來了侍芝,和我們想要的還不一樣研铆,發(fā)現(xiàn)控制臺(tái)打印出我們沒加權(quán)限,現(xiàn)在加一下
-
在真機(jī)上運(yùn)行發(fā)現(xiàn)州叠,可以定位到自己的位置棵红,也在地圖上有一個(gè)大頭針標(biāo)記在自己的位置上,但是我們的控制臺(tái)打印了一些東西
-
要求我們必須添加bundle display name 咧栗,并且我們的反檢索沒有成功逆甜,沒有在控制臺(tái)輸出我們的位置,下面就按提示添加一下bundle display name 致板,運(yùn)行一下看看效果
-
- 發(fā)現(xiàn)加入bundle display name成功的解決了反檢索的問題交煞,并且成功的輸出了我們當(dāng)前的位置??,到此就結(jié)束了定位自己的位置斟或,并且在地圖上標(biāo)記出來素征,后續(xù)會(huì)更新關(guān)于地圖的別的功能,盡請(qǐng)期待.....
demo地址
有沒有幫到你呢萝挤???
(歡迎大家對(duì)不合適的地方進(jìn)行指正御毅,看完覺得有幫到你給點(diǎn)個(gè)贊??吧)