最近在地圖開發中,有些小伙伴遇到了一個問題就是在清除地圖上所有的大頭針的時候,發現當前位置也被清除了。這是我們不想要的,我們只想清除我們自己添加的大頭針,當前位置并不需要清除掉。
一、先了解MAAnnotationView、MAAnnotation、MAUserLocation
MAAnnotationView是一個繼承UIView的view,它是一個視圖,視圖上有圖片等元素,這就是我們平時所說的大頭針的本質,它其實就是一個view,放在了地圖上而已。
MAAnnotationView類里有一個annotation屬性,是標注點的protocol
///關聯的annotation
@property (nonatomic, strong) id <MAAnnotation> annotation;
這里像不像我們平時寫的協議呢?實際上就是一個協議
///該類為標注點的protocol,提供了標注類的基本信息函數
@protocol MAAnnotation <NSObject>
///標注view中心坐標
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@optional
///annotation標題
@property (nonatomic, copy) NSString *title;
///annotation副標題
@property (nonatomic, copy) NSString *subtitle;
/**
* @brief 設置標注的坐標,在拖拽時會被調用.
* @param newCoordinate 新的坐標值
*/
- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate;
@end
- 我們要生成大頭針的時候需要實現地圖的這個代理方法,在此代理方法中設置不同的大頭針
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation {
//用戶當前位置大頭針
if ([annotation isKindOfClass:[MAUserLocation class]])
{
static NSString *userLocationStyleReuseIndetifier = @"userLocationStyleReuseIndetifier";
MAAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:userLocationStyleReuseIndetifier];
if (annotationView == nil)
{
annotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:userLocationStyleReuseIndetifier];
}
annotationView.canShowCallout = NO;
annotationView.image = [UIImage imageNamed:@"heardImg_passenger_default"];
annotationView.frame = CGRectMake(0, 0, 26, 26);
annotationView.contentMode = UIViewContentModeScaleToFill;
annotationView.layer.masksToBounds = YES;
return annotationView;
}
//其他大頭針
else if ([annotation isKindOfClass:[MAPointAnnotation class]]) {
static NSString *stopCarReuseIndetifier = @"stopCarReuseIndetifier";
MAAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:stopCarReuseIndetifier];
if (annotationView == nil)
{
annotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:stopCarReuseIndetifier];
}
UIImage *image = [UIImage imageNamed:@"centerAnnotation"];
annotationView.image = image;
annotationView.frame = CGRectMake(0, 0, image.size.width, image.size.height);
annotationView.contentMode = UIViewContentModeScaleToFill;
annotationView.layer.masksToBounds = YES;
annotationView.centerOffset = CGPointMake(0, -0.5*image.size.height);
return annotationView;
}
return nil;
}
用戶的當前位置,也叫定位小藍點,定位精度圈等,實際上也是一個“大頭針“,和我們添加在地圖上的大頭針一樣,是一個MAAnnotationView,view上有一個關聯的annotation。
當前位置的大頭針是MAUserLocation類型,所以用[annotation isKindOfClass:[MAUserLocation class]]判斷,其他的大頭針是MAPointAnnotation類型。
二、怎么移除不會移除當前位置
NSMutableArray *removeAnnotations = [[NSMutableArray alloc]init];
//將所有需要移除打大頭針添加一個數組,去掉當前位置的大頭針
[removeAnnotations addObjectsFromArray:self.mapView.annotations];
[removeAnnotations removeObject:self.mapView.userLocation];
//移除需要移除的大頭針
[self.mapView removeAnnotations:removeAnnotations];
思路也很簡單,先創建一個可變數組接收需要移除的大頭針對象,然后從該數組中將當前位置的大頭針去掉,最后移除需要移除的數據就OK了。