<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