gpt4 book ai didi

iphone - 获取 MKMapView 的边界

转载 作者:行者123 更新时间:2023-12-03 18:15:03 26 4
gpt4 key购买 nike

为了设置对外部服务器的查询,我想获取我正在构建的 iPhone 应用程序中当前 map View 的边界。 UIView 应该响应边界,但似乎 MKMapView 没有。设置区域并放大 map 后,我尝试获取边界。我被困在第一步,即尝试获取代表 map 东南角和西北角的 CGPoints。之后我要使用:

- (CLLocationCoordinate2D)convertPoint:(CGPoint)point toCoordinateFromView:(UIView *)view

将点转换为 map 坐标。但我什至无法做到这一点......

//Recenter and zoom map in on search location
MKCoordinateRegion region = {{0.0f, 0.0f}, {0.0f, 0.0f}};
region.center = mySearchLocation.searchLocation.coordinate;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[self.mapView setRegion:region animated:YES];


//After the new search location has been added to the map, and the map zoomed, we need to update the search bounds
//First we need to calculate the corners of the map
CGPoint se = CGPointMake(self.mapView.bounds.origin.x, mapView.bounds.origin.y);
CGPoint nw = CGPointMake((self.mapView.bounds.origin.x + mapView.bounds.size.width), (mapView.bounds.origin.y + mapView.bounds.size.height));
NSLog(@"points are: se %@, nw %@", se, nw);

代码编译时不会出现警告,但 se 和 nw 均为 null。查看 self.mapView.bounds.origin.x ,变量为 0。尝试直接 NSLog self.mapView.bounds.size.width 给我一个“程序收到信号:“EXC_BAD_ACCESS”。这似乎来自 NSLog。

有人知道从 MKMapView 的可见区域获取东南角和西北角(在 map 坐标中)的正确方法吗?

编辑:似乎每当您在这里问一些问题时,答案都会立即出现。我使用 %@ 而不是 @f 来打印 NSLog 中的每个变量,这会引发错误。我还发现了MKMapview的annotationVisibleRect属性。看来,annotationVisibleRect 是基于父 View 坐标的。

最佳答案

好吧,我正式回答了我自己的问题,但由于我在任何地方都找不到它,所以我将在这里发布答案:

//To calculate the search bounds...
//First we need to calculate the corners of the map so we get the points
CGPoint nePoint = CGPointMake(self.mapView.bounds.origin.x + mapView.bounds.size.width, mapView.bounds.origin.y);
CGPoint swPoint = CGPointMake((self.mapView.bounds.origin.x), (mapView.bounds.origin.y + mapView.bounds.size.height));

//Then transform those point into lat,lng values
CLLocationCoordinate2D neCoord;
neCoord = [mapView convertPoint:nePoint toCoordinateFromView:mapView];

CLLocationCoordinate2D swCoord;
swCoord = [mapView convertPoint:swPoint toCoordinateFromView:mapView];

SWIFT 5

public extension MKMapView {

var newBounds: MapBounds {
let originPoint = CGPoint(x: bounds.origin.x + bounds.size.width, y: bounds.origin.y)
let rightBottomPoint = CGPoint(x: bounds.origin.x, y: bounds.origin.y + bounds.size.height)

let originCoordinates = convert(originPoint, toCoordinateFrom: self)
let rightBottomCoordinates = convert(rightBottomPoint, toCoordinateFrom: self)

return MapBounds(
firstBound: CLLocation(latitude: originCoordinates.latitude, longitude: originCoordinates.longitude),
secondBound: CLLocation(latitude: rightBottomCoordinates.latitude, longitude: rightBottomCoordinates.longitude)
)
}
}

public struct MapBounds {
let firstBound: CLLocation
let secondBound: CLLocation
}

用法

self.mapView.newBounds

关于iphone - 获取 MKMapView 的边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2081753/

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