**蘋(píng)果地圖添加自定義氣泡的方法和高德地圖添加自定義氣泡的方法一樣。
**
自定義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
自定義自己所需的類(lèi)
@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];
//計(jì)算大圖針
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;
}
在開(kāi)發(fā)過(guò)程中遇到一個(gè)很奇怪的問(wèn)題,就是氣泡漂移,會(huì)重新還原成系統(tǒng)氣泡,找了很多博客,也沒(méi)發(fā)現(xiàn)問(wèn)題,想重寫(xiě)系統(tǒng)方法,也沒(méi)找到系統(tǒng)類(lèi),后來(lái)發(fā)現(xiàn)問(wèn)題出在代理,我們代碼拿到地圖的位置之后,解析地址之后會(huì)設(shè)置
map.delegate = nil
就是這個(gè)方法導(dǎo)致的氣泡漂移,還原成系統(tǒng)氣泡。去掉這段代碼就ok了!