如何将地图注释视图按钮与数据库连接到另一个视图?

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id )annotation { //if it's user location, return nil if ([annotation isKindOfClass:[MKUserLocation class]]) return nil; //try to dequeue an existing pin view first static NSString* AnnotationIdentifier = @"AnnotationIdentifier"; MKPinAnnotationView* pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier]; pinView.animatesDrop = YES; pinView.canShowCallout = YES; pinView.pinColor = MKPinAnnotationColorRed; //button on the right for popup for pins UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; [rightButton setTitle:annotation.title forState:UIControlStateNormal]; [rightButton addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside]; pinView.rightCalloutAccessoryView = rightButton; //zoom button on the left of popup for pins UIButton* leftButton = [UIButton buttonWithType:UIButtonTypeContactAdd]; [leftButton setTitle:annotation.title forState:UIControlStateNormal]; [leftButton addTarget:self action:@selector(zoomToLocation:) forControlEvents:UIControlEventTouchUpInside]; pinView.leftCalloutAccessoryView = leftButton; return pinView; } //for map view annotation right button -(void)showDetails:(id)sender{ NSLog(@"Annotation Click"); //fypAppDelegate *appDelegate = (fypAppDelegate *)[[UIApplication sharedApplication] delegate]; //Attraction *attraction = (Attraction *)[appDelegate.attractions objectAtIndex:sender]; infoViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"info"]; self.infoView = viewController; [self.navigationController pushViewController:infoView animated:true]; } //for map view annotation left button -(void)zoomToLocation:(id)sender{ NSLog(@"Annotation Click"); } 

以上是地图注释的代表。 我能够显示引脚并显示地图注释视图,但我不知道如何将按钮事件链接到下一个视图(infoViewController)。 所以你们可以看到,右边的按钮是我想用来让用户查看有关该位置的更多信息的按钮,而左按钮,我想让用户放大到该针的坐标。

数据来自我创建的数据库。 以下是我如何做它只是为了参考(如果你们可能需要它)

 -(void)putPins { fypAppDelegate *appDelegate = (fypAppDelegate *)[[UIApplication sharedApplication] delegate]; //get data [appDelegate readTopAttractions]; int i = 0; int count = appDelegate.attractions.count; self.mapAnnotations = [[NSMutableArray alloc] initWithCapacity:appDelegate.attractions.count]; while (i < count) { Attraction *attraction = (Attraction *)[appDelegate.attractions objectAtIndex:i]; i++; //Set coordinates for pin CLLocationCoordinate2D location; location.latitude = (double)[[attraction xCoor] doubleValue]; location.longitude = (double)[[attraction yCoor] doubleValue]; MapPin *mapPin = [[MapPin alloc] init]; [mapPin setCoordinate:location]; [mapPin setName: [attraction name]]; NSString *desc = [attraction description]; int i = 0, position; while(i < 50){ if ([desc characterAtIndex:i] == ' '){ position = i; i++; } else i++; } desc = [@"" stringByAppendingFormat:@"%@%@", [desc substringToIndex:position], @"..."]; [mapPin setDescription: desc]; [self.mapAnnotations addObject:mapPin]; } [self.mapView addAnnotations:self.mapAnnotations]; } 

如果你们需要更多细节,请告诉我。 谢谢! =)

showDetails:zoomToLocation:方法中,您可以通过执行以下操作来获取对其标注按钮被点击的注释的引用:

 MapPin *ann = (MapPin *)[mapView.selectedAnnotations objectAtIndex:0]; 

zoomToLocation:您可以使用以下方法放大该注释:

 [mapView setRegion: MKCoordinateRegionMakeWithDistance(ann.coordinate, 500, 500) //500 meters vertical span, 500 meters horizontal span animated:YES]; 

showDetails: ,您可以将ann或其属性传递给详细视图。

顺便说一句,您可以使用map视图的calloutAccessoryControlTapped委托方法,而不是使用viewForAnnotationcalloutAccessoryControlTapped调用自定义方法,该方法可以更直接地访问被点击的注释。 例如:

 -(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control { MapPin *ann = (MapPin *)view.annotation; if (control == view.rightCalloutAccessoryView) { NSLog(@"calloutAccessoryControlTapped: control=RIGHT"); //show detail view (or you can call your custom method here)... } else if (control == view.leftCalloutAccessoryView) { NSLog(@"calloutAccessoryControlTapped: control=LEFT"); //zoom in (or you can call your custom method here)... } else { NSLog(@"calloutAccessoryControlTapped: unknown control"); } } 

如果您决定使用calloutAccessoryControlTapped委托方法,请确保从viewForAnnotation删除addTarget调用。

你想放大特定的针? 是对的吗?

因此,您可以使用MKMapView中的setRegion:animated: – 方法。

例:

 mapView = MKMapView location = CLLLocationCoordinate2D METERS_PER_MILE = 1609.344 (defined as a Constant) MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(location, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE); MKCoordinateRegion adjustedRegion = [mapView regionThatFits:region]; [mapView setRegion:adjustedRegion animated:YES]; 

AppleDocs
http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html

http://developer.apple.com/library/IOs/#documentation/MapKit/Reference/MapKitDataTypesReference/Reference/reference.html