ios苹果地图添加自定义气泡
2023-05-23 本文已影响0人
xing_x
**苹果地图添加自定义气泡的方法和高德地图添加自定义气泡的方法一样。
**
自定义Annotation
@interface WSBaseAnnotation : NSObject<MKAnnotation>
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property (nonatomic, strong) UIImage *image;
@end
自定义自己所需的类
@interface WSStoreAnnotation : WSBaseAnnotation
@property (nonatomic,copy) NSString *storeName;
@property (nonatomic,copy) NSString *storeId;
@property (nonatomic,strong) WSStoreParamModel *store;
/*wgs84的anotation*/
- (id)initWith:(CLLocationCoordinate2D)coordiante storeId:(NSString *)storeId
storeName:(NSString *)storeName;
- (id)initWith:(CLLocationCoordinate2D)coordiante annotationStore:(WSStoreParamModel *)store;
@end
自定义气泡
#pragma mark-----UI old
- (id)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
if(self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) {
self.storeAnnotation = (WSStoreAnnotation *)annotation;
[self addSubview:self.callOutView];
}
return self;
}
- (WSStoreAnnotationCalloutView*)callOutView{
if (_callOutView==nil) {
_callOutView = [[WSStoreAnnotationCalloutView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
_callOutView.center = CGPointMake(CGRectGetWidth(self.bounds) / 2.f + self.calloutOffset.x,
-CGRectGetHeight(self.callOutView.bounds) / 2.f + self.calloutOffset.y);
_callOutView.userInteractionEnabled = YES;
}
return _callOutView;
}
-(void)setStoreAnnotation:(WSStoreAnnotation *)storeAnnotation{
_storeAnnotation = storeAnnotation;
UIImage * annotationImage;
[self setImage:nil];
annotationImage = [UIImage imageNamed:@"map_blue"];
[self setImage:annotationImage];
}
/*气泡上的信息展示*/
- (void)setCallOutViewStoreInfo:(WSStoreParamModel*)storeInfo{
[self setImage:nil];
NSString * imageName = [NSString getVisitStateImageName:storeInfo.visitStatusCode];
self.callOutView.iconImg.image = [UIImage imageNamed:imageName];
self.callOutView.title = @"";
self.callOutView.titleLabel.text = [NSString getVisitStateWithVisitStateCode:storeInfo.visitStatusCode];
WeakSelf(weakSelf);
[self.callOutView setShowMapStoreInfoViewBlock:^{
__object_block_return(weakSelf.showMapStoreInfoActionBlock, storeInfo);
}];
}
map上添加气泡方法
- 添加气泡
- 气泡显示方法
for (WSStoreAnnotation *storeAnnotation in self.mapView.annotations) {
if ([storeAnnotation isKindOfClass:[WSStoreAnnotation class]]) {
[self.mapView removeAnnotation:storeAnnotation];
}
}
[self.mapView removeOverlays:self.mapView.overlays];
//计算大图针
NSMutableArray *storeAnnotations = [NSMutableArray array];
for (WSStoreParamModel * store in storeAnnotationArray)
{
if(store.lon.length>0&&store.lat.length>0){
CLLocationCoordinate2D tmpStoreCoordinate = CLLocationCoordinate2DMake(store.lat.floatValue, store.lon.floatValue);
WSStoreAnnotation * tmpStoreAnnotation = [[WSStoreAnnotation alloc]initWith:tmpStoreCoordinate annotationStore:store];
[storeAnnotations addObject:tmpStoreAnnotation];
}
}
if ([storeAnnotations count] > 0) {
[self.mapView addAnnotations:storeAnnotations];
}
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
// If it's the user location, just return nil.
if ([annotation isKindOfClass:[WSStoreAnnotation class]]){
WSStoreAnnotation *storeAnnotation = (WSStoreAnnotation *)annotation;
WSStoreAnnotationView* annotationView = (WSStoreAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (!annotationView){
// If an existing pin view was not available, create one.
annotationView = [[WSStoreAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:identifier];
}else{
annotationView.annotation = storeAnnotation;
}
//annotationView.canShowCallout = YES;
annotationView.storeAnnotation = annotation;
if(self.canJumpCheckInStore){
[annotationView setCallOutViewStoreInfo:storeAnnotation.store];
WeakSelf(weakSelf);
[annotationView setShowMapStoreInfoActionBlock:^(WSStoreParamModel *storeModel) {
DDLog(@"显示底部商店信息Action");
if(weakSelf.jumpCheckInStore){
weakSelf.jumpCheckInStore(storeAnnotation.store);
}
}];
}
return annotationView;
}
return nil;
}
在开发过程中遇到一个很奇怪的问题,就是气泡漂移,会重新还原成系统气泡,找了很多博客,也没发现问题,想重写系统方法,也没找到系统类,后来发现问题出在代理,我们代码拿到地图的位置之后,解析地址之后会设置
map.delegate = nil
就是这个方法导致的气泡漂移,还原成系统气泡。去掉这段代码就ok了!