百度地图

1.集成
  • 1.如果地图显示不出来,出现网格
由于百度地图的key与app的bundle id不匹配导致的
  • 2.配置注意: 
2.定位,需要注意的:必须地图加载完成再定位,否则会失败
3.移动地图调用的方法
/**
 * 点击地图空白处会回调此接口
 *@param mapview 地图View
 *@param coordinate 空白处坐标点的经纬度
 */
- (void)mapView:(BMKMapView *)mapView onClickedMapBlank:(CLLocationCoordinate2D)coordinate;

// 当点击annotation view弹出的泡泡时,调用此接口
- (void)mapView:(BMKMapView *)mapView annotationViewForBubble:(BMKAnnotationView *)view;

/**
 * 当选中一个大头针时,调用此接口,但是如果气泡显示就不会调用
 * @param mapView 地图View
 * @param view 选中的annotation views
 */
- (void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)view;

//添加气泡会调用的方法
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation;

/**
 *地图初始化完毕时会调用此接口
 *@param mapView 地图View
 */
- (void)mapViewDidFinishLoading:(BMKMapView *)mapView;

/**
 * 正在移动地图的时候调用
 *@param mapView 地图View
 */
- (void)mapStatusDidChanged:(BMKMapView *)mapView;

/**
 * 移动地图完成调用
 *@param mapView 地图View
 *@param animated 是否动画
 */
- (void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated;
 
4.计算2点间的距离
//计算2点间的距离
- (double)dealWithTwoPointDistance:(CLLocationCoordinate2D)beginPoint endPoint:(CLLocationCoordinate2D)endPoint
{
    BMKMapPoint point1 = BMKMapPointForCoordinate(CLLocationCoordinate2DMake(beginPoint.latitude, beginPoint.longitude));
    
    BMKMapPoint point2 = BMKMapPointForCoordinate(CLLocationCoordinate2DMake(endPoint.latitude, endPoint.longitude));
    
    CLLocationDistance distance = BMKMetersBetweenMapPoints(point1,point2);
    
    return distance;
}

5.修改气泡的位置

(1)自定义个类,继承于BMKActionPaopaoView
#import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件

@interface LyActionPaopaoView : BMKActionPaopaoView

@end
(2)重写setframe的方法
 这样设置,就是让气泡位置固定在某个特定位置
#import "LyActionPaopaoView.h"

@implementation LyActionPaopaoView

- (void)setFrame:(CGRect)frame
{
    //ScreenHeight - NavBarHeight - 40 - BottomHeight这个是地图的高度
    CGRect rect = CGRectMake(10, ScreenHeight - NavBarHeight - 40 - BottomHeight - 100, [UIScreen mainScreen].bounds.size.width - 20, 85);
    [super setFrame:rect];
}

@end
6.隐藏气泡
 [mapView deselectAnnotation:(BMKAnnotationView *)view.annotation animated:YES];

* 取消所有标注的选中状态
    for (id <BMKAnnotation> annotation in _mapView.annotations) {
        [_mapView deselectAnnotation:annotation animated:NO];
    } 

7.移动的时候隐藏气泡

 

8.添加多个大头针

9.自定义大头针

  • 1.继承于BMKAnnotationView
#import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件
@class PlayGroundMainModel;

@protocol LyAnnotationViewDelegate <NSObject>

@optional
- (void)lyAnnotationViewTapIcon:(NSInteger)tag;
@end

@interface LyAnnotationView : BMKAnnotationView

//控件
@property(nonatomic,strong)UIImageView *icon;

//初始化传入的数据
@property(nonatomic,strong)PlayGroundMainModel *model;
@property(nonatomic,assign)BOOL isSelectIcon;//是否选中
@property(nonatomic,assign)NSInteger  iconTag;//给头像icon的标识,第几个

@property(nonatomic,weak)id<LyAnnotationViewDelegate> delegate;

//1.初始化方法
+ (instancetype)annotationViewWithMapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation;

@end
//类似于自定义cell
+ (instancetype)annotationViewWithMapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation
{
    static NSString *pinID = @"LyBaiduMapManager";
    // 从缓存池取出大头针数据视图
    LyAnnotationView *customView = [mapView dequeueReusableAnnotationViewWithIdentifier:pinID];
    // 如果取出的为nil , 那么就手动创建大头针视图
    if (customView == nil) {
        customView = [[LyAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinID];
    }
    return customView;
}

- (id)initWithAnnotation:(id<BMKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) {
        self.annotation = annotation;
        self.canShowCallout = NO;
        self.enabled = NO;
        [self addSubview:self.icon];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.icon.bounds = self.bounds;
}

- (void)setModel:(PlayGroundMainModel *)model
。。。。。。。
  • 2.在map页面
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation {
    
    if ([annotation isKindOfClass:[BMKPointAnnotation class]])
    {
        LyAnnotationView *customView = [LyAnnotationView annotationViewWithMapView:mapView viewForAnnotation:annotation];
        
        if (self.arrayPoint.count != 0)//添加了气泡
        {
            //它是一个一个添加的
            PlayGroundMainModel *model = self.arrayModel[self.arrayPoint.count - 1];
            
            customView.model = model;
            customView.iconTag = self.arrayPoint.count - 1;
            customView.delegate = self;
            [self.arrayImage addObject:customView.icon];
            
            if (self.arrayPoint.count == 1)
            {
                customView.isSelectIcon = YES;
                [customView setSelected:YES animated:YES];
            }
            else
            {
                customView.isSelectIcon = NO;
                [customView setSelected:NO animated:YES];
            }
        }
        return customView;
    }
    
    return nil;
}
  • 3.注意:设置大头针尺寸的时候,设置bounds,只需要设置大小,位置不要设置
 
posted @ 2017-04-17 20:09  ZJea  阅读(541)  评论(0编辑  收藏  举报