高德地图上,点击空白处,一定会清除当前selected annotation的选中状态。可以通过记录当前selected annotation,在点击空白处之后,一定会执行didDeselectAnnotationView回调,可在回调中对其状态进行恢复。
custom annoView.h
@property(nonatomic, strong) NSString *imageName;
@property(nonatomic, strong) NSString *selectedImageName;
custom annoView.m重写方法:
- (void)setSelected:(BOOL)selected {
if (selected == self.isSelected) {
return;
}
[super setSelected:selected];
if (selected) {
self.image = [UIImage imageNamed:self.selectedImageName];
}else {
self.image = [UIImage imageNamed:self.imageName];
}
}
viewcontroller.m
/* 起始点经纬度. */
@property (nonatomic) CLLocationCoordinate2D startCoordinate;
/* 终点经纬度. */
@property (nonatomic) CLLocationCoordinate2D destinationCoordinate;
/* 用于显示当前路线方案. */
@property (nonatomic) MANaviRoute * naviRoute;
@property (nonatomic, strong) MAPointAnnotation *startAnnotation;
@property (nonatomic, strong) SPPackageAnotation *destinationAnnotation;
@property (nonatomic, strong) SPPackageAnotation *destinationAnnotation_temp;
- (MAAnnotationView*)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation {
if ([annotation isKindOfClass:[XXXAnotation class]]) {
SSSAnnotationView *annoView = (SSSAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"PackageID"];
if (annoView == nil) {
annoView = [[SSSAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"PackageID"];
}
XXXAnotation *packAnno = (XXXAnotation *)annotation;
NSString *imageName = @"";
NSString *selectedImageName = @"";
if (....) {
imageName = @"icon_package_green_small";
selectedImageName = @"icon_package_green_large";
}else {
imageName = @"icon_package_red_small";
selectedImageName = @"icon_package_red_large";
}
annoView.imageName = imageName;
annoView.selectedImageName = selectedImageName;
...
return annoView;
}else if ([annotation isKindOfClass:[MAPointAnnotation class]]) {
...
}
return nil;
}
回调方法处理(包含路线规划)
- (void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view {
if ([view isKindOfClass:[SSSAnnotationView class]]) {
self.destinationCoordinate = view.annotation.coordinate;
self.destinationAnnotation = (XXXAnotation *)view.annotation;
if (self.startAnnotation != nil) {
[self clear];//清除路线,参考官方Demo
[self searchRoutePlanning];//路线规划,参考官方Demo
}
}
}
- (void)mapView:(MAMapView *)mapView didDeselectAnnotationView:(MAAnnotationView *)view {
if ([view isKindOfClass:[SSSAnnotationView class]]) {
if (self.startAnnotation != nil && self.destinationAnnotation != nil) {
self.destinationAnnotation_temp = self.destinationAnnotation;
self.destinationAnnotation = nil;
[self clear];//清除路线,
[self redrawRouteAfterDelay];//恢复已选中状态!
}
}
}
//恢复已选中状态!
- (void)redrawRouteAfterDelay {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if (self.destinationAnnotation == nil) {
self.destinationAnnotation = self.destinationAnnotation_temp;
self.destinationCoordinate = self.destinationAnnotation.coordinate;
//触发回调"- (void)mapView:(..)mapView didSelectAnnotationView:(..)view"
[_aMapView selectAnnotation:self.destinationAnnotation animated:NO];
}
});
}