如果在你的app中需要用到百度地图,下面是我的一些经验。
网上的那些介绍类似百度地图使用的,多而杂,按照其操作,往往看似要成功,就差那么点,不如直接进百度开放平台里的开发指南:开发指南
1.下载SDK
2.注册百度地图应用,获得(访问应用(AK))
3.配置开发环境(按照开发文档一步一步操作是最好的)
完成上面繁琐的配置开发环境,接下来仿照开发指南里的demo,和里面的(类参考),基本上就能完成百度地图的大部分功能了。
自定义大头针
//自定义百度地图的大头针
-(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id)annotation{
BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
newAnnotationView.pinColor = BMKPinAnnotationColorPurple;
newAnnotationView.animatesDrop = YES;//设置该标点动画显示
newAnnotationView.annotation = annotation;
// newAnnotationView.image = [UIImage imageNamed:@"1"];
// 添加手势双击时跳转
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
//设置点按的次数
tap.numberOfTapsRequired = 2;
// //设置点按的手指的数
// tap.numberOfTouchesRequired = 2;
// 2.为控件添加手势
[newAnnotationView addGestureRecognizer:tap];
return newAnnotationView;
}
// 3.实现手势执行的方法
- (void)tap:(UITapGestureRecognizer*)sender
{
}
注意:
代理和添加大头针要放在viewWillAppear里面,否则可能出现自定义无效。
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.mapView.delegate = self;
[self addAnnotation];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
self.mapView.delegate = nil;
}
- (void)addAnnotation {
// 大头针1
CLLocationCoordinate2D location1=CLLocationCoordinate2DMake(30.10, 108.35);
BMKPointAnnotation *annotation1=[[BMKPointAnnotation alloc]init];annotation1.title=@"利川";
annotation1.subtitle=@"****";
annotation1.coordinate=location1;
// annotation1.image=[UIImage imageNamed:@"1"];
[self.mapView addAnnotation:annotation1];
// 大头针2
CLLocationCoordinate2D location2=CLLocationCoordinate2DMake(41.17, 114.72);
BMKPointAnnotation *annotation2=[[BMKPointAnnotation alloc]init];
annotation2.title=@"张北";
annotation2.subtitle=@"*****";
annotation2.coordinate=location2;
// annotation2.image=[UIImage imageNamed:@"1"];
[self.mapView addAnnotation:annotation2];
// 大头针3
CLLocationCoordinate2D location3=CLLocationCoordinate2DMake(47.55, 106.53);
BMKPointAnnotation *annotation3=[[BMKPointAnnotation alloc]init];
annotation3.title=@"Salkhif";
annotation3.subtitle=@"*****";
annotation3.coordinate=location3;
// annotation2.image=[UIImage imageNamed:@"1"];
[self.mapView addAnnotation:annotation3];
// 大头针4
CLLocationCoordinate2D location4=CLLocationCoordinate2DMake(31.56, 114.09);
BMKPointAnnotation *annotation4=[[BMKPointAnnotation alloc]init];
annotation4.title=@"大悟";
annotation4.subtitle=@"****";
annotation4.coordinate=location4;
// annotation2.image=[UIImage imageNamed:@"1"];
[self.mapView addAnnotation:annotation4];
}