gpt4 book ai didi

ios - 从 MKTileOverlayPath 获取 MKMapRect

转载 作者:可可西里 更新时间:2023-11-01 05:04:23 31 4
gpt4 key购买 nike

我搜索了很多答案,我无法想象这样的方法不存在!!我有一个 MKTileOverlayPath (x , y & z) 并且还能够得到这个矩形中心的经度/纬度。现在我需要获取每个矩形的跨度,这可能吗?

最佳答案

是的,尽管仅使用 MapKit 并不完全明显。

图 block 和叠加路径在网格系统中定义,其中(正方形) map 一侧的点数等于 2^z * 256,因为默认屏幕图 block 大小为 256px 正方形。 z0 是一个 256px 正方形图 block ,z1 是四个图 block ,总世界为 512px 正方形,依此类推。该系统中的坐标是线性的(正方形 map 中的所有正方形)。

MKMapPoint 派生自 z20,因此 MKMapSizeWorld 是一侧的 268,435,456 map 点。如果您计算一下,2^20 * 256 = 268,435,456。 map 点也是线性的(方形 map 中的像素/点)。

纬度和经度当然不是线性的,因为这是一个投影表示,这就是为什么你有像 MKMetersPerMapPointAtLatitude() 这样的函数。

如果您有以下 MKTileOverlayPath:

  • z = 10
  • x = 12
  • y = 8

您知道世界点的大小是 2^10 * 256 = 262,144 并且世界是 2^10 = 1,024 方 block 。

图 block 的左边缘是 256 * 12 = 3,072 点,上边缘是 256 * 8 = 2,048 点。这些与 z10 有关,268,435,456/262,144 = 1,024z20 小。

这是 { x: (3,072 * 1,024 = 3,145,728), y: (2,048 * 1,024 = 2,097,152) MKMapPoint

右下角同样是 { x: 3,407,872, y: 2,359,296 }(将 256 * 1,024 = 262,144 的图 block 大小添加到每个)。

您可以对每个使用 MKCoordinateForMapPoint() 得到 CLLocationCoordinate2D,并减去它们的差异以获得 MKCoordinateSpan

  • 左上角:{ 纬度:84.8024737243345,经度:-175.78125 }
  • 右下:{ 纬度:84.7705283207591,经度:-175.4296875 }
  • 跨度:{ latitudeDelta:0.0319454035754205,longitudeDelta:0.3515625 }

是的,这些点非常靠近 map 左上角的阿拉斯加区域,但这是合乎逻辑的,因为 x = 12y = 8 1,024 个图 block ,从左上角开始编号。

@interface GridTileOverlay : MKTileOverlay

@end

@implementation GridTileOverlay
-(void)loadTileAtPath:(MKTileOverlayPath)path result:(void (^)(NSData *, NSError *))result {
NSLog(@"Loading tile x/y/z: %ld/%ld/%ld",(long)path.x,(long)path.y,(long)path.z);

int worldPointSize = pow(2,(int)path.z)*256; // 2^10 * 256 = 262,144
int tilesOnASide = pow(2,(int)path.z); // 2^10 = 1,024
long leftEdge = path.x *256; // 256 * 12 = 3,072 points
long topEdge = path.y *256; // 256 * 8 = 2,048
int w = self.boundingMapRect.size.width; // 2^20 * 256 = 268,435,456
int zScale = w / worldPointSize; // 268,435,456 / 262,144 = 1,024
int tileSize = 256 * zScale; // 256 * 1,024 = 262,144
long x0 = leftEdge*zScale; // 3,072 * 1,024 = 3,145,728
long y0 = topEdge*zScale; // 2,048 * 1,024 = 3,145,728
long x1 = x0 + tileSize;
long y1 = y0 + tileSize;

MKMapPoint ul = MKMapPointMake(x0, y0); // upper left
MKMapPoint lr = MKMapPointMake(x1, y1); // lower right
MKMapRect mapRect = MKMapRectMake (fmin(ul.x, lr.x),
fmin(ul.y, lr.y),
fabs(ul.x - lr.x),
fabs(ul.y - lr.y));
}
@end

关于ios - 从 MKTileOverlayPath 获取 MKMapRect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27418144/

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