百度地图的一些坑

  1. 在Appdelegate中注册 百度地图 Api 填写你在百度地图申请的budleid

_mapManager = [[BMKMapManager alloc]init];

// 如果要关注网络及授权验证事件,请设定    generalDelegate参数

BOOL ret = [_mapManager start:@" "  generalDelegate:nil];

if (!ret) {

NSLog(@"manager start failed!");

}

2. 画地图的

_mapView = [[BMKMapView alloc]init];

//    _mapView.delegate = self;

_mapView.mapType = BMKMapTypeStandard;

//    _mapView.zoomEnabled = YES;

_mapView.gesturesEnabled = YES;

_mapView.scrollEnabled = YES;

地图的精度

_mapView.zoomLevel = 15;

_mapView.showsUserLocation = NO;

/***动态定制我的位置样式        */

BMKLocationViewDisplayParam *displayParam = [[BMKLocationViewDisplayParam alloc] init];

displayParam.locationViewOffsetX=0;//定位偏移量(经度)

displayParam.locationViewOffsetY=0;//定位偏移量(纬度)

displayParam.isAccuracyCircleShow=NO;//经度圈是否显示

//这里替换自己的图标路径,必须把图片放到百度地图SDK的Resources/mapapi.bundle/images 下面

//还有一种方法就是获取到_locationView之后直接设置图片

displayParam.locationViewImgName=@"hzb_dtdw_dw";

[_mapView updateLocationViewWithParam:displayParam];

[self.view addSubview:_mapView];

_mapView.sd_layout.leftEqualToView(self.view).rightEqualToView(self.view).topEqualToView(self.view).bottomEqualToView(self.view);

3 、地图画好之后 如果需要定位的话就 检索当前的位置  获取当前的位置 在更新到地图的中心点坐标 就是了 百度地图有个定位的功能 BMKLocationService 这个类里设置定位  重点。。。。需要在 plist文件中设置是否需要定位 做项目的时候忘了这个 配置 定位没出来   多注意看官方文档才是关键  类推的还有一个是导航的功能 也需要配置plist文件 导航的直接调用百度的导航功能就是了

/**

*打开定位服务

*需要在info.plist文件中添加(以下二选一,两个都添加默认使用NSLocationWhenInUseUsageDescription):

*NSLocationWhenInUseUsageDescription 允许在前台使用时获取GPS的描述

*NSLocationAlwaysUsageDescription 允许永远可获取GPS的描述

*/

-(void)startUserLocationService;

4. 检索位置 怎么获取自己的位置呢  就是这个类 BMKLocationService 

(1)

_locationSevice = [[BMKLocationService alloc]init];

//    _locationSevice.delegate = self;

_mapView.userTrackingMode = BMKUserTrackingModeNone;

_mapView.showsUserLocation = YES;

[_locationSevice setDesiredAccuracy:kCLLocationAccuracyBest];

//    [_locationManger requestAlwaysAuthorization];

_locationSevice.distanceFilter =kCLDistanceFilterNone;

开始定位了

[_locationSevice startUserLocationService];

(2)定位的结果在这里奥

// 位置更新后

-(void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation{

NSLog(@"----%@",userLocation.location);

_mapView.centerCoordinate = userLocation.location.coordinate;

跟新我的位置

[_mapView updateLocationData:userLocation];

}

5 .关于检索 分为正检索和反检索 就是根据你的坐标获取你所在的地址名 叫正检索 反检索 就是根据地址名获取所在的坐标 BMKGeoCodeSearch 用到的这个类

(1)/**

*根据地址名称获取地理信息

*异步函数,返回结果在BMKGeoCodeSearchDelegate的onGetAddrResult通知

*@param geoCodeOption      geo检索信息类

*@return 成功返回YES,否则返回NO

*/

- (BOOL)geoCode:(BMKGeoCodeSearchOption*)geoCodeOption;

输入地址名

(2)结果 

/**

*返回地址信息搜索结果

*@param searcher 搜索对象

*@param result 搜索结BMKGeoCodeSearch果

*@param error 错误号,@see BMKSearchErrorCode

*/

- (void)onGetGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error;

对应的根据坐标获取地址名的 同理

6.自定义大头针   大头针需要自定义

(1)

-(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id)annotation{

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

NSString *AnnotationViewID = @"annotationViewID";

BMKPinAnnotationView * annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];

// 从天上掉下效果

annotationView.animatesDrop = NO;

// 设置可拖拽

annotationView.draggable = NO;

UIImage * locaImage = [UIImage imageNamed:@"dw_gj_icon_ls"];

annotationView.annotation = annotation;

annotationView.image = locaImage;

UIView *popView  = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 250, 100)];

UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"dw_gj_srk"]];

image.frame = CGRectMake(0, 0, 250, 100);

image.contentMode = UIViewContentModeScaleToFill;

[popView addSubview:image];

//自定义显示的内容

UILabel *driverName = [[UILabel alloc]initWithFrame:CGRectMake(0, 3, 250, 20)];

driverName.text = [NSString stringWithFormat:@"当前位置%@",(_trajectoryDatemodel.Track[0])[@"Place"]];

driverName.backgroundColor = [UIColor clearColor];

driverName.font = [UIFont systemFontOfSize:12.5];

driverName.textColor = [UIColor whiteColor];

driverName.textAlignment = NSTextAlignmentLeft;

[popView addSubview:driverName];

UILabel *carName = [[UILabel alloc]initWithFrame:CGRectMake(0, 25, 250, 20)];

carName.text = [NSString stringWithFormat:@"目的地%@",_EndString];

carName.backgroundColor = [UIColor clearColor];

carName.font = [UIFont systemFontOfSize:12.5];

carName.textColor = [UIColor whiteColor];

carName.textAlignment = NSTextAlignmentLeft;

[popView addSubview:carName];

UILabel *Timelabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 51, 250, 20)];

Timelabel.text =_TimeString;

Timelabel.backgroundColor = [UIColor clearColor];

Timelabel.font = [UIFont systemFontOfSize:12.5];

Timelabel.textColor = [UIColor whiteColor];

Timelabel.textAlignment = NSTextAlignmentLeft;

[popView addSubview:Timelabel ];

BMKActionPaopaoView *pView = [[BMKActionPaopaoView alloc]initWithCustomView:popView];

pView.frame = CGRectMake(0, 0, 250, 100);

((BMKPinAnnotationView*)annotationView).paopaoView = nil;

((BMKPinAnnotationView*)annotationView).paopaoView = pView;

return annotationView;

}

return nil;

}

(2)大头针的点击事件

//点击

-(void)mapView:(BMKMapView *)mapView annotationViewForBubble:(BMKAnnotationView *)view{

if ([view.annotation isKindOfClass:[Newpoint class]]) {

Newpoint *anno = (Newpoint*)view.annotation;

点击事件这里写 传值什么的

[self OnTouchPoint];

}

}

7 .还有一个计算轨迹的 把所得的大头针都展示在列表上 然后在自定义大头针的时候判断第一个和别的不一样就是了

BMKPointAnnotation  *pointAnnotation = [[BMKPointAnnotation alloc]init];

CLLocationCoordinate2D coor;

coor.latitude = [(_trajectoryDatemodel.Track[0])[@"Latitude"] doubleValue];

coor.longitude = [(_trajectoryDatemodel.Track[0])[@"Longitude"] doubleValue];

pointAnnotation.coordinate = coor;

pointAnnotation.title =(_trajectoryDatemodel.Track[0])[@"Place"];

[_TrajectoryMapView addAnnotation:pointAnnotation];

[_TrajectoryMapView selectAnnotation:pointAnnotation animated:YES];

_TrajectoryMapView.centerCoordinate = (CLLocationCoordinate2D){[(_trajectoryDatemodel.Track[0])[@"Latitude"] doubleValue],[(_trajectoryDatemodel.Track[0])[@"Longitude"] doubleValue]};

_TrajectoryMapView.delegate = self;

CLLocationCoordinate2D coords[100] = {0};

for (int i=0; i<_trajectoryDatemodel.Track.count; i++) {

double Latitude  = [(_trajectoryDatemodel.Track[i])[@"Latitude"] doubleValue] ;

double Longitude = [(_trajectoryDatemodel.Track[i])[@"Longitude"] doubleValue]  ;

coords[i].latitude = Latitude ;

coords[i].longitude = Longitude ;

BMKPointAnnotation *point  = [[BMKPointAnnotation alloc]init];

point.coordinate = {coords[i].latitude,coords[i].longitude};

point.title =(_trajectoryDatemodel.Track[i])[@"Place"];

[_TrajectoryMapView addAnnotation:point];

}

你可能感兴趣的:(百度地图的一些坑)