【问题标题】:add UIButton to annotation callout in mapkit将 UIButton 添加到 mapkit 中的注释标注
【发布时间】:2014-04-04 08:17:40
【问题描述】:

我想在特定位置的注释标注中添加一个自定义图像按钮。例如,搜索星巴克会显示星巴克位置的注释标记,当按下标记时,标注将显示一个按钮,然后将引导您到另一个带有星巴克信息的视图控制器。现在注释会在按下时显示该位置的地址,我如何将其更改为在我选择的自定义位置显示一个按钮?我对 xcode 很陌生,相对于我迄今为止如何设计我的应用程序,似乎找不到太多有用的信息。除了我不知道从哪里开始添加按钮之外,一切都按预期运行。

这是我的视图控制器

#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

【问题讨论】:

    标签: ios objective-c uibutton annotations mapkit


    【解决方案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 来管理通过我的视图控制器的导航。

    【讨论】:

    • 感谢您的回复,我只是在理解第一个代码的位置时遇到了一些麻烦。我的 viewForAnnotation 方法在哪里?好像没找到。
    • 使用mapView:viewForAnnotation 方法代码编辑了我的答案。您应该在 ViewController.m 内找到它...
    • 我很难让这段代码正常运行。我将这两种方法都放入了我的 ViewController.m 文件中,但我收到错误消息说 detalview 是一个未声明的标识符。我对 xcode 完全陌生,对于我缺乏知识,我深表歉意。
    • 无需道歉。你有一个名为 DetailViewController 的类吗?你在课堂上正确导入了吗?
    • 不,我没有一个名为详细视图控制器的类。该按钮应该将我引导到 InfoController,它是我故事板中的下一个 viewController。 InfoController 将填充特定于在调用中使用按钮选择的位置的信息。
    【解决方案2】:

    关于如何自定义标注视图的 SO 上有大量资源,例如 herehere。您还可以查看 Apple 的 MKAnnotationView 文档,尤其是子类化注释及其 MapCallouts 示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-02
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多