ios - Obj-C - 来自 XIB 的自定义地图注释标注视图
问题描述
当我的 MapView 上的注释被点击时,我试图有一个自定义视图/XIB 显示。也就是说,我已经为 swift 找到了这个问题的各种答案——但没有找到目标 C 的答案。
目前,我可以使用以下代码显示自定义注释:
视图控制器.m
-(void)viewDidLoad {
NSMutableDictionary *viewParamsFriend = [NSMutableDictionary new];
[viewParamsFriend setValue:@"accepted_friends" forKey:@"view_name"];
[DIOSView viewGet:viewParamsFriend success:^(AFHTTPRequestOperation *operation, id responseObject) {
self.friendData = [responseObject mutableCopy];
int index = 0;
for (NSMutableDictionary *multiplelocationsFriend in self.friendData) {
NSString *location = multiplelocationsFriend[@"address2"];
NSString *userNames = multiplelocationsFriend[@"node_title"];
NSString *userBio = multiplelocationsFriend[@"body"];
CLGeocoder *geocoderFriend = [[CLGeocoder alloc] init];
[geocoderFriend geocodeAddressString:location
completionHandler:^(NSArray* placemarks, NSError* error){
if (placemarks && placemarks.count > 0) {
CLPlacemark *topResult = [placemarks objectAtIndex:0];
MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
MKCoordinateRegion region = self.friendsMapView.region;
region.span.longitudeDelta /= 150.0;
region.span.latitudeDelta /= 150.0;
PointAnnotation *point = [[PointAnnotation alloc] init];
point.coordinate = placemark.coordinate;
point.title = userNames;
point.subtitle = userBio;
point.index = index; // Store index here.
[self.friendsMapView addAnnotation:point];
}
}
];
index = index + 1;
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Failure: %@", [error localizedDescription]);
}];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
if ([annotation isKindOfClass:[MKPointAnnotation class]])
{
MKAnnotationView *pinView = (MKAnnotationView*)[self.friendsMapView dequeueReusableAnnotationViewWithIdentifier:@"AnnotationIdentifier"];
if (!pinView)
{
pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"AnnotationIdentifier"];
pinView.image = [UIImage imageNamed:@"mapann3.png"];
} else {
pinView.annotation = annotation;
}
pinView.canShowCallout = YES;
pinView.calloutOffset = CGPointMake(0, 0);
UIImageView *iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mapann3.png"]];
pinView.leftCalloutAccessoryView = iconView;
return pinView;
}
return nil;
}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
UITapGestureRecognizer *tapGesture2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(calloutTappedTwo:)];
[view addGestureRecognizer:tapGesture2];
}
-(void)calloutTappedTwo:(UITapGestureRecognizer *) sender
{
MKAnnotationView *view = (MKAnnotationView*)sender.view;
id <MKAnnotation> annotation = [view annotation];
if ([annotation isKindOfClass:[MKPointAnnotation class]])
{
PointAnnotation *selectedPoint = (PointAnnotation *) view.annotation;
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
OtherUserViewController *yourViewController = (OtherUserViewController *)[storyboard instantiateViewControllerWithIdentifier:@"OtherUserViewController"];
NSMutableDictionary *dictionary = self.friendData[selectedPoint.index];
yourViewController.frienduserData = dictionary;
[self.navigationController pushViewController:yourViewController animated:YES];
}
}
也就是说,如果我希望我创建的自定义 XIB 在点击每个注释时显示为标注,我应该在哪里/如何调用它?
解决方案
推荐阅读
- arrays - 确定数组/字符串中最大匹配序列的有效方法(在 Ruby 中)?
- r - 如何计算R中两个时间戳列的平均值?
- xslt - XSL:按元素匹配是否与按元素名称匹配?
- validation - gSheet - Google Apps 脚本 - 复制 DataValidation - 下拉菜单:“正在加载...”
- bootstrap-4 - Bootstrap Radio Group 无法在选项卡面板中选择
- amazon-web-services - AWS boto3 在创建 AMI 后无法放置标签
- openmdao - 安装 OpenMDAO 2.6.0 错误
- terraform - 地形 | aws_route53_record | 构建变更集时出错:InvalidChangeBatch
- winapi - 带有 RIDEV_NOHOTKEYS 的原始输入键盘显示经典的 Alt+Tab 菜单
- unity3d - 在 Unity 中离开相机视野后销毁游戏对象