//導入<BaiduMapAPI_Map/BMKMapComponent.h>庫 #import "AppDelegate.h" #import "MainViewController.h" #import <BaiduMapAPI_Map/BMKMapComponent.h> //遵循代理 @interface AppDelegate ()<BMKGeneralDelegate> @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; [self.window makeKeyAndVisible]; self.window.backgroundColor = [UIColor whiteColor]; MainViewController *mainVC = [[MainViewController alloc] init]; UINavigationController *mainNC = [[UINavigationController alloc] initWithRootViewController:mainVC]; self.window.rootViewController = mainNC; //初始化 BMKMapManager *mapManager = [[BMKMapManager alloc]init]; [mapManager start:@"填入申請的key" generalDelegate:self]; return YES; }
/* *在MainViewController.m裏建立地圖 */ @implementation MapViewController - (void)viewWillAppear:(BOOL)animated { [_mapView viewWillAppear]; _mapView.delegate = self; // 此處記得不用的時候須要置nil,不然影響內存的釋放 } -(void)viewWillDisappear:(BOOL)animated { [_mapView viewWillDisappear]; _mapView.delegate = nil; // 不用時,置nil } #pragma mark--------數據請求-------- - (void)dataHandle { //網絡獲取annotation信息 AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; manager.responseSerializer = [AFJSONResponseSerializer serializer]; manager.requestSerializer = [AFJSONRequestSerializer serializer]; [manager GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSArray *tempArr = [responseObject objectForKey:@"data"]; for (int i = 0; i < tempArr.count; i ++) { //model類 MapItem *mapItem = [[MapItem alloc] initWithDic:[tempArr objectAtIndex:i]]; [self.mapItemArr addObject:mapItem]; CLLocationCoordinate2D coor; coor.latitude = [mapItem.longitude floatValue]; coor.longitude = [mapItem.latitude floatValue]; //自定義MMAnnotation MMAnnotation *annotation = [[MMAnnotation alloc]initWithCoordinate2D:coor]; annotation.address = mapItem.position; annotation.title = mapItem.name; annotation.imageUrl = mapItem.pictureUrl; annotation.context = mapItem.synopsis; //添加annotation到地圖 [self.mapView addAnnotation:annotation]; } } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { }]; } - (void)viewDidLoad { [super viewDidLoad]; self.mapItemArr = [NSMutableArray array]; [self dataHandle]; //地圖設置 self.mapView = [[BMKMapView alloc]initWithFrame:CGRectMake1(0, 0, 320, 480)]; self.mapView.delegate = self; //縮放比例 self.mapView.zoomLevel = 15.0; //地圖中心點座標 self.mapView.centerCoordinate = CLLocationCoordinate2DMake(37.569872, 121.260381); self.view = self.mapView; //切換爲普通地圖 [self.mapView setMapType:BMKMapTypeStandard]; /* * 切換爲衛星圖 * [self.mapView setMapType:BMKMapTypeSatellite]; */ //實時路況圖層 YES開啓 NO關閉 [self.mapView setTrafficEnabled:NO]; } #pragma mark----------百度地圖大頭針設置-------------- /*重寫- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation代理方法,指定annotation爲自定義的類型 */ - (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(MMAnnotation *)annotation { int i = 0; if ([annotation isKindOfClass:[MMAnnotation class]] == YES) { BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"]; //自定義的彈出視圖 MMView *mmView = [[MMView alloc] initWithFrame:CGRectMake1(0, 0, 280, 150)]; //將annotation信息顯示在自定義彈出視圖上 mmView.titleLabel.text = annotation.title; mmView.contextLabel.text = annotation.context; [mmView.imageView sd_setImageWithURL:[NSURL URLWithString:annotation.imageUrl]]; mmView.addressLabel.text = annotation.address; //BaiduMap承載annotation詳情的氣泡形狀視圖 BMKActionPaopaoView *paoView = [[BMKActionPaopaoView alloc] initWithCustomView:mmView]; //不設置會出現重疊或卡頓 paoView.backgroundColor = [UIColor whiteColor]; paoView.frame = CGRectMake1(0, 0, 280, 150); newAnnotationView.paopaoView = nil; newAnnotationView.paopaoView = paoView; //默認annotation的顏色 newAnnotationView.pinColor = BMKPinAnnotationColorPurple; /* *自定義annotation顯示圖片 * newAnnotationView.image = [UIImage imageNamed:@"annotation.png"] */ newAnnotationView.animatesDrop = YES;// 設置該標註點動畫顯示 return newAnnotationView; } i ++; return nil; }
/* *在MMAnnotation.h裏 */ @interface MMAnnotation : NSObject<BMKAnnotation> @property (nonatomic, copy) NSString *title; @property (nonatomic, copy) NSString *imageUrl; @property (nonatomic, copy) NSString *context; @property (nonatomic, copy) NSString *address; @property (nonatomic, readonly) CLLocationCoordinate2D coordinate; - (id)initWithCoordinate2D:(CLLocationCoordinate2D)coordinate; @end /* *在MMAnnotation.m裏 */ @implementation MMAnnotation - (id)initWithCoordinate2D:(CLLocationCoordinate2D)coordinate { self = [super init]; if (self != nil) { _coordinate = coordinate; } return self; } @end
/* *在MMView裏定義須要的控件 */ @interface MMView : UIView @property (nonatomic, strong) UIImageView *imageView; @property (nonatomic, strong) UILabel *titleLabel; @property (nonatomic, strong) UILabel *contextLabel; @property (nonatomic, strong) UILabel *addressLabel; @end