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

时间:2015-04-02 15:21:37

标签: ios annotations mapkit

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

我的标签栏用于过滤掉MapKit中的注释。

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

当我点击标签栏项目1时,它在MapKit中只显示一组注释,其他注释组将在MapKit中隐藏/删除但我未能完成这种工作。

我的代码:

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

1 个答案:

答案 0 :(得分:0)

当像这样调用removeAnnotations:时:

[mapView removeAnnotations:myCafe];

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

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

由于代码在每次选择时都会创建每个注释的新实例,因此在地图上找不到新实例,因此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语句也可以简化代码,并且在每种情况下,只创建选择所需的注释(您不需要每次都创建所有注释)。