如何在地图的注释的标注中添加自定义视图 [英] How To add custom View in map's Annotation's Callout's
问题描述
这是我的代码。我想添加自己的自定义标注视图而不是iOS默认值。我知道只有左侧标注和右侧标注视图,但我需要添加一个带有我自己的背景和标签的视图工具提示类型。
Here is my code. I want to add my own custom callout view instead of iOS default. I know there is only left callout and right callout view, but I need to add a view tooltip type with my own background and label.
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView *userAnnotationView = nil;
if ([annotation isKindOfClass:MKUserLocation.class])
{
userAnnotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"UserLocation"];
if (userAnnotationView == nil) {
userAnnotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"UserLocation"];
}
else
userAnnotationView.annotation = annotation;
userAnnotationView.enabled = YES;
userAnnotationView.canShowCallout = YES;
userAnnotationView.image = [UIImage imageNamed:@"map_pin.png"];
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0,0,141,108)];
view.backgroundColor = [UIColor clearColor];
UIImageView *imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"toltip.png"]];
[view addSubview:imgView];
userAnnotationView.leftCalloutAccessoryView = view;
return userAnnotationView;
}
}
推荐答案
我有同样的问题并最终做了以下事情:
I had same problem and ended up doing following thing:
当我收到mapView委托回调
When I receive mapView delegate callback
-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
(是时候我要显示自定义的CalloutView)
(it's time when I want to show my custom CalloutView)
我使用收到的视图作为参数 *(MKAnnotationView )视图 (这是一个引脚视图),只需使用
I use view received as parameter *(MKAnnotationView )view (which is a pin view) and simply add my custom view to that pin view using
[view addSubview:customView];
它会在该引脚视图的顶部添加您的自定义视图,以便我们希望它在引脚上方我们必须更改自定义视图中心属性,如下所示:
It will add your custom view on top of that pin view so if we want it to be above pin we have to change custom view center property like this:
CGRect customViewRect = customView.frame;
CGRect rect = CGRectMake(-customViewRect.size.width/2, -customViewRect.size.height-7, customViewRect.size.width, customViewRect.size.height);
customView.frame = rect;
[view addSubview:customView];
在我的情况下看起来像这样
In my case it looks like this
!注意:
有一点需要注意,但是 自定义视图会忽略触摸事件 ,因为 mapView 以不同的方式处理。这是一个 快速修复 :
There is one caveat which however can be easily fixed, your custom view will ignore touch events, because of mapView working with touches differently. Here's a quick fix:
1)子类 MKAnnotationView (或 MKPinAnnotationView 取决于您的需要)
1) Subclass MKAnnotationView (or MKPinAnnotationView depends on what you need)
2)在mapView委托中
2) in your mapView delegate
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
使用您的子类而不是MKAnnotationView / MKPinAnnotationView
use your subclass instead of MKAnnotationView/MKPinAnnotationView
3)在您的子类.m文件中覆盖以下两种方法:
3) in your subclass .m file override two methods as following:
- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event
{
UIView* hitView = [super hitTest:point withEvent:event];
if (hitView != nil)
{
[self.superview bringSubviewToFront:self];
}
return hitView;
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event
{
CGRect rect = self.bounds;
BOOL isInside = CGRectContainsPoint(rect, point);
if(!isInside)
{
for (UIView *view in self.subviews)
{
isInside = CGRectContainsPoint(view.frame, point);
if(isInside)
break;
}
}
return isInside;
}
全部完成!
这篇关于如何在地图的注释的标注中添加自定义视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!