百度地圖自定義泡泡檢視
阿新 • • 發佈:2019-02-11
描述:最近專案中用到了百度地圖獲取附近的店鋪資訊,改店鋪資訊需要用自定義PaoPaoView來展示,其設定步驟如下:
#import <MapKit/MapKit.h>
@interface MapViewController : BaseViewController<BMKMapViewDelegate,BMKLocationServiceDelegate>
BMKMapView *_mapView;
BMKLocationService *_locService;
BMKPointAnnotation *_annotation;
CLLocationCoordinate2D
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 <BMKAnnotation>)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(@"取消選中大頭針"); }