优秀比不过别人,那么努力呢?

iOS成长之路 百度地图

 

使用cocospod导入百度地图  详情见文档

使用 百度地图 需要获取密钥  

 

 

2 配置 项目 

info中

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>
保证网络可用
 <key>LSApplicationQueriesSchemes</key>
    <array>
        <string>baidumap</string>
    </array>
保证百度地图可调用
导入功能文件头部
#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件
 
#import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件
 
#import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入检索功能所有的头文件
 
#import <BaiduMapAPI_Cloud/BMKCloudSearchComponent.h>//引入云检索功能所有的头文件
 
#import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的头文件
 
#import <BaiduMapAPI_Utils/BMKUtilsComponent.h>//引入计算工具所有的头文件
 
#import <BaiduMapAPI_Radar/BMKRadarComponent.h>//引入周边雷达功能所有的头文件
 
#import < BaiduMapAPI_Map/BMKMapView.h>//只引入所需的单个头文件


定位

初始化百度地图 以及 定位

mapView  =  [[BMKMapView alloc]initWithFrame:CGRectMake(0, 80, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 50)];

    

    [self.view addSubview: mapView];

    locService = [[BMKLocationService alloc]init];

设置代理

    locService.delegate = self;

    //启动LocationService

    [locService startUserLocationService];

    //以下_mapView为BMKMapView对象

    代理

#pragma mark - locationInfomation(用户位置信息)

/**

 *用户方向更新后,会调用此函数

 *@param userLocation 新的用户位置

 */

- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation

{

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        if(userLocation){

            NSLog(@"heading is %@",userLocation.heading);

        }

    });

}

 

//处理位置坐标更新  定位当前位置

- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation

{

    if(userLocation){

        static dispatch_once_t onceToken;

        dispatch_once(&onceToken, ^{

            //当前经纬度

            NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);

            _str = [NSMutableString stringWithFormat:@"%f,%f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude];

            self.latitude = userLocation.location.coordinate.latitude;

            self.longitude = userLocation.location.coordinate.longitude;

            //更新我的位置数据

            [mapView updateLocationData:userLocation];

            

            ///geo搜索服务 (将经纬度转化为地址,城市等信息,被称为反向地理编码)

            self.searcher = [[BMKGeoCodeSearch alloc]init];

            self.searcher.delegate = self;

            CLLocationCoordinate2D point = (CLLocationCoordinate2D){self.latitude,self.longitude};

            BMKReverseGeoCodeOption *reverseGeoCodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];

            reverseGeoCodeSearchOption.reverseGeoPoint = point;

            BOOL flag = [self.searcher reverseGeoCode:reverseGeoCodeSearchOption];

            if(flag)

            {

                NSLog(@"反geo检索发送成功");

            }

            else

            {

                NSLog(@"反geo检索发送失败");

            }

        });

    }

}

 

#pragma mark - onGetReverseGeoCodeResult(反向地理编码结果)

//接收反向地理编码结果

-(void) onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error

{

    if (error == BMK_SEARCH_NO_ERROR) {

        //这里打印出反向地理编码的结果,包括城市,地址等信息

        NSLog(@"测试结果 %@  %@",result.addressDetail.city,result.address);

        

        

        _placename.text = result.address;

    }

    else {

        NSLog(@"抱歉,未找到结果");

    }

}

 

#pragma mark - configDelegate(设置代理及取消代理)

//设置代理

-(void)viewWillAppear:(BOOL)animated

{

    [mapView viewWillAppear];

    mapView.delegate = self;

 

}

 

// 此处记得不用的时候需要置nil,否则影响内存的释放

-(void)viewWillDisappear:(BOOL)animated

{

    [mapView viewWillDisappear];

//    mapView.delegate = nil; // 不用时,置nil

    _searcher.delegate = nil;

    

}

 

#pragma mark - checkNetwork(检查网络及授权)

//检查网络状态

- (void)onGetNetworkState:(int)iError

{

    if (0 == iError) {

        NSLog(@"联网成功");

    }

    else{

        NSLog(@"onGetNetworkState %d",iError);

    }

    

}

 

 

 

#pragma mark - BMKMapViewDelegate(地图标注及路线颜色)

/**

 *根据overlay生成对应的View

 *@param mapView 地图View

 *@param overlay 指定的overlay

 *@return 生成的覆盖物View

 */

- (BMKOverlayView*)mapView:(BMKMapView *)map viewForOverlay:(id<BMKOverlay>)overlay

{

    if ([overlay isKindOfClass:[BMKPolyline class]]) {

        BMKPolylineView* polylineView = [[BMKPolylineView alloc] initWithOverlay:overlay];

        polylineView.fillColor = [[UIColor alloc] initWithRed:0 green:1 blue:1 alpha:1];

        polylineView.strokeColor = [[UIColor alloc] initWithRed:0 green:0 blue:1 alpha:0.7];

        polylineView.lineWidth = 3.0;

        return polylineView;

    }

    return nil;

}

 

 

    //开启跟随

    mapView.userTrackingMode = BMKUserTrackingModeFollow;

    mapView.showsUserLocation = YES;//显示定位图层

 

写的不是很好    给自己做个保存作用
posted @ 2016-05-12 17:07  一只卑微的蚂蚁  阅读(238)  评论(0编辑  收藏  举报