百度地图

<span style="font-size:24px;">1,显示当前位置的地图</span>
#import "ViewController2.h"
#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件
#import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件
#import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的头文件
#import <BaiduMapAPI_Utils/BMKUtilsComponent.h>//引入计算工具所有的头文件
#import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入检索功能所有的头文件
@interface ViewController2 ()<BMKMapViewDelegate,BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate>{
    UITableView *mytable;
    IBOutlet BMKMapView* _mapView;
    BMKLocationService *_locService;
    UILabel *lb;
    CLLocationDistance      l;
    CLLocationDistance      v;
    BMKGeoCodeSearch        *_search;
}


@end

@implementation ViewController2

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title =@"消息";
    mytable =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view                                                          .frame.size.height)];
    mytable.backgroundColor =[UIColor whiteColor];
    mytable.separatorStyle =UITableViewCellSeparatorStyleNone;
    [self.view addSubview:mytable];
    [self initSth];
    lb =[[UILabel alloc]initWithFrame:CGRectMake(20, 100, 280, 160)];
    lb.textColor =[UIColor blackColor];
    lb.textAlignment =NSTextAlignmentCenter;
    [mytable addSubview:lb];
    _mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [mytable addSubview:_mapView];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    _locService.delegate = self;
    _search.delegate = self;
    _mapView.delegate = self;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    _search.delegate = nil;
    _locService.delegate = nil;
    _mapView.delegate = self;
}

- (void)initSth{
    _locService = [[BMKLocationService alloc]init];
    _locService.delegate = self;
    [_locService startUserLocationService];
    _search = [[BMKGeoCodeSearch alloc]init];
    _search.delegate = self;
}

- (void)locateIf{
    NSString *addString = [[NSUserDefaults standardUserDefaults] objectForKey:@"addressString"];
    if (addString.length <= 0 || [addString isEqualToString:@"(null)"] || [addString isEqualToString:@"null"]) {
        lb.text = [NSString stringWithFormat:@""];
        [_locService startUserLocationService];
        [self startLocationServices];
    }else{
        lb.text = [NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"addressString"]];
    }
    [mytable reloadData];
    return;
}

- (void)startLocationServices{
    self.mapView.showsUserLocation = YES;
}

- (void)stopLocationServices{
    [_locService stopUserLocationService];
}

- (void)didFailToLocateUserWithError:(NSError *)error{
    NSLog(@"location error");
}

- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error{
    NSLog(@"%@",result);
    lb.text = [NSString stringWithFormat:@"%@",result.address];
    lb.text  = [NSString stringWithFormat:@"%@",result.address];
    [mytable reloadData];
    [self stopLocationServices];
}

- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation{
    NSLog(@"定位");
}

- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation{
    if (userLocation != nil) {
        l = _locService.userLocation.location.coordinate.latitude;//得到纬度
        v = _locService.userLocation.location.coordinate.longitude;//得到经度
        CLLocationCoordinate2D pt = (CLLocationCoordinate2D){l, v};//此处填写想要反地理编码的坐标
        BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];
        reverseGeocodeSearchOption.reverseGeoPoint = pt;
        BOOL flag = [_search reverseGeoCode:reverseGeocodeSearchOption];
        _mapView.centerCoordinate = userLocation.location.coordinate;
//        [_mapView setZoomLevel:15];
        if(flag){
            NSLog(@"反geo检索发送成功");
        }
        else{
            NSLog(@"反geo检索发送失败");
        }
    }
}

@end


2,显示定位

#import "ViewController3.h"
#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件
#import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件
#import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的头文件
#import <BaiduMapAPI_Utils/BMKUtilsComponent.h>//引入计算工具所有的头文件
#import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入检索功能所有的头文件
@interface ViewController3 ()<BMKMapViewDelegate,BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate>{
    UITableView *mytable;
    IBOutlet BMKMapView* _mapView;
    BMKLocationService *_locService;
    UILabel *lb;
    CLLocationDistance      l;
    CLLocationDistance      v;
    BMKGeoCodeSearch        *_search;
}


@end

@implementation ViewController3

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title =@"消息";
    mytable =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, self.view                                                          .frame.size.height)];
    mytable.backgroundColor =[UIColor whiteColor];
    mytable.separatorStyle =UITableViewCellSeparatorStyleNone;
    [self.view addSubview:mytable];
    [self initSth];
    lb =[[UILabel alloc]initWithFrame:CGRectMake(20, 100, 280, 160)];
    lb.textColor =[UIColor blackColor];
    lb.textAlignment =NSTextAlignmentCenter;
    [mytable addSubview:lb];
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    _locService.delegate = self;
    _search.delegate = self;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    _search.delegate = nil;
    _locService.delegate = nil;
}

- (void)initSth{
    _locService = [[BMKLocationService alloc]init];
    _locService.delegate = self;
    [_locService startUserLocationService];
    _search = [[BMKGeoCodeSearch alloc]init];
    _search.delegate = self;
}

- (void)locateIf{
    NSString *addString = [[NSUserDefaults standardUserDefaults] objectForKey:@"addressString"];
    if (addString.length <= 0 || [addString isEqualToString:@"(null)"] || [addString isEqualToString:@"null"]) {
        lb.text = [NSString stringWithFormat:@""];
        [_locService startUserLocationService];
        [self startLocationServices];
    }else{
        lb.text = [NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"addressString"]];
    }
    [mytable reloadData];
    return;
}

- (void)startLocationServices{
    self.mapView.showsUserLocation = YES;
}

- (void)stopLocationServices{
    [_locService stopUserLocationService];
}

- (void)didFailToLocateUserWithError:(NSError *)error{
    NSLog(@"location error");
}

- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error{
    NSLog(@"%@",result);
    lb.text = [NSString stringWithFormat:@"%@",result.address];
    lb.text  = [NSString stringWithFormat:@"%@",result.address];
    [mytable reloadData];
    [self stopLocationServices];
}

- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation{
    NSLog(@"定位");
}

- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation{
    if (userLocation != nil) {
        l = _locService.userLocation.location.coordinate.latitude;//得到纬度
        v = _locService.userLocation.location.coordinate.longitude;//得到经度
        CLLocationCoordinate2D pt = (CLLocationCoordinate2D){l, v};//此处填写想要反地理编码的坐标
        BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];
        reverseGeocodeSearchOption.reverseGeoPoint = pt;
        BOOL flag = [_search reverseGeoCode:reverseGeocodeSearchOption];
        if(flag){
            NSLog(@"反geo检索发送成功");
        }
        else{
            NSLog(@"反geo检索发送失败");
        }
    }
}

@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值