我想在特定位置的注释标注中添加自定义图像按钮。例如,搜索星巴克会为星巴克位置显示一个注释标记,当按下标记时,标注将显示一个按钮,然后将指向另一个具有星巴克信息的视图控制器。现在,注释会在按下时显示位置的地址,如何将其更改为在我选择的自定义位置显示按钮?我是xcode的新手,到目前为止我似乎无法找到有关我如何设计应用程序的有用信息。除了我不知道从哪里开始添加按钮这一事实外,一切都按照需要运行。
这是我的ViewControllers
#import "ViewController.h"
@interface ViewController () <UISearchDisplayDelegate, UISearchBarDelegate>
@end
@implementation ViewController {
MKLocalSearch *localSearch;
MKLocalSearchResponse *results;
}
#pragma mark - View Lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
[self.searchDisplayController setDelegate:self];
[self.ibSearchBar setDelegate:self];
// Zoom the map to current location.
[self.ibMapView setShowsUserLocation:YES];
[self.ibMapView setUserInteractionEnabled:YES];
[self.ibMapView setUserTrackingMode:MKUserTrackingModeFollow];
}
#pragma mark - Search Methods
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
// Cancel any previous searches.
[localSearch cancel];
// Perform a new search.
MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
request.naturalLanguageQuery = searchBar.text;
request.region = self.ibMapView.region;
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
localSearch = [[MKLocalSearch alloc] initWithRequest:request];
[localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error){
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
if (error != nil) {
[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Map Error",nil)
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:NSLocalizedString(@"OK",nil) otherButtonTitles:nil] show];
return;
}
if ([response.mapItems count] == 0) {
[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"No Results",nil)
message:nil
delegate:nil
cancelButtonTitle:NSLocalizedString(@"OK",nil) otherButtonTitles:nil] show];
return;
}
results = response;
[self.searchDisplayController.searchResultsTableView reloadData];
}];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [results.mapItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *IDENTIFIER = @"SearchResultsCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:IDENTIFIER];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:IDENTIFIER];
}
MKMapItem *item = results.mapItems[indexPath.row];
cell.textLabel.text = item.name;
cell.detailTextLabel.text = item.placemark.addressDictionary[@"Street"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.searchDisplayController setActive:NO animated:YES];
MKMapItem *item = results.mapItems[indexPath.row];
[self.ibMapView addAnnotation:item.placemark];
[self.ibMapView selectAnnotation:item.placemark animated:NO];
[self.ibMapView setCenterCoordinate:item.placemark.location.coordinate animated:YES];
[self.ibMapView setUserTrackingMode:MKUserTrackingModeNone];
}
@end
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UISearchBar *ibSearchBar;
@property (strong, nonatomic) IBOutlet MKMapView *ibMapView;
@end
答案 0 :(得分:1)
您可以在viewForAnnotation
方法中将按钮设置为标注附件视图:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
static NSString *AnnotationIdentifier = @"Annotation";
if ([annotation isKindOfClass:MKUserLocation.class]) {
return nil;
}
MKPinAnnotationView* pinAnnotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if (!pinAnnotationView)
{
pinAnnotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] ;
pinAnnotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pinAnnotationView.canShowCallout = YES;
}
return pinAnnotationView;
}
然后您可以使用mapView:annotationView:calloutAccessoryControlTapped
委托方法在用户点击标注视图的控件时进行响应,在这种情况下,重定向到另一个视图控制器:
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
InfoController *infoController = [[InfoController alloc] initWithNibName:@"InfoController" bundle:[NSBundle mainBundle]];
/*
here you can pass the necessary information to your InfoController
*/
[self.navigationController pushViewController:infoController animated:YES];
[infoController release];
}
在此示例中,我使用UINavigationController
来管理通过我的视图控制器的导航。
答案 1 :(得分:0)
关于如何自定义标注视图,SO上有很多资源,例如here和here。您还可以查看Apple MKAnnotationView的文档,尤其是子类注释及其MapCallouts示例。