分享

百度地图简单使用

 稀世珍藏 2016-10-24

一、根据所需功能引入对应头文件

//#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>//只引入所需的单个头文件


#import "MyAnnotation.h"//大头针类

#import "MyAnnotationView.h"//大头针视图类

二、遵守的代理及定义的全局变量

@interface ClassObj () <BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate,BMKMapViewDelegate>

{

    BMKMapView* _mapView;    

    CLLocationCoordinate2D corcar;

    BMKUserLocation *userCoodiLocation;

    BMKGeoCodeSearch *_geoSearcher;

}

@property (nonatomic, strong) BMKLocationService *localService;



三、地图的基本设置、反地理编码、大头针视图自定义


-(BMKLocationService *)localService{

    if (!_localService) {

        _localService = [[BMKLocationService alloc] init];

        [_localService setDesiredAccuracy:kCLLocationAccuracyBest];//设置定位精度

    }

    return _localService;

}

- (void)viewDidLoad {

    [super viewDidLoad];

    // 设置地图

    [self setUpBaiduMapView];

    // 反地理编码

    [self requestAddressAndTime];

}

// 设置地图

- (void)setUpBaiduMapView{

    

    _mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 120+64, SizeWidth, Sizeheight-64-120-170)];

    _mapView.delegate = self;

    //_mapView.showMapScaleBar = YES;

    [_mapView setZoomLevel:15];

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

    //_mapView.userTrackingMode = BMKUserTrackingModeFollowWithHeading;//定位罗盘模式

    [self.view addSubview:_mapView];

    self.localService.delegate = self;

    [self.localService startUserLocationService];//用户开始定位


    //在地图上添加按钮的方法

    manButton = [UIButton buttonWithType:UIButtonTypeCustom];

    manButton.frame = CGRectMake(10, _mapView.frame.size.height-40, 30, 30);

    [manButton setBackgroundImage:[UIImage imageNamed:@"LocatForMan"] forState:UIControlStateNormal];

    [_mapView addSubview:manButton];

    [_mapView bringSubviewToFront:manButton];//在地图上添加按钮的方法

    [manButton addTarget:self action:@selector(manbuttonclick) forControlEvents:UIControlEventTouchUpInside];

}


-(void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];

    [_mapView viewWillAppear];

    _mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放

}

-(void)viewWillDisappear:(BOOL)animated {

    [super viewWillDisappear:animated];

    [_localService stopUserLocationService];

    [_mapView viewWillDisappear];

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

}

- (void)dealloc {

    if (_mapView) {

        _mapView = nil;

    }

}

-(void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation

{

    //NSLog(@"当前位置%f,%f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);

    [_mapView updateLocationData:userLocation];

    userCoodiLocation = userLocation;

    

}

//修改大图针视图  在第四步中会重写大头针与大头针视图代码

- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation

{

    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {

        MyAnnotationView *newAnnotationView = [[MyAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];

        return newAnnotationView;

    }

    return nil;

}

//初次进入地图,将地图定位到自己最好在地图加载完成方法中实现

- (void)mapViewDidFinishLoading:(BMKMapView *)mapView

{

    [_mapView setCenterCoordinate:userCoodiLocation.location.coordinate];

}

// 查询定位地址跟时间 拿到坐标后反地理编码  然后标注大头针

- (void)requestAddressAndTime{

                CLLocationCoordinate2D coordinateCar = CLLocationCoordinate2DMake([string doublevalue],[string doublevalue]);

                NSDictionary* testdic = BMKConvertBaiduCoorFrom(coordinateCar,BMK_COORDTYPE_COMMON);

                //转换GPS坐标至百度坐标

                testdic = BMKConvertBaiduCoorFrom(coordinateCar,BMK_COORDTYPE_GPS);

                coordinateCar = BMKCoorDictionaryDecode(testdic);

                corcar = coordinateCar;

                

                _geoSearcher =[[BMKGeoCodeSearch alloc]init];

                _geoSearcher.delegate = self;

                

                //发起反向地理编码检索

                

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

                reverseGeoCodeSearchOption.reverseGeoPoint = coordinateCar;

                BOOL flag = [_geoSearcher reverseGeoCode:reverseGeoCodeSearchOption];

                if(flag)

                {

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

                }

                else

                {

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

                }

}

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

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

{

    if (error == BMK_SEARCH_NO_ERROR) {

        //      在此处理正常结果

        DLog(@"%@", result);

        label_address.text = result.address;

        // 添加一个PointAnnotation

        MyAnnotation* annotation = [[MyAnnotation alloc]init];

        annotation.coordinate = corcar;

        annotation.title = result.address;

        [_mapView addAnnotation:annotation];

        

    } else {

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

    }

}

四、重写大头针及大头针视图


1、大头针MyAnnotation.h中代码.m中不用改

#import <Foundation/Foundation.h>

#import <BaiduMapAPI_Map/BMKPointAnnotation.h>

@interface MyAnnotation : BMKPointAnnotation

/**

 *  图标

 */

@property (nonatomic, copy) NSString *icon;

@end

2、大头针视图MyAnnotationView.h中代码

#import <BaiduMapAPI_Map/BMKMapView.h>

#import <BaiduMapAPI_Map/BMKAnnotationView.h>

@interface MyAnnotationView : BMKAnnotationView

/**

 *  创建方法

 *

 *  @param mapView 地图

 *

 *  @return 大头针

 */+ (instancetype)annotationViewWithMap:(BMKMapView *)mapView;

@end

3、大头针视图MyAnnotationView.m中代码

#import "MyAnnotationView.h"

#import "MyAnnotation.h"

@implementation MyAnnotationView

- (instancetype)initWithAnnotation:(id<BMKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier

{

    if (self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) {

        

    }

    return self;

}

+ (instancetype)annotationViewWithMap:(BMKMapView *)mapView

{

    static NSString *identifier = @"anno";

    // 1.从缓存池中取

    MyAnnotationView *annoView = (MyAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

    // 2.如果缓存池中没有, 创建一个新的

    if (annoView == nil) {

        annoView = [[MyAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:identifier];

    }  

    return annoView;

}

- (void)setAnnotation:(MyAnnotation *)annotation

{

    [super setAnnotation:annotation];

    //设置图标

    self.image = [UIImage imageNamed:@"CarLocation@2x"];

}

@end


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多