1

我试图让一个自定义的 MKAnnotationView (PostFlag) 在选择它时做两件事:1) 展开以显示有关注释的详细信息,以及 2) 在地图中居中。The trouble is that when the annotation is selected, the MKMapView sometimes dramatically zooms out. 这导致我处理重要缩放事件的逻辑运行,它将附近的注释收集到常见的注释视图中,导致注释视图重新加载,这通常用关闭的注释视图替换用户试图查看的注释。

我尝试解决此问题的方法是尝试确保在尝试使注释居中时不会错误地设置区域(例如,跨度会导致缩小)。那里的一切似乎都很好 - 当我设置它时,该区域是正确的。委托方法 mapView:didChangeRegion:animated: 仍然会被调用,尽管区域缩小了。我不确定那里到底发生了什么。

在将区域更改为注释居中之前将 scrollEnabled 和 zoomEnabled 设置为 NO 似乎没有任何效果。

我是否为注释视图的扩展设置动画并不重要 - 缩放仍然会发生。

这听起来很熟悉吗?这是一些代码:

- (void) mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
    self.selectedFlag = (PostFlag*)view;  //disables scrolling/zooming on the map

    //move region to show expanded flag
    float size = (m_selectedFlag.expandedHeight / 2.0) + 50; //getting a comfortable position in the display
    float dLat = size * mapView.region.span.latitudeDelta / mapView.frame.size.height;  //get span in degrees from degree to pixel ratio

    //build a new coordinate for our center location
    CLLocationCoordinate2D originalCoord = ((PostAnnotation*)m_selectedFlag.annotation).coordinate;
    CLLocationCoordinate2D offsetCoord = CLLocationCoordinate2DMake(originalCoord.latitude + dLat, originalCoord.longitude);

    //adjust the map view to center on that new coordinate
    [m_mapView setRegion: MKCoordinateRegionMake(offsetCoord, m_mapRegionAtTimeOfLastUpdate.span) animated: YES];
}


- (void) setSelectedFlag:(PostFlag *) val
{
    if(m_selectedFlag)
    {
        m_selectedFlag.expanded = NO;
        [m_selectedFlag release];
    }

    if(val)
        m_selectedFlag = [val retain];
    else
        m_selectedFlag = nil;

    if(m_selectedFlag)
    {
        m_selectedFlag.expanded = YES;

        [m_mapView bringSubviewToFront: m_selectedFlag];
        m_mapView.scrollEnabled = NO;
        m_mapView.zoomEnabled = NO;
    }
    else
    {
        m_mapView.scrollEnabled = YES;
        m_mapView.zoomEnabled = YES;
    }
}

当 PostFlag 折叠时,它告诉这个视图控制器和 vc 重新启用地图上的滚动和缩放。因此,为了清楚起见,当注释视图展开时,地图上的缩放和滚动被关闭。

- (void) postFlagWasCollapsed: (PostFlag*) flag
{
    [self.expandedFlags removeObject: flag];

    if([self.expandedFlags count] == 0)
    {
        m_mapView.scrollEnabled = YES;
        m_mapView.zoomEnabled = YES;
    }
}

另外,为了澄清起见,我保留了扩展注释视图的数组。由于用户可以点击附近的注释视图并在先前展开的注释视图关闭时将其展开,因此可能会出现两个注释视图同时展开的失误。因此,为了确保在关闭所有注释之前我不会重新打开滚动/缩放,我使用了数组。

- (void) postFlagWasExpanded: (PostFlag*) flag
{
    if(!self.expandedFlags)
        self.expandedFlags = [NSMutableArray array];

    if([self.expandedFlags indexOfObject: flag] == NSNotFound)
        [self.expandedFlags addObject: flag];

}

所以这可能比你想要的更多细节。但是,如果有人知道为什么在选择注释视图时地图视图会缩小,请加入。提前致谢!

4

2 回答 2

1

I don't think scrollEnabled means what you think it means. It stops the user from scrolling, but it doesn't stop your code from telling the map to scroll. Same goes for zooms.

"If you set the value of this property to NO, you may still change the map location programmatically by changing the value in the region property."

Ref: http://developer.apple.com/library/ios/DOCUMENTATION/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html#//apple_ref/occ/instp/MKMapView/scrollEnabled

So forget meddling with scrollEnabled. Look again at your setRegion call. I would think your span is coming out wrong. Log the region and tell us what it sets when it is doing what you want and when it radically zooms out.

于 2012-08-14T01:06:30.340 回答
0

这可能已经解决了,但是如果其他人有同样的问题,只需将setRegion替换为setCenterCoordinate

根据苹果文档,setRegion 将对视图中的注释进行假设并相应地更改缩放。setCenterCoordinate 将不进行此类评估,而只是将地图以您指定为地图中心的坐标为中心。

希望这对某人有所帮助,因为它也困扰了我一段时间。

于 2012-11-20T10:14:37.660 回答