将某些地图注释保存为收藏夹,然后显示它们

时间:2015-01-05 18:48:04

标签: ios mkmapview mkannotation

我已经挣扎了好几天而且无处可去。

我有一个带有大约60个注释的mapView。当用户触摸注释时,注释细节将与右侧标注气泡一起显示。如果用户触摸了标注气泡,则会出现UIAlert,询问用户是否要将注释保存为"收藏夹" (所有代码都适用于这一点)。

我接下来要做的是将此注释保存为应用程序中的最爱。然后我希望用户能够清除所有注释的mapView并仅显示已保存的"收藏夹"按下按钮。

我完全不知道如何继续,并且正在寻求一些帮助,我需要做些什么来完成这两项工作。我正在使用Xcode v6.1.1。

一些示例代码:

//** Set map starting region and display two of 60 annotations **
    MKCoordinateRegion regionStart = { {0.0, 0.0 }, { 0.0, 0.0 } };
    regionStart.center.latitude = 45.526197;
    regionStart.center.longitude = -122.675212;
    regionStart.span.longitudeDelta = 0.1f;
    regionStart.span.latitudeDelta = 0.1f;
    [mapView setRegion:regionStart animated:YES];

    [mapView setDelegate:self];

    //** Data for Acme Widgets **
    MKCoordinateRegion region1 = { {0.0, 0.0 }, { 0.0, 0.0 } };
    region1.center.latitude = 45.5262223;
    region1.center.longitude = -122.63642379999999;
    [mapView setRegion:region1 animated:YES];

    DisplayMap *ann1 = [[DisplayMap alloc] init];

    ann1.title = @"Acme Widgets";
    ann1.subtitle = @"1234 Main Street";
    ann1.coordinate = region1.center;
    [self.mapView addAnnotation:ann1];

    //** Data for Mike's Garage **
    MKCoordinateRegion region2 = { {0.0, 0.0 }, { 0.0, 0.0 } };
    region2.center.latitude = 45.5337167;
    region2.center.longitude = -122.6917036;
    [mapView setRegion:region2 animated:YES];

    DisplayMap *ann2 = [[DisplayMap alloc] init];
    ann2.title = @"Mike's Garage";
    ann2.subtitle = @"234 East Center Street";
    ann2.coordinate = region2.center;
    [mapView addAnnotation:ann2];


-(void)saveAsFavorite{
    //** Displays UIAlertView when callout bubble on annotation is touched **
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Save As Favorite?"
                                                   message: @""
                                                  delegate: self
                                         cancelButtonTitle:@"Cancel"
                                         otherButtonTitles:@"Yes",nil];

    [alert setTag:1];
    [alert show];

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    //** Test for which UIAlertView button is touched **
    if (buttonIndex==0) {
        NSLog(@"The Cancel button was touched");
    }
    else{
        //** I want to be able to somehow note this annotation as a "favorite" **
        NSLog(@"The YES button was touched");
        }
}

-(void)clearAllAnnotations{
   //** Clear all map annotations **
   [self.mapView removeAnnotations:[self.mapView annotations]];
}

-(IBAction)showFavorites:(UIButton *)sender{
//** Need to add code for displaying only "favorite" annotations here **
}

0 个答案:

没有答案