Iphone 如何在地图';s注释';s标注';s
这是我的密码。我想添加自己的自定义详图索引视图,而不是iOS默认视图。我知道只有左详图索引视图和右详图索引视图,但我需要添加具有自己背景和标签的视图工具提示类型Iphone 如何在地图';s注释';s标注';s,iphone,maps,Iphone,Maps,这是我的密码。我想添加自己的自定义详图索引视图,而不是iOS默认视图。我知道只有左详图索引视图和右详图索引视图,但我需要添加具有自己背景和标签的视图工具提示类型 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { MKAnnotationView *userAnnotationView = nil;
- (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;
}
}
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释
{
MKAnnotationView*userAnnotationView=nil;
if([annotation isKindOfClass:MKUserLocation.class])
{
userAnnotationView=(MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@“UserLocation]”;
if(userAnnotationView==nil){
userAnnotationView=[[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@“UserLocation”];
}
其他的
userAnnotationView.annotation=注释;
userAnnotationView.enabled=是;
userAnnotationView.canShowCallout=是;
userAnnotationView.image=[UIImage ImageName:@“map_pin.png”];
UIView*view=[[UIView alloc]initWithFrame:CGRectMake(0,0141108)];
view.backgroundColor=[UIColor clearColor];
UIImageView*imgView=[[UIImageView alloc]initWithImage:[UIImageName:@“toltip.png”];
[视图添加子视图:imgView];
userAnnotationView.leftCalloutAccessoryView=view;
返回userAnnotationView;
}
}
这里是详图索引视图的最佳示例
我也有同样的问题,最后做了以下事情: 当我收到mapView委托回调时
-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
(此时我要显示自定义详图索引视图)
我使用收到的视图作为参数*(MKAnnotationView)视图(这是一个pin视图),只需使用
[view addSubview:customView];
它会将您的自定义视图添加到该管脚视图的顶部,因此如果我们希望它位于管脚上方,则必须更改“自定义视图中心”属性,如下所示:
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];
在我的情况下,它看起来像这样
!注:
有一个警告很容易解决,您的自定义视图将忽略触摸事件,因为地图视图处理触摸的方式不同。这里有一个快速解决方法:
1)子类MKAnnotationView(或MKPinAnnotationView取决于您需要什么)
2)在mapView代理中
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView?
全部完成我刚刚为我的地图视图创建了一个自定义标注,避免了点击时标注消失的问题。是我采取的步骤的要点。我创建了一个库来显示自定义标注 您可以传递详图索引的任何自定义视图! 它还支持uicontrol的事件
以Swift获得上述最佳答案 这可以节省几分钟来重写自己 !!注: 有一个很容易修正的警告,您的自定义视图将忽略触摸事件,因为mapView处理触摸的方式不同。这里有一个快速解决方案: 1) 子类MKAnnotationView(或MKPinAnnotationView取决于您需要什么) 2) 在mapView代理中
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView?
使用子类而不是MKAnnotationView/MKPinAnnotationView
3) 在子类.m文件中,重写以下两种方法:
- (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;
}
override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
let hitView = super.hitTest(point, withEvent: event)
if (hitView != nil)
{
self.superview?.bringSubviewToFront(self)
}
return hitView;
}
override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
let rect = self.bounds
var isInside = CGRectContainsPoint(rect, point)
if(!isInside) {
for view in self.subviews {
isInside = CGRectContainsPoint(view.frame, point)
break;
}
}
return isInside
}
你可以使用这个图书馆。它还为详图索引视图提供了一个随时可用的模板。为自定义视图添加酷动画和锚定视图
太棒了!为什么不选择此选项作为解决方案?非常有效,谢谢!你怎么知道的?我花了很长时间摆弄相框和
sizeToFit
,但都不管用!什么是visibleCalloutView?addSubview:customView.center=CGPointMake(view.bounds.size.width*0.5f,-self.visibleCalloutView.bounds.size.height*0.5f);在对象重复的方法“VisibleCallotView”中,我得到错误“在对象类型上找不到”,请帮助我。@iPatel这是因为添加的子视图超出了superview的visible rect。如果在MKAnnotationView
上设置clipstobunds
,您甚至看不到添加的子视图。从iOS9开始,有一个detailcalloaccessoryview
来定制callout。我已经实现了您的代码,但它不起作用。你能帮我吗?刚刚在iOS 9上使用过,目前看起来还不错。嗨,当选择了另一个注释时,我如何隐藏你的标注?@Misha当选择另一个注释时,标注应该自动隐藏。当标注太近/重叠时,这种行为似乎存在一些问题。当我有时间的时候,我会尝试修复并推送更新。我也希望如此。但它不起作用。标注未关闭:(我认为你的休息时间应该放在一个if-isInside{}