【问题标题】:How can I create a custom callout view for MapKit user location?如何为 MapKit 用户位置创建自定义标注视图?
【发布时间】:2012-05-04 06:28:35
【问题描述】:

我已经为我的所有地图图钉创建了一个自定义 MKPinAnnotationView,但是我在自定义用户位置图钉时遇到了问题。如果我为它创建一个自定义图钉,那么我会丢失蓝色脉动点,并用我不想要的图钉替换它。所以我在 viewForAnnotation 方法中返回 nil:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{   
    if(annotation == self.mapView.userLocation)
    {
        // show the blue dot for user's GPS location
        return nil;
    }

    ... code for custom annotation pins goes here
}

但是,我确实希望它在我点击它时调用自定义标注视图。我尝试在选择蓝点时添加子视图:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{    
    if(view == [self.mapView viewForAnnotation:self.mapView.userLocation])
    {
        MKAnnotationView *userLocationView = [self.mapView viewForAnnotation:self.mapView.userLocation];

        userLocationCalloutView = [[MyAnnotationCalloutView alloc] initWithFrame:CGRectMake(-113, -58, 247, 59)];
        [userLocationCalloutView.rightAccessoryButton addTarget:self action:@selector(pinButtonTapped) forControlEvents:UIControlEventTouchDown];
        [userLocationView addSubview:userLocationCalloutView];
    }
}

但问题在于标注附件按钮位于点击区域之外,我无法添加 hitTest:withEvent 因为蓝点注释视图是私有的......有什么想法吗?

【问题讨论】:

    标签: ios mkmapview mapkit mkannotationview userlocation


    【解决方案1】:

    可以在委托方法mapView:didAddAnnotationViews中获取userLocation注解构建的MKAnnotationView:

    - (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
    {
        for(MKAnnotationView *view in views)
        {
            if (view.annotation == mapView.userLocation)
            {
                view.canShowCallout = YES;
                view.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
                break;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 2016-05-04
      • 2019-11-07
      • 2012-07-11
      相关资源
      最近更新 更多