简体   繁体   English

为什么添加注释时地图视图会放大?

[英]Why is my map view zooming in when I add annotations?

I have a MKMapView that I configure with: 我有一个配置的MKMapView:

static let STARTING_MAP_RANGE: Double = 1000  // meters
. . .
let region: MKCoordinateRegion = MKCoordinateRegionMakeWithDistance(location,
                                                                    MapViewController.STARTING_MAP_RANGE,
                                                                    MapViewController.STARTING_MAP_RANGE)

mapView.setRegion(region, animated: true)

I add an annotation for the current location and it all looks fine. 我为当前位置添加了一个注释,一切看起来都很好。 When I add an annotation for other points in the visible region, the MKMapView zooms in to the minimum area needed to show all the annotations. 当我为可见区域中的其他点添加注释时,MKMapView会放大到显示所有注释所需的最小区域。

The weird thing is that I tried to figure out where this was happening by printing out the bottom left and top right latitude and longitude like this: 奇怪的是,我试图通过打印出像这样的左下和右上的经纬度来弄清楚这是哪里发生的:

private func printMapRegion(caller: String)
{
    let mapRect = mapView.visibleMapRect;
    let bottomLeft = MKCoordinateForMapPoint(MKMapPointMake(mapRect.origin.x, MKMapRectGetMaxY(mapRect)))
    let topRight = MKCoordinateForMapPoint(MKMapPointMake(MKMapRectGetMaxX(mapRect), mapRect.origin.y))

    print("\(caller):  (\(bottomLeft.latitude),\(bottomLeft.longitude)) -- (\(topRight.latitude),\(topRight.longitude))")
}

When I run this before and after setting the annotations, I get identical values, despite seeing the map zoom on the screen (both in the simulator and my iPhone). 在设置注释之前和之后运行此命令时,尽管在屏幕上看到了地图缩放(在模拟器和iPhone中),我都得到了相同的值。

I added a refresh button to reset the map. 我添加了刷新按钮以重置地图。 It works as far as zooming the map out, but it also reports that the bottom left and top right coordinates are the same before and after zooming. 它可以缩小地图,但是它还报告了缩放前后左下角和右上角的坐标相同。

Is there something wrong with my understanding of visibleMapRect? 我对visibleMapRect的理解有什么问题吗?

I found that the reason for the zooming is that I was adding the annotations like this: 我发现缩放的原因是我添加了如下注释:

    mapView.removeAnnotations(mapView.annotations)
    mapView.addAnnotations(annotations)
    mapView.showAnnotations(mapView.annotations, animated: true)

Removing the call to showAnnotations eliminated the problem. 删除对showAnnotations的调用可以解决此问题。 I'm still curious as to why visibleMapRect is reporting the same bounding coordinates after the visible zoom. 我仍然对为什么visibleMapRect在可见缩放后报告相同的边界坐标感到好奇。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM