gpt4 book ai didi

ios - 将 UIButton 添加到 mapkit 中的注释标注

转载 作者:行者123 更新时间:2023-11-29 12:49:03 25 4
gpt4 key购买 nike

我想在特定位置的注释标注中添加一个自定义图像按钮。例如,搜索星巴克会弹出星巴克位置的注释标记,当按下标记时,标注将显示一个按钮,然后将您定向到另一个带有星巴克信息的 View Controller 。现在注释在按下时显示位置的地址,我如何将其更改为在我选择的自定义位置显示按钮?我是 xcode 的新手,到目前为止,我似乎找不到太多关于我如何设计我的应用程序的有用信息。除了我不知道从哪里开始添加按钮之外,一切都按预期运行。

这是我的 View Controller

#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

最佳答案

您可以在 viewForAnnotation 方法中将按钮设置为 callout 附件 View :

 - (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委托(delegate)方法在用户点击标注 View 的控件时进行响应,在这种情况下,重定向到另一个 View Controller :

 -(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 来管理通过我的 View Controller 的导航。

关于ios - 将 UIButton 添加到 mapkit 中的注释标注,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22857237/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com