描述:最近项目中用到了百度地图获取附近的店铺信息,改店铺信息需要用自定义PaoPaoView来展示,其设置步骤如下:
#import
@interface MapViewController : BaseViewController<BMKMapViewDelegate,BMKLocationServiceDelegate>
BMKMapView *_mapView;
BMKLocationService *_locService;
BMKPointAnnotation *_annotation;
CLLocationCoordinate2D coordinate; //设定经纬度
BMKPinAnnotationView *newAnnotation;
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
self.navigationController.navigationBarHidden = NO;
_mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放
_locService.delegate = self;
}
#pragma mark -- UI
- (void)createBaiDuMapView
{
_mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
[self.view addSubview:_mapView];
_mapView.delegate=self;
//用户位置追踪(用户位置追踪用于标记用户当前位置,此时会调用定位服务)
_mapView.userTrackingMode=MKUserTrackingModeFollow;
//设置地图类型
_mapView.mapType=MKMapTypeStandard;
_mapView.showsUserLocation = YES; //设置为可以显示用户位置
//设定经纬度
SharedInfo *shared = [SharedInfo sharedDataInfo];
float latitude = [shared.localLatitude floatValue];
float longitude = [shared.localLongitude floatValue];
coordinate.latitude = latitude;//纬度
coordinate.longitude = longitude;//经度
_locService = [[BMKLocationService alloc]init];
_locService.delegate=self;
[_locService startUserLocationService];
_mapView.userTrackingMode=BMKUserTrackingModeNone;//地图模式
[_mapView setShowsUserLocation:YES];//显示定位的蓝点儿
//设置定位精确度,默认:kCLLocationAccuracyBest
[BMKLocationService setLocationDesiredAccuracy:kCLLocationAccuracyBest];
//指定最小距离更新(米),默认:kCLDistanceFilterNone
[BMKLocationService setLocationDistanceFilter:300.0f];
BMKCoordinateRegion viewRegion = BMKCoordinateRegionMake(coordinate, BMKCoordinateSpanMake(0.3,0.3));
BMKCoordinateRegion adjustedRegion = [_mapView regionThatFits:viewRegion];
[_mapView setRegion:adjustedRegion animated:YES];
}
#pragma mark -- HTTP
- (void)shopsList:(NSString *)lng setLat:(NSString *)lat
{
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:lng,@"lng",lat,@"lat",@"",@"cate_id",@"",@"r_id",@"5000",@"fujin",@"",@"keyword",@"1",@"page",@"20",@"per_page",@"",@"order",@"",@"sort", nil];
//[self initMBProgress:@"数据加载中..."];
[CKHttpCommunicate createRequest:HTTP_METHOD_NEARBY_STORE_LIST WithParam:params withMethod:@"POST" success:^(id result) {
if (result && [[result objectForKey:@"error"]intValue] == 0 ) {
NSDictionary *jsonArray = [result objectForKey:@"data"];
self.cityData = [jsonArray objectForKey:@"stores"];
for (int i = 0; i < self.cityData.count; i ++) {
NSDictionary *dic = [self.cityData objectAtIndex:i];
NSString *latStr = [dic objectForKey:@"lat"];
NSString *lngStr = [dic objectForKey:@"lng"];
CLLocationCoordinate2D coor;
coor.longitude = [lngStr floatValue];
coor.latitude = [latStr floatValue];
// 在地图中添加一个PointAnnotation,用于添加大头针
_annotation = [[BMKPointAnnotation alloc]init];
_annotation.coordinate = coor;
//_annotation.title = @"标题";
//_annotation.subtitle = @"详情介绍";
[_mapView addAnnotation:_annotation]; //添加大头针对象
}
[_mapView setZoomLevel:15.3f];
}
} failure:^(NSError *erro) {
}];
}
#pragma mark -- BaiDuMapDelegate
- (void)mapViewDidFinishLoading:(BMKMapView *)mapView
{
//NSLog(@"开始定位");
}
//大头针
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id )annotation
{
NSString *AnnotationViewID = [NSString stringWithFormat:@"renameMark%d",identity];
newAnnotation = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
NSDictionary *dic = [self.cityData objectAtIndex:identity];
NSString *store_name = [dic objectForKey:@"store_name"];
NSString *store_logo = [dic objectForKey:@"store_logo"];
NSString *address = [dic objectForKey:@"address"];
NSString *jianju = [dic objectForKey:@"jianju"];
// 设置颜色
((BMKPinAnnotationView*)newAnnotation).pinColor = BMKPinAnnotationColorRed;
// 从天上掉下效果
((BMKPinAnnotationView*)newAnnotation).animatesDrop = YES;
// 设置可拖拽
((BMKPinAnnotationView*)newAnnotation).draggable = YES;
//设置大头针图标
//((BMKPinAnnotationView*)newAnnotation).image = [UIImage imageNamed:@"default_avatar"];
popView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 260, 124)];
//设置弹出气泡图片
UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"map_search_store"]];
image.frame = CGRectMake(0, 0, 260, 124);
[popView addSubview:image];
//泡泡上得图片
UIImageView *storeImage = [[UIImageView alloc]initWithFrame:CGRectMake(20, 20, 45, 45)];
[storeImage sd_setImageWithURL:[NSURL URLWithString:store_logo]placeholderImage:[UIImage imageNamed:@"default_avatar"]];
[popView addSubview:storeImage];
storeName = [[UILabel alloc]initWithFrame:CGRectMake(71.5, 18, 160, 20)];
storeName.text = store_name;
storeName.backgroundColor = [UIColor clearColor];
storeName.font = [UIFont systemFontOfSize:15];
storeName.textColor = [UIColor redColor];
[popView addSubview:storeName];
//距离
spacingLabel = [[UILabel alloc]initWithFrame:CGRectMake(85, 38, 100, 20)];
spacingLabel.text = [jianju stringByReplacingOccurrencesOfString:@"千米" withString:@"km"];
spacingLabel.backgroundColor = [UIColor clearColor];
spacingLabel.font = [UIFont systemFontOfSize:12];
spacingLabel.textColor = [UIColor grayColor];
[popView addSubview:spacingLabel];
//地址
addLabel = [[UILabel alloc]initWithFrame:CGRectMake(126, 39, 125, 20)];
addLabel.text = address;
addLabel.backgroundColor = [UIColor clearColor];
addLabel.font = [UIFont systemFontOfSize:12];
addLabel.textColor = [UIColor grayColor];
[popView addSubview:addLabel];
lookBtn = [UIButton buttonWithType:UIButtonTypeCustom];
lookBtn.frame = CGRectMake(30, 77, 84, 30);
[lookBtn setImage:[UIImage imageNamed:@"go_look_btn"] forState:UIControlStateNormal];
[lookBtn addTarget:self action:@selector(lookAction:) forControlEvents:UIControlEventTouchUpInside];
lookBtn.tag = identity;
[popView addSubview:lookBtn];
goBtn = [UIButton buttonWithType:UIButtonTypeCustom];
goBtn.frame = CGRectMake(60+84, 77, 84, 30);
[goBtn setImage:[UIImage imageNamed:@"go_here_btn"] forState:UIControlStateNormal];
[goBtn addTarget:self action:@selector(goAction) forControlEvents:UIControlEventTouchUpInside];
[popView addSubview:goBtn];
BMKActionPaopaoView *pView = [[BMKActionPaopaoView alloc]initWithCustomView:popView];
pView.frame = CGRectMake(0, 0, 260, 124);
((BMKPinAnnotationView*)newAnnotation).paopaoView = nil;
((BMKPinAnnotationView*)newAnnotation).paopaoView = pView;
identity ++;
return newAnnotation;
}
//选中大头针的时候,返还一个View
- (void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)view
{
//NSLog(@"选中大头针");
}
//取消选中大头针的时候
- (void)mapView:(BMKMapView *)mapView didDeselectAnnotationView:(BMKAnnotationView *)view
{
//NSLog(@"取消选中大头针");
}