//导入<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