[ios]关于选项卡栏按钮有 MapKit NSMutableArray 注释

标签: mapkit ios
发布时间: 2017/3/31 22:02:39
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我创建了 4 类型的批注组。在 UI 映射中,我添加了标签栏底部作为我的按钮。

我的选项卡栏用于筛选出 MapKit 中的批注。

例如...我有 4 个组的注释和 4 选项卡栏项。

当我单击选项卡栏项目 1 时,它只有 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:

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 语句和在每个情况下,仅创建所需的选择 (你不需要每次创建所有注释) 的注释。

赞助商