关于 MapKit 中带有 NSMutableArray 注释的标签栏按钮

About the Tab bar button with NSMutableArray annotation in MapKit

我创建了 4 种类型的注释组。在地图UI中,我还在底部添加了一个标签栏作为我的按钮。

我的标签栏是用来过滤掉MapKit中的标注的。

例如...我有 4 组注释和 4 个标签栏项目。

当我点击标签栏项目 1 时,它在 MapKit 中只显示一组注释,其他组注释在 MapKit 中会 hide/remove 但我未能实现这种工作。

我的代码:

在MapViewController.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>

@interface MapViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate , UITabBarDelegate>{

    IBOutlet UITabBar *tabBar;

}

@property (weak, nonatomic) IBOutlet UIBarButtonItem *sidebarButton;

@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property (strong, nonatomic) CLLocationManager *locationManager;


@end

我的mapViewController.m:

#import "MapViewController.h"
#import "SWRevealViewController.h"
#import "Annotation.h"



@interface MapViewController ()<CLLocationManagerDelegate>


@end

//set desitination of map
#define PENANG_LATI 5.419501;
#define PENANG_LONG 100.323264;


//shop
#define SHOP_LATI 5.419501;
#define SHOP_LONG 100.323264;

//cafe
#define CAFE_LATI 5.419917;
#define CAFE_LONG 100.322969;

//food
#define FOOD_LATI 5.419746;
#define FOOD_LONG 100.322610;

//mural
#define MURAL_LATI 5.419786;
#define MURAL_LONG 100.322510;




#define THE_SPAN 0.005f;

@implementation MapViewController
@synthesize mapView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}



- (void)viewDidLoad
{

    [super viewDidLoad];

    //tabBar
    tabBar.delegate = self;

    MKCoordinateRegion myRegion;

    //Center
    CLLocationCoordinate2D center;
    center.latitude = PENANG_LATI;
    center.longitude = PENANG_LONG;

    //SPAN
    MKCoordinateSpan span;
    span.latitudeDelta = THE_SPAN;
    span.longitudeDelta = THE_SPAN;

    myRegion.center = center;
    myRegion.span = span;

    //set map
    [mapView setRegion:myRegion animated:YES];

    SWRevealViewController *revealViewController = self.revealViewController;
    if ( revealViewController )
    {
        [self.sidebarButton setTarget: self.revealViewController];
        [self.sidebarButton setAction: @selector( revealToggle: )];
        [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
    }


    /*
    //create coordinate
    CLLocationCoordinate2D penangLocation;
    penangLocation.latitude = PENANG_LATI;
    penangLocation.longitude = PENANG_LONG;

    Annotation * myAnnotation = [Annotation alloc];
    myAnnotation.coordinate = penangLocation;
    myAnnotation.title = @"THE ONE ACADEMY PENANG";
    myAnnotation.subtitle = @"HELLO!! I STUDY HERE";
    [self.mapView addAnnotation:myAnnotation];
     */






}


- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    //my group of annotation location
    NSMutableArray * myShop = [[NSMutableArray alloc] init];

    NSMutableArray * myCafe = [[NSMutableArray alloc] init];

    NSMutableArray * myFood = [[NSMutableArray alloc] init];

    NSMutableArray * myMural = [[NSMutableArray alloc] init];

    CLLocationCoordinate2D penangLocation;
    Annotation * myShopAnnotation;
    Annotation * myCafeAnnotation;
    Annotation * myFoodAnnotation;
    Annotation * myMuralAnnotation;


    //shop location

    myShopAnnotation = [[Annotation alloc] init];
    penangLocation.latitude = SHOP_LATI;
    penangLocation.longitude = SHOP_LONG;
    myShopAnnotation.coordinate = penangLocation;
    myShopAnnotation.title = @"Shop";
    myShopAnnotation.subtitle = @"I study here";
    [myShop addObject:myShopAnnotation];

    //cafe location

    myCafeAnnotation = [[Annotation alloc] init];
    penangLocation.latitude = CAFE_LATI;
    penangLocation.longitude = CAFE_LONG;
    myCafeAnnotation.coordinate = penangLocation;
    myCafeAnnotation.title = @"Cafe";
    myCafeAnnotation.subtitle = @"I paid here";
    [myCafe addObject:myCafeAnnotation];

    //food location

    myFoodAnnotation = [[Annotation alloc] init];
    penangLocation.latitude = FOOD_LATI;
    penangLocation.longitude = FOOD_LONG;
    myFoodAnnotation.coordinate = penangLocation;
    myFoodAnnotation.title = @"Food";
    myFoodAnnotation.subtitle = @"I walk here";
    [myFood addObject:myFoodAnnotation];


    //Mural location

    myMuralAnnotation = [[Annotation alloc] init];
    penangLocation.latitude = MURAL_LATI;
    penangLocation.longitude = MURAL_LONG;
    myMuralAnnotation.coordinate = penangLocation;
    myMuralAnnotation.title = @"Mural";
    myMuralAnnotation.subtitle = @"I walk here";
    [myMural addObject:myMuralAnnotation];

    if(item.tag == 1)
    {
        //show and hide annotation
        NSLog(@"shop");
        [mapView addAnnotations:myShop];
        [mapView removeAnnotations:myCafe];
        [mapView removeAnnotations:myFood];
        [mapView removeAnnotations:myMural];
    }
    if(item.tag == 2)
    {
        //show and hide annotation
        NSLog(@"cafe");
        [mapView removeAnnotations:myShop];
        [mapView addAnnotations:myCafe];
        [mapView removeAnnotations:myFood];
        [mapView removeAnnotations:myMural];


    }


    if(item.tag == 3)
    {
        //show and hide annotation
        NSLog(@"food");

        [mapView removeAnnotations:myShop];
        [mapView removeAnnotations:myCafe];
        [mapView removeAnnotations:myFood];
        [mapView addAnnotations:myMural];


    }

    if(item.tag == 4)
    {
        //show and hide annotation
        NSLog(@"mural");
        [mapView removeAnnotations:myShop];
        [mapView removeAnnotations:myCafe];
        [mapView removeAnnotations:myFood];
        [mapView addAnnotations:myMural];

    }
}





- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




@end

removeAnnotations:这样调用时:

[mapView removeAnnotations:myCafe];

地图视图在 myCafe 中查找已经在地图上的确切 objects(使用指针比较)。

此处的地图视图与使用坐标、标题等的注释不匹配

由于代码会在每次进行选择时为每个注释创建 新实例 ,因此在地图上找不到新实例,因此 removeAnnotations: 什么都不做。

相反,在您的情况下,您可以告诉地图视图删除 所有 现有注释,然后根据当前选择添加新注释。

而不是这个:

    [mapView addAnnotations:myShop];
    [mapView removeAnnotations:myCafe];
    [mapView removeAnnotations:myFood];
    [mapView removeAnnotations:myMural];

这样做:

    [mapView removeAnnotations:mapView.annotations];  //remove all
    [mapView addAnnotations:myShop];  //then add new


代码也可以通过使用 switch 语句来简化,并且在每种情况下,只创建选择所需的注释(您不需要每次都创建所有注释)。