博客> 地图自定义大头针和气泡
地图自定义大头针和气泡
2019-01-18 17:47 评论:0 阅读:186 912612039

import "NearlyGoodsViewController.h"

import "GoodsListModel.h"

import "MapPopView.h"

import "MyAnnotation.h"

@interface NearlyGoodsViewController () { BMKLocationViewDisplayParam *param; }

@property (strong, nonatomic) NSMutableArray *dataArr;

@property (strong, nonatomic) MapPopView *popView;

@property (assign, nonatomic) CGFloat lat; @property (assign, nonatomic) CGFloat lng;

@end

@implementation NearlyGoodsViewController

@synthesize lat,lng,popView;

  • (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated];

    self.navigationItem.leftBarButtonItem = nil;

    [_mapView viewWillAppear]; _mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放 _locService.delegate = self; }

  • (void)viewDidLoad { [super viewDidLoad]; //适配ios7 if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=7.0)) { self.navigationController.navigationBar.translucent = NO; }

    _dataArr = [[NSMutableArray alloc] init];

    _mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - TabBar_Height)]; [self.view addSubview:_mapView];

    _locService = [[BMKLocationService alloc]init]; [self stratLocationAndFollowHeading];

    self.title = @"周边货源";

    //获取数据 [self FindGoodsListRequest];

}

-(void)viewWillDisappear:(BOOL)animated { [_mapView viewWillDisappear]; _mapView.delegate = nil; // 不用时,置nil _locService.delegate = nil; }

  • (void)stratLocationAndFollowHeading { NSLog(@"进入普通定位态"); NSLog(@"进入普通定位态"); [_locService startUserLocationService]; _mapView.showsUserLocation = NO;//先关闭显示的定位图层 //设置定位的状态 _mapView.userTrackingMode = BMKUserTrackingModeFollow; _mapView.showsUserLocation = YES; //显示定位图层 _mapView.isSelectedAnnotationViewFront = YES; [self customLocationAccuracyCircle]; }

//自定义精度圈

  • (void)customLocationAccuracyCircle { param = [[BMKLocationViewDisplayParam alloc] init]; [_mapView setZoomLevel:10]; param.isAccuracyCircleShow = NO; param.locationViewImgName = @"定位"; [_mapView updateLocationViewWithParam:param];

}

/* 用户方向更新后,会调用此函数 @param userLocation 新的用户位置 /

  • (void)didUpdateUserHeading:(BMKUserLocation *)userLocation { [_mapView updateLocationData:userLocation]; NSLog(@"heading is %@",userLocation.heading); // 停止定位 [_locService stopUserLocationService]; }

/* 用户位置更新后,会调用此函数 @param userLocation 新的用户位置 /

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

    // NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude); [_mapView updateLocationData:userLocation];

}

/* 在地图View停止定位后,会调用此函数 @param mapView 地图View /

  • (void)didStopLocatingUser { NSLog(@"stop locate"); }

/* 定位失败后,会调用此函数 @param mapView 地图View @param error 错误号,参考CLError.h中定义的错误号 */

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

  • (void)mapViewDidFinishLoading:(BMKMapView *)mapView { NSLog(@"dfgh"); }

  • (void)dealloc { if (_mapView) { _mapView = nil; } }

  • (void)addAnnotation {

    for (int i = 0; i < self xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed> %@",model.StartName,model.EndName]; annotation1.goodsNameStr = [NSString stringWithFormat:@"货物:%@ %@ 吨",model.Name,model.Value]; annotation1.noteStr = [NSString stringWithFormat:@"运输要求:"]; [_mapView addAnnotation:annotation1]; } }

pragma mark 当选中一个 标注(大头针)时 响应

  • (void)mapView:(BMKMapView )mapView didSelectAnnotationView:(BMKAnnotationView )view {

    /// 设置当前地图的中心点 把选中的标注作为地图中心点 [_mapView setCenterCoordinate:view.annotation.coordinate animated:YES] ; }

pragma mark -- BMKMapdelegate

/* 根据anntation生成对应的View @param mapView 地图View @param annotation 指定的标注 @return 生成的标注View / -(BMKAnnotationView )mapView:(BMKMapView )mapView viewForAnnotation:(id )annotation { if ([annotation isKindOfClass:[MyAnnotation class]]) { static NSString key1 = @"AnnotationKey1"; BMKPinAnnotationView newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:key1]; newAnnotationView.animatesDrop = NO; newAnnotationView.annotation = annotation; newAnnotationView.image = [UIImage imageNamed:@"货源定位"]; //这里我根据自己需要,继承了BMKPointAnnotation,添加了标注的类型等需要的信息 MyAnnotation tt = (MyAnnotation )annotation; //判断类别,需要添加不同类别,来赋予不同的标注图片

    popView = [[MapPopView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 100, 140)];
    self.goodsId = tt.goodsId;
    popView.lineNameLabel.text = tt.lineStr;
    popView.goodsNameLabel.text = tt.goodsNameStr;
    popView.notLabel.text = tt.noteStr;
    [popView.detailBtn addTarget:self action:@selector(detailBtn:) forControlEvents:UIControlEventTouchUpInside];
    BMKActionPaopaoView *pView = [[BMKActionPaopaoView alloc]initWithCustomView:popView];
    pView.frame = CGRectMake(0, 0, SCREEN_WIDTH - 100, 140);
    ((BMKPinAnnotationView*)newAnnotationView).paopaoView = nil;
    ((BMKPinAnnotationView*)newAnnotationView).paopaoView = pView;

    return newAnnotationView;
}
return nil;

}

收藏
0
sina weixin mail 回到顶部