gpt4 book ai didi

cocoa - MKMapView 与其区域属性未对齐

转载 作者:行者123 更新时间:2023-12-03 17:38:52 29 4
gpt4 key购买 nike

我想在 MKMapView 中显示某个 map 区域,但是当我在 map 上放置一个具有相同参数的矩形叠加层时,它会垂直显示不对齐。在赤道附近看起来足够好,但偏差随着纬度和跨度的增加而增加。

这是针对 Mac 应用程序的,但对于 iOS 来说应该是相同的。

这是我的相关代码:

MKCoordinateRegion mapRegion = MKCoordinateRegionMake(CLLocationCoordinate2DMake(latCenter, lonCenter), MKCoordinateSpanMake(mapWidthY, mapWidthX));
self.radarMap.region = mapRegion;
CLLocationCoordinate2D coordinates[4];

coordinates[0] = CLLocationCoordinate2DMake(latCenter+mapWidthY/2, lonCenter+mapWidthX/2);
coordinates[1] = CLLocationCoordinate2DMake(latCenter+mapWidthY/2, lonCenter-mapWidthX/2);
coordinates[2] = CLLocationCoordinate2DMake(latCenter-mapWidthY/2, lonCenter-mapWidthX/2);
coordinates[3] = CLLocationCoordinate2DMake(latCenter-mapWidthY/2, lonCenter+mapWidthX/2);
self.boundaryOverlay = [MKPolygon polygonWithCoordinates:coordinates count:4];
[self.radarMap addOverlay:self.boundaryOverlay];

它显示了这一点:(请注意,蓝色矩形覆盖层向上移动,因此上部区域不会显示): enter image description here

而不是这样的:(我知道它以宽高比填充显示): enter image description here

最佳答案

当您设置 MKMapView 对象的区域属性时,MapKit 会调整区域属性的值,使其与实际可见区域相匹配。这意味着区域的实际值并不完全是您分配给它的值。因此,您应该从 MKMapView 对象获取区域的更新值并使用它来创建多边形,而不是使用分配给 map 的区域来创建多边形。

MKCoordinateRegion mapRegion = MKCoordinateRegionMake(CLLocationCoordinate2DMake(latCenter, lonCenter), MKCoordinateSpanMake(mapWidthY, mapWidthX));
self.radarMap.region = mapRegion;
CLLocationCoordinate2D coordinates[4];

// Get the actual region that MapKit is using
MKCoordinateRegion actualMapRegion = self.radarMap.region;
CLLocationDegrees actualLatCenter = actualMapRegion.center.latitude;
CLLocationDegrees actualLonCenter = actualMapRegion.center.longitude;
CLLocationDegrees actualLatSpan = actualMapRegion.span.latitudeDelta;
CLLocationDegrees actualLonSpan = actualMapRegion.span.longitudeDelta;

// And use that to create the polygon
coordinates[0] = CLLocationCoordinate2DMake(actualLatCenter+ actualLatSpan/2, actualLonCenter+ actualLonSpan/2);
coordinates[1] = CLLocationCoordinate2DMake(actualLatCenter+ actualLatSpan/2, actualLonCenter-actualLonSpan/2);
coordinates[2] = CLLocationCoordinate2DMake(actualLatCenter-actualLatSpan/2, actualLonCenter-actualLonSpan/2);
coordinates[3] = CLLocationCoordinate2DMake(actualLatCenter-actualLatSpan/2, actualLonCenter+ actualLonSpan/2);
self.boundaryOverlay = [MKPolygon polygonWithCoordinates:coordinates count:4];
[self.radarMap addOverlay:self.boundaryOverlay];

当你向北移动时,我对你所看到的日益增加的错位感到好奇。我想到您可能对mapWidthX 和mapWidthY 使用固定比率。 MapKit 使用非等角投影。这样做的结果之一是 map 在南北方向上拉伸(stretch),距离赤道越远拉伸(stretch)得越多。

如果您使用适合赤道的比例创建区域,则当您向两极移动时,该比例将会不正确。 MKMapView 将获取您提供的区域并显示靠近它的内容。但距离赤道越远,需要进行的调整就越多。而且你给它的区域和它实际使用的区域之间的差异就越大。

关于cocoa - MKMapView 与其区域属性未对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27031786/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com