重写此方法即可实现大头针图片的替换:
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
即代码:
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
newAnnotationView.pinColor = BMKPinAnnotationColorPurple;
newAnnotationView.animatesDrop = YES;// 设置该标注点动画显示
newAnnotationView.annotation=annotation;
newAnnotationView.image = [UIImage imageNamed:@"dfh"]; //把大头针换成别的图片
return newAnnotationView;
}
return nil;
}
另外,如果同一个地图中添加的大头针的图片不同,可通过下属方法解决:
例如现在是两种大头针:
可以声明两个大头针的成员变量:
BMKPointAnnotation* pointAnnotation;
BMKPointAnnotation* animatedAnnotation;
其中,想让animatedAnnotation添加原生的大头针图片,pointAnnotation给换成自己想要添加的大头针图片,在添加animatedAnnotation类的大头针时,按正常添加即可:
animatedAnnotation = [[BMKPointAnnotation alloc]init];
animatedAnnotation.coordinate = userLocation.location.coordinate;
animatedAnnotation.title = @"当前位置";
[_mapView addAnnotation:animatedAnnotation];
而对于想要改变图片的大头针:
-(void)addAnnotationWith:(NSDictionary *)dic {
NSLog(@"添加大头针");
//添加大头针
pointAnnotation = [[BMKPointAnnotation alloc]init];
pointAnnotation.coordinate = coorOfLocation;
CLLocationCoordinate2D coor;
coor.latitude = [[NSString stringWithFormat:@"%@",[dic objectForKey:@"latitude"]] floatValue];
coor.longitude = [[NSString stringWithFormat:@"%@",[dic objectForKey:@"longitude"]] floatValue];
pointAnnotation.coordinate = coor;
NSLog(@"%f======%f",pointAnnotation.coordinate.latitude,pointAnnotation.coordinate.longitude);
pointAnnotation.title = @"司机当前位置";
[_mapView setCenterCoordinate:coor animated:YES];
[_mapView addAnnotation:pointAnnotation];
}
//实现大头针图片的修改
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
if (annotation == pointAnnotation) {//加一个判断即可实现将pointAnnotation类的大头针的图片修改
NSString *AnnotationViewID = @"renameMark";
BMKPinAnnotationView *annotationView = (BMKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
if (annotationView == nil) {
annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
// 设置颜色
annotationView.pinColor = BMKPinAnnotationColorPurple;
annotationView.annotation=annotation;
annotationView.image = [UIImage imageNamed:@"place1"]; //把大头针换成别的图片
}
return annotationView;
}
return nil;
}