iOS--百度地圖相關(guān)功能的實(shí)現(xiàn)

一.配置百度地圖SDK

1. 申請(qǐng)密鑰

屏幕快照 2016-09-14 下午2.16.55.png

進(jìn)入應(yīng)用管理平臺(tái),點(diǎn)擊創(chuàng)建應(yīng)用

屏幕快照 2016-09-14 下午2.18.25.png
CC60DF7D-BFCC-4734-B8B7-4F8D258505D4.png

然后點(diǎn)擊提交 密鑰就申請(qǐng)成功了??!
在左側(cè)的查看應(yīng)用里面,就可以看到剛剛申請(qǐng)好的密鑰。

2. 下載百度地圖SDK

點(diǎn)擊左側(cè)的相關(guān)下載就可以看到

1CBF05D1-42B6-4A4C-9629-3DB2C12BC4A9.png

如果不需要參考示例代碼,只需要在自定義下載中下載開發(fā)包就可以了(話說(shuō)看了我的簡(jiǎn)書要示例代碼什么用!?。。?/p>

3. 配置開發(fā)環(huán)境

在左側(cè)的列表中可以看到注意事項(xiàng),一定要仔細(xì)閱讀,按照步驟添加就完全沒問(wèn)題的。
在這里我是用的cocoapods導(dǎo)入的SDK(推薦使用)
如果不明白步驟的同學(xué)可以看這里
猛戳?。。。。。。?!
這樣我們基本上配置好了百度地圖的SDK。

二. 顯示基本地圖

1.將百度地圖的SDK包拽入工程后,在appdelegate的頭文件中調(diào)用百度地圖,并且實(shí)例一個(gè)地圖屬性。

![59A7DB53-222F-4801-964D-0C043EA129C6.png](http://upload-images.jianshu.io/upload_images/2906579-2f1bf2ebba5ad52d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

然后初始化,調(diào)用方法注冊(cè)我們之前申請(qǐng)過(guò)的密鑰。

 BOOL ret=[_mapManager start:@"###################" generalDelegate:nil];
    if (!ret) {
        //如果地圖打開失敗嗎
        UIAlertView * alt=[[UIAlertView alloc]initWithTitle:NSLocalizedString(@"ditudakaishibai",@"") message:nil delegate:self cancelButtonTitle:NSLocalizedString(@"queren",@"") otherButtonTitles:nil, nil];
        [alt show];
    }

2. 在需要現(xiàn)實(shí)地圖的視圖中調(diào)用谷歌地圖,并遵守協(xié)議

BMKMapViewDelegate

初始化百度地圖并且設(shè)置相應(yīng)屬性

 _mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT/5*3)];
    _mapView.showMapScaleBar = YES;//比例尺
    [_mapView setMapType:BMKMapTypeStandard];//地圖的樣式(標(biāo)準(zhǔn)很高 地圖
    _mapView.delegate = self;
    [self.view addSubview:_mapView];

此時(shí)百度地圖就已經(jīng)可以出現(xiàn)在了我們的界面上?。。?/p>

三. 地圖相關(guān)功能的實(shí)現(xiàn)

1. 定位功能

調(diào)用頭文件

C96A21B4-5E3B-4C3A-9266-63E46D149A73.png

并遵守協(xié)議

CLLocationManagerDelegate,BMKLocationServiceDelegate

實(shí)例化定位管理器,并申請(qǐng)定位功能許可

 //定位管理器
_locationManager=[[CLLocationManager alloc]init];
//如果沒有授權(quán)則請(qǐng)求用戶授權(quán)
if ([CLLocationManager authorizationStatus]==kCLAuthorizationStatusNotDetermined){
        [_locationManager requestWhenInUseAuthorization];
    }

接下來(lái)我們將使用百度SDK所帶的定位方法進(jìn)行定位
設(shè)置定位的相關(guān)屬性

 _locService=[[BMKLocationService alloc]init];
        _locService.delegate=self;
        [_locService startUserLocationService];//激活定位狀態(tài)
        _mapView.showsUserLocation = NO;//先關(guān)閉顯示的定位圖層
        _mapView.userTrackingMode = BMKUserTrackingModeFollow;//(跟隨態(tài))
        _mapView.showsUserLocation = YES;//顯示定位圖層

此時(shí)定位功能已經(jīng)開啟,地圖正式開始定位了
接下來(lái)是定位的兩個(gè)代理方法

//用戶方向更新后自動(dòng)調(diào)用  百度地圖
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
    [_mapView updateLocationData:userLocation];
//    NSLog(@"heading is %@",userLocation.heading);
}

接下來(lái)是用戶位置更新后自動(dòng)調(diào)用,在這里我實(shí)現(xiàn)了幾個(gè)功能。

- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    [_mapView updateLocationData:userLocation];
}

當(dāng)用戶將當(dāng)前定位到的視圖移走,此時(shí)將不會(huì)跟隨定位,也就是不會(huì)在地圖上實(shí)時(shí)更新地理位置,也就是滑走地圖之后不會(huì)自己跑回來(lái)。實(shí)現(xiàn)方法是如果當(dāng)前是普通態(tài)就不進(jìn)行地圖的更新,并且如果移動(dòng)超過(guò)十米,那么地圖會(huì)恢復(fù)跟隨態(tài)實(shí)時(shí)更新地圖。

if ((_mapView.userTrackingMode == BMKUserTrackingModeNone)) {
        if ([self julioldlat:[[user valueForKey:@"weidu"] doubleValue]
                     oldlong:[[user valueForKey:@"jingdu"] doubleValue]
                      nowlat:userLocation.location.coordinate.latitude
                     nowlong:userLocation.location.coordinate.latitude]) {
             _mapView.userTrackingMode = BMKUserTrackingModeFollow;
        }
    }

里面調(diào)用了一個(gè)方法,通過(guò)經(jīng)緯度計(jì)算距離

-(BOOL)julioldlat:(double)oldlat oldlong:(double)oldlong nowlat:(double)nowlat nowlong:(double)nowlong
{
    BOOL JuLi=NO;
    CLLocation *orig=[[CLLocation alloc] initWithLatitude:oldlat  longitude:oldlong];
    CLLocation* dist=[[CLLocation alloc] initWithLatitude:nowlat longitude:nowlong];
CLLocationDistance kilometers=[orig distanceFromLocation:dist];
    NSLog(@"距離:%f",kilometers);
    if (kilometers>=10) {
        JuLi=YES;
    }
    else{
        JuLi=NO;
    }
    return JuLi;
}

接下來(lái)將得到的經(jīng)緯度點(diǎn)顯示在地圖的中心就可以了

BMKCoordinateRegion region;
//將定位的點(diǎn)居中顯示     region.center.latitude=userLocation.location.coordinate.latitude;
region.center.longitude=userLocation.location.coordinate.longitude;
 _mapView.centerCoordinate = userLocation.location.coordinate;

2. 獲取地理位置名稱

也就是反地理編碼,百度地圖自帶的回調(diào)方法
首先出入獲取到的經(jīng)緯度,調(diào)用方法判斷是否獲取成功

CLLocationDegrees latitude=[[NSUserDefaults standardUserDefaults] doubleForKey:@"weidu"];
    CLLocationDegrees longitude=[[NSUserDefaults standardUserDefaults] doubleForKey:@"jingdu"];
    CLLocation *location=[[CLLocation alloc]initWithLatitude:latitude longitude:longitude];
        BMKGeoCodeSearch * geosearch=[[BMKGeoCodeSearch alloc]init];
        geosearch.delegate=self;
        BMKReverseGeoCodeOption * regeosrarch=[[BMKReverseGeoCodeOption alloc]init];
        regeosrarch.reverseGeoPoint=location.coordinate;
        if ([geosearch reverseGeoCode:regeosrarch]) {
}

如果返回的是YES,那么在下面的回調(diào)方法中就可以得到地理位置名稱了

//反地理編碼結(jié)束后 返回結(jié)果   百度地圖
-(void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
    user=[NSUserDefaults standardUserDefaults];
    if (error) {
);
    }
    NSString * placename=result.address;
}

3.地圖覆蓋物

(1)BMKPointAnnotation 標(biāo)記

self.baiduPointAnnotation = [[BMKPointAnnotation alloc]init];
self.baiduPointAnnotation.coordinate = positionbaidu;
self.baiduPointAnnotation.title =[NSString stringWithFormat:@"%@:%@",dict1[@"site_name"],dict2[@"card_name"]];
[self.baiduMapView addAnnotation:self.baiduPointAnnotation];

(2)BMKCircle 圓

//  設(shè)置圓點(diǎn)和半徑
self.circle=[BMKCircle circleWithCenterCoordinate:positionbaidu radius:distance];
[self.baiduMapView addOverlay:self.circle];
//   協(xié)議方法
-(BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id<BMKOverlay>)overlay
{
    if ([overlay isKindOfClass:[BMKCircle class]])
    {
        BMKCircleView* circleView = [[BMKCircleView alloc] initWithOverlay:overlay];
        circleView.fillColor = [[UIColor colorWithRed:0.76 green:0.76 blue:0.76 alpha:0.3] colorWithAlphaComponent:0.5];
        circleView.strokeColor = [[UIColor colorWithRed:0.76 green:0.76 blue:0.76 alpha:0.9] colorWithAlphaComponent:0.5];
        circleView.lineWidth = 1.0;
        return circleView;
    }
    return nil;
}

(3)BMKPolyline 線(多點(diǎn)相連)

//  arr.count 需要連接的點(diǎn)的數(shù)量
CLLocationCoordinate2D coor[arr.count];
//  這里省略了for循環(huán)    將每一個(gè)點(diǎn)放入coor中
coor[i].latitude=curCoordinate2D.latitude;
coor[i].longitude=curCoordinate2D.longitude;

BMKPolyline * polyline=[BMKPolyline polylineWithCoordinates:coor count:arr.count];
[self.baiduMapView addOverlay:polyline];
//   協(xié)議方法
-(BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id<BMKOverlay>)overlay
{
    if ([overlay isKindOfClass:[BMKPolyline class]]) {
        BMKPolylineView * polylineView=[[BMKPolylineView alloc]initWithOverlay:overlay];
        polylineView.strokeColor=[[UIColor blueColor] colorWithAlphaComponent:1];
        polylineView.lineWidth=2.0;
        return polylineView;
    }
    return nil;
}

                                 ### 點(diǎn)個(gè)贊又不能懷孕
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容