最近仿照美团做了款应用,刚好用到百度地图,高德地图之前用的比较多,只是这个项目的后台服务器是另外一个公司做的,他们用的就是百度地图,现在网上用百度地图的还不算太多,博文也是断断续续的,主要是中间跳跃有点大,没有可运行的demo,看不到效果。纠结了好久,结合别人的总结和从百度官网上下载的demo,详细记录一下百度地图的使用过程,分享一下,也便于以后查阅使用。
百度地图的使用和苹果一样,相对于高德地图来说规范了好多。直接将程序的bundle Identity和appkey值绑定,虽然刚开始用感觉有点不习惯,相信规范严谨对于国内的程序开发也算是一种引导吧。闲言少絮,下面开始百度地图的使用说明。
第二步:将百度地图sdk包添加到程序,在AppDelegate导入
import<CoreLocation/CoreLocation.h>
import "AGViewDelegate.h"
import "BMapKit.h"
上述文件,添加如下委托和对象
@interface AppDelegate : UIResponder <UIApplicationDelegate,CLLocationManagerDelegate,BMKGeneralDelegate>
{
AGViewDelegate _viewDelegate;//
BMKMapManager _mapManager;//百度地图管理器
}
并在
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中调用开启百度地图定位的方法
//地图 - (void)configureAPIKey
{
_mapManager = [[BMKMapManager alloc] init];
BOOL ret = [_mapManager start:@"appkey"generalDelegate:self];
if (ret)
{
NSLog(@"百度地图开启定位:");
}
}
第三步:百度地图视图和百度位置服务的使用
在使用地图的页面导入百度地图文件
import "BMapKit.h"
添加
BMKMapViewDelegate,BMKLocationServiceDelegate委托对象
@property(nonatomic,retain)BMKMapView* baiduMapView;//地图视图
@property(nonatomic,retain)BMKLocationService* locationService;//百度地图定位服务对象
@synthesize baiduMapView;
@synthesize locationService;
当然不用的时候需要释放掉
if (baiduMapView)
{
[baiduMapView release];
baiduMapView = nil;
}
if (locationService)
{
[locationService release];
locationService = nil;
}
-
(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
baiduMapView.delegate = self;
locationService.delegate = self;
}
-(void)viewWillDisappear:(BOOL)animated
{
[baiduMapView viewWillDisappear];
baiduMapView.delegate = nil;
locationService.delegate = nil;[super viewWillDisappear:animated];
}
当然在使用地图视图控件之前需要先开启百度地图的定位服务
//初始化定位服务对象
self.locationService = [[[BMKLocationService alloc]init]autorelease];
locationService.delegate=self;
//显示定位的蓝点儿必须先开启定位服务
[locationService startUserLocationService];
接下来就是地图视图控件使用了
self.baiduMapView = [[[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)]autorelease];
//初始化百度地图对象
baiduMapView.delegate = self;
[baiduMapView setMapType:BMKMapTypeStandard];
baiduMapView.zoomLevel=17;//地图级别
//显示定位的蓝点儿必须先开启定位服务
[locationService startUserLocationService];
[baiduMapView setShowsUserLocation:NO];
baiduMapView.userTrackingMode=BMKUserTrackingModeNone;//地图模式
[baiduMapView setShowsUserLocation:YES];//显示定位的蓝点儿
coordinate.latitude = [XtomFunction xfuncGetAppdelegate].currentLocation.coordinate.latitude;//纬度
coordinate.longitude = [XtomFunction xfuncGetAppdelegate].currentLocation.coordinate.longitude;//经度
BMKCoordinateRegion viewRegion = BMKCoordinateRegionMake(coordinate, BMKCoordinateSpanMake(0.3,0.3));
BMKCoordinateRegion adjustedRegion = [baiduMapView regionThatFits:viewRegion];
[baiduMapView setRegion:adjustedRegion animated:YES];
到这里已经可以展示地图信息数据了,当然一些复杂的东西需要下面的委托实现
第四步:百度地图中常用委托方法的使用
/**
*用户方向更新后,会调用此函数
*@param userLocation 新的用户位置
*/
-
(void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{[baiduMapView updateLocationData:userLocation];
NSLog(@"heading is %@,%f,%f,%f,%f,%f,%f",userLocation.heading,userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude,localLatitude,localLongitude,coordinate.latitude,coordinate.longitude);
NSLog(@"dingweifuwu:%f,%f",locationService.userLocation.location.coordinate.latitude,locationService.userLocation.location.coordinate.longitude);NSLog(@"dangqian:%f,%f",locationService.userLocation.location.coordinate.latitude,locationService.userLocation.location.coordinate.longitude);
/*
//改变成百度经纬度
AppDelegate myDelegate = [XtomFunction xfuncGetAppdelegate];
myDelegate.currentLocation = userLocation.location;
myDelegate.myCoordinate=CLLocationCoordinate2DMake(userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude);/
}
//开始缩放委托 -
(void)mapView:(BMKMapView )mapView regionWillChangeAnimated:(BOOL)animated;
{
NSString showmeg = [NSString stringWithFormat:@"地图区域发生了变化(x=%d,y=%d,\r\nwidth=%d,height=%d).\r\nZoomLevel=%d;RotateAngle=%d;OverlookAngle=%d",(int)baiduMapView.visibleMapRect.origin.x,(int)baiduMapView.visibleMapRect.origin.y,(int)baiduMapView.visibleMapRect.size.width,(int)baiduMapView.visibleMapRect.size.height,(int)baiduMapView.zoomLevel,baiduMapView.rotation,baiduMapView.overlooking];NSLog(@"gesturescale:%@",showmeg);
CLLocationCoordinate2D firstLocation=[baiduMapView convertPoint:CGPointMake(0, 0) toCoordinateFromView:baiduMapView];
CLLocationCoordinate2D secondLocation=[baiduMapView convertPoint:CGPointMake(0, UI_View_Hieght+64) toCoordinateFromView:baiduMapView];
distance=[XtomFunction distanceBetweenOrderBy:firstLocation.latitude :firstLocation.longitude :secondLocation.latitude :secondLocation.longitude]/2;
//第一次获取数据的时候不加载
if (isFirstLoad)
{
localLatitude=baiduMapView.centerCoordinate.latitude;
localLongitude=baiduMapView.centerCoordinate.longitude;
//重新获取数据
[self requestGetLocationList];
}
}
//原理类似 UITableView 循环委托加载 CellforRowWithIndexPath
-
(BMKAnnotationView )mapView:(BMKMapView )view viewForAnnotation:(id <BMKAnnotation>)annotation
{
//商家详情
if (isShangjia)
{
if ([annotation isKindOfClass:[BMKPointAnnotation class]])
{
// 生成重用标示identifier
NSString AnnotationViewID = @"shangjia";
// 检查是否有重用的缓存
BMKAnnotationView annotationView = [baiduMapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
// 缓存没有命中,自己构造一个,一般首次添加annotation代码会运行到此处
if (annotationView == nil)
{ annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID]; ((BMKPinAnnotationView)annotationView).pinColor = BMKPinAnnotationColorRed;
// 设置重天上掉下的效果(annotation)
((BMKPinAnnotationView)annotationView).animatesDrop = YES; }
// 设置位置
annotationView.centerOffset = CGPointMake(0, -(annotationView.frame.size.height * 0.5));annotationView.annotation = annotation;
// 单击弹出泡泡,弹出泡泡前提annotation必须实现title属性
annotationView.canShowCallout = YES;
//美食
if ([activityType isEqualToString:@"美食"])
{
annotationView.image=[UIImage imageNamed:@"美食图标.png"];
}
//丽人
else if ([activityType isEqualToString:@"丽人"])
{
annotationView.image=[UIImage imageNamed:@"丽人图标.png"];
}
//生活服务
else if ([activityType isEqualToString:@"生活服务"])
{
annotationView.image=[UIImage imageNamed:@"生活服务图标.png"];
}
//休闲娱乐
else if ([activityType isEqualToString:@"休闲娱乐"])
{
annotationView.image=[UIImage imageNamed:@"休闲娱乐图标.png"];
}
//旅游
else if ([activityType isEqualToString:@"旅游"])
{
annotationView.image=[UIImage imageNamed:@"旅游图标.png"];
}
//购物
else if ([activityType isEqualToString:@"购物"])
{
annotationView.image=[UIImage imageNamed:@"购物图标.png"];
}
//酒店
else if ([activityType isEqualToString:@"酒店"])
{
annotationView.image=[UIImage imageNamed:@"酒店图标.png"];
}
//今日订单
else if ([activityType isEqualToString:@"今日订单"])
{
annotationView.image=[UIImage imageNamed:@"今日订单图标.png"];
}
//annotationView.image=[UIImage imageNamed:@"丽人图标.png"];// 设置是否可以拖拽 annotationView.draggable = NO; return annotationView; /* BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"]; newAnnotationView.pinColor = BMKPinAnnotationColorPurple; newAnnotationView.animatesDrop = YES;// 设置该标注点动画显示 newAnnotationView.annotation=annotation; newAnnotationView.image = [UIImage imageNamed:@"丽人图标.png"]; //把大头针换成别的图片 return newAnnotationView;*/ }
}
else if (isLuxian)
{
if ([annotation isKindOfClass:[RouteAnnotation class]])
{
return [self getRouteAnnotationView:view viewForAnnotation:(RouteAnnotation*)annotation];
}
return nil;
}
}
这里介绍一下百度地图中常见的两个点,花费了我好长的时间总结出来的,分享一下吧。比如获取百度地图中当前的位置,可以在开启百度位置服务,走完 -
(void)didUpdateUserHeading:(BMKUserLocation )userLocation委托方法后,通过
locationService.userLocation.location.coordinate获得
再比如 想获取当前地图的中心点的经纬度 可以这样获得baiduMapView.centerCoordinate 还有百度地图中坐标点和地图中经纬度的转换
CLLocationCoordinate2D firstLocation=[baiduMapView convertPoint:CGPointMake(0, 0) toCoordinateFromView:baiduMapView];这个是远点的经纬度
第四步:自定义标注的添加
//添加大头钉
BMKPointAnnotation annotation = [[BMKPointAnnotation alloc]init];CLLocationCoordinate2D coor; coor.latitude = localLatitude; coor.longitude = localLongitude; annotation.coordinate = coor; //annotation.title = @"这里是北京"; [baiduMapView addAnnotation:annotation];
结合
//原理类似 UITableView 循环委托加载 CellforRowWithIndexPath
(BMKAnnotationView *)mapView:(BMKMapView *)view viewForAnnotation:(id <BMKAnnotation>)annotation委托方法实现,当然还有个获取选中哪个大头钉或者标注点的委托方法
//当选中一个annotation views时,调用此接口-
(void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(CustomAnnotationView *)view
{
//只展示首页的大头钉
if (isLuxian||isShangjia)
{
return;
}
//获取点击视图的区域
CLLocationCoordinate2D leftTopLocation=[baiduMapView convertPoint:CGPointMake(view.frame.origin.x, view.frame.origin.y) toCoordinateFromView:baiduMapView];
CLLocationCoordinate2D rightDownLocation=[baiduMapView convertPoint:CGPointMake(view.frame.origin.x+view.frame.size.width, view.frame.origin.y+view.frame.size.height) toCoordinateFromView:baiduMapView];//临时数组
NSMutableArray *tempArray=[[[NSMutableArray alloc]init]autorelease];
for (int i = 0; i<detailData.count; i++)
{
NSMutableDictionary *dict = [[[NSMutableDictionary alloc] init] autorelease];
NSDictionary *temDict = [detailData objectAtIndex:i];
for(NSString *key in temDict.allKeys)
{
NSString *value = [temDict objectForKey:key];
if(![XtomFunction xfunc_check_strEmpty:value])
{
[dict setObject:value forKey:key];
}
}
//纬度
float tempLat=[[temDict objectForKey:@"lat"]floatValue];
//经度
float tempLng=[[temDict objectForKey:@"lng"]floatValue];NSLog(@"jingweidu:%f,%f,%f,%f,%f,%f",leftTopLocation.latitude,leftTopLocation.longitude,rightDownLocation.latitude,rightDownLocation.longitude,tempLat,tempLng); //获取该区域下的数据 if (tempLat >=rightDownLocation.latitude&&tempLat <=leftTopLocation.latitude&&tempLng >=leftTopLocation.longitude&&tempLng <=rightDownLocation.longitude) { [tempArray addObject:temDict]; }
}
NSLog(@"shuzu:%@",tempArray);
[view.superview insertSubview:view atIndex:1000];
view.temArr=tempArray;
view.biaozhu.dataSource=tempArray;
[view loadTableData];
[view retain];
}也一并分享一下吧。
第五步:百度地图中公交线路信息的获取和公交路线的绘制
公交、地铁、汽车、步行这几个是常见的东西这个比较麻烦一些,感兴趣的可以去百度地图的demo中查询,我给出链接吧。http://download.csdn.net/detail/kuuailetianzi/7907889 至此,百度地图的使用介绍的差不多了,我也该休息休息了。