MKMapView – >显示我所在位置的按钮

我有一个使用这些源代码行实现的MKMapView (我的位置是蓝色的子弹,另一个是紫色的针脚):

 - (MKAnnotationView *)mapView:(MKMapView *)mapViewLocal viewForAnnotation:(id )annotation { if (annotation == mapViewLocal.userLocation) { mapViewLocal.userLocation.title = @"Test"; [mapViewLocal setRegion:MKCoordinateRegionMakeWithDistance(mapViewLocal.userLocation.coordinate, 1000, 1000) animated:YES]; return nil; } MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapViewLocal dequeueReusableAnnotationViewWithIdentifier:@"Pin"]; if(pinView == nil) { pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Pin"] autorelease]; pinView.pinColor = MKPinAnnotationColorPurple; pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; pinView.animatesDrop = NO; pinView.canShowCallout = YES; } else { pinView.annotation = annotation; } return pinView; } 

紫色针脚有一个细节公开按钮,但我的注释没有。 我该如何设置这样一个按钮?

这就是我可以按下按钮的方法:

 -(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 

我怎么能区分我的位置和所有其他位置,因为我需要一个不同的处理。 是否有其他代表或我是否必须制作某种if条款?

在你的didUpdateToLocation写下类似的内容

 AddressAnnotation *myAnnotation = [[AddressAnnotation alloc] initWithCoordinate:currentLocation]; myAnnotation.title = @"You are here"; [self.mapView addAnnotation:myAnnotation]; 

接着

就像是

 - (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id )annotation { // try to dequeue an existing pin view first static NSString* annotationIdentifier = @"annotationIdentifier"; NSString *titlestr = annotation.title; MKPinAnnotationView* pinView = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier]; MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil] autorelease]; if (!pinView) { // if an existing pin view was not available, create one // MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc] //initWithAnnotation:annotation reuseIdentifier:annotationIdentifier] autorelease]; if([titlestr isEqualToString:@"You are here"]) { customPinView.pinColor = MKPinAnnotationColorGreen; NSLog(@"customPinView.pinColor = MKPinAnnotationColorGreen;"); } else{ customPinView.pinColor = MKPinAnnotationColorPurple; customPinView.selected = TRUE; NSLog(@"customPinView.pinColor = MKPinAnnotationColorPurple;"); UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; [rightButton addTarget:self action:@selector(ShowStoreDetail:) forControlEvents:UIControlEventTouchUpInside]; customPinView.rightCalloutAccessoryView = rightButton; } customPinView.animatesDrop = YES; customPinView.canShowCallout = YES; return customPinView; } else { pinView.annotation = annotation; if([titlestr isEqualToString:@"You are here"]) { customPinView.pinColor = MKPinAnnotationColorPurple; NSLog(@"customPinView.pinColor = MKPinAnnotationColorGreen;"); } else{ customPinView.pinColor = MKPinAnnotationColorPurple; customPinView.selected = TRUE; NSLog(@"customPinView.pinColor = MKPinAnnotationColorPurple;"); UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; [rightButton addTarget:self action:@selector(ShowStoreDetail:) forControlEvents:UIControlEventTouchUpInside]; customPinView.rightCalloutAccessoryView = rightButton; } } return pinView; return nil; }