显示可见地图视图边界内的 MKAnnotationViews
Displaying MKAnnotationViews that are within the bounds of the visible mapview
我正在向我的服务器查询根据用户在地图视图中的可观察部分最近的位置列表。
我不太确定该怎么做,但我应该发送 mapview 可见区域的中心纬度和经度,然后根据最近的半径搜索结果吗?
我知道 MKMapView
有两个属性,分别是 region
和 visibleMapRect
。我应该使用其中之一吗?如果是,根据我的问题,哪一个更合适?
编辑
当您搜索附近的位置时,我希望实现与苹果地图和 Yelp 应用程序相同的功能,它会根据地图视图的可见部分向您显示相关内容。
编辑 2
我见过很多人将他们 mapview
的可见部分分成象限,最常见的是 NW、NE、SW 和 SE。但是,我仍然不完全确定他们为什么要这样做。我想知道查询后端的最佳方法,其中包含每个位置的纬度和经度,以找到 mapview
上存在的位置。
我在 Whosebug 上到处查看,发现了一个类似的问题 here。但是,它没有回答我的问题,也没有提到 visibleMapRect
因为它已经超过 5 年了。
非常感谢任何帮助。
所以您需要为您的服务器调用提供这 2 个坐标 bottomleft 和 topright 4 个点。
- 如果位置纬度 > bottomleft.lat 并且如果位置纬度 < topright.lat
- 如果位置长 > bottomleft.long 并且如果位置长 < topright.long
iOS 代码看起来像这样使用这四个变量作为参数的 make 服务器调用
fun viewDidLoad() {
super.viewDidLoad()
self.mapView.delegate = self
}
func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
// Using region
var span: MKCoordinateSpan = mapView.region.span
var center: CLLocationCoordinate2D = mapView.region.center
// This is the farthest Lat point to the left
var farthestLeft = center.latitude - span.latitudeDelta * 0.5
// This is the farthest Lat point to the Right
var farthestRight = center.latitude + span.latitudeDelta * 0.5
// This is the farthest Long point in the Upward direction
var farthestTop = center.longitude - span.longitudeDelta * 0.5
// This is the farthest Long point in the Downward direction
var farthestBottom = center.longitude + span.longitudeDelta * 0.5
var SWCoord = MKCoordinateForMapPoint(farthestBottom, farthestLeft)
var NECoord = MKCoordinateForMapPoint(farthestTop, farthestRight)
// Using visibleMapRect
var mapRect = mapView.visibleMapRect
// This is the top right Coordinate
var NECoord = getCoordinateFromMapRectanglePoint(MKMapRectGetMaxX(mapRect), y: mapRect.origin.y)
// This is the bottom left Coordinate
var SWCoord = getCoordinateFromMapRectanglePoint(mapRect.origin.x, y: MKMapRectGetMaxY(mapRect))
// Not needed but could be useful
// var NWCoord = getCoordinateFromMapRectanglePoint(MKMapRectGetMinX(mapRect), y: mapRect.origin.y)
// var SECoord = getCoordinateFromMapRectanglePoint(MKMapRectGetMaxX(mapRect), y: MKMapRectGetMaxY(mapRect))
}
func getCoordinateFromMapRectanglePoint(x: Double, y: Double) -> CLLocationCoordinate2D {
var mapPoint = MKMapPointMake(x, y)
return MKCoordinateForMapPoint(mapPoint)
}
如果您还有任何问题,请发表评论
EDIT 2 I have seen a lot of people break up the visible portion of their mapview
into quadrants, most commonly NW,NE,SW and SE.
However, I'm still not entirely sure why theyr'e doing this. I would
like to know the best way to query the back end, which contains a lat
and long for each location, to find the locations which exist on the
mapview
.
我不确定这是否是您所指的,但他们可以使用 quadtrees 作为一种有效搜索大量坐标位置的方法。
This post from Thoughtbot 演示了如何使用四叉树在地图上将数以千计的位置坐标显示为集群,它包括非常简洁地解释其工作原理的 gif:
A quad tree is a data structure comprising nodes which store a bucket of points and a bounding box. Any point which is contained within the node’s bounding box is added to its bucket. Once the bucket gets filled up, the node splits itself into four nodes, each with a bounding box corresponding to a quadrant of its parents bounding box. All points which would have gone into the parent’s bucket now go into one of its children’s buckets.
来源:How To Efficiently Display Large Amounts of Data on iOS Maps
我正在向我的服务器查询根据用户在地图视图中的可观察部分最近的位置列表。
我不太确定该怎么做,但我应该发送 mapview 可见区域的中心纬度和经度,然后根据最近的半径搜索结果吗?
我知道 MKMapView
有两个属性,分别是 region
和 visibleMapRect
。我应该使用其中之一吗?如果是,根据我的问题,哪一个更合适?
编辑 当您搜索附近的位置时,我希望实现与苹果地图和 Yelp 应用程序相同的功能,它会根据地图视图的可见部分向您显示相关内容。
编辑 2
我见过很多人将他们 mapview
的可见部分分成象限,最常见的是 NW、NE、SW 和 SE。但是,我仍然不完全确定他们为什么要这样做。我想知道查询后端的最佳方法,其中包含每个位置的纬度和经度,以找到 mapview
上存在的位置。
我在 Whosebug 上到处查看,发现了一个类似的问题 here。但是,它没有回答我的问题,也没有提到 visibleMapRect
因为它已经超过 5 年了。
非常感谢任何帮助。
所以您需要为您的服务器调用提供这 2 个坐标 bottomleft 和 topright 4 个点。
- 如果位置纬度 > bottomleft.lat 并且如果位置纬度 < topright.lat
- 如果位置长 > bottomleft.long 并且如果位置长 < topright.long
iOS 代码看起来像这样使用这四个变量作为参数的 make 服务器调用
fun viewDidLoad() {
super.viewDidLoad()
self.mapView.delegate = self
}
func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
// Using region
var span: MKCoordinateSpan = mapView.region.span
var center: CLLocationCoordinate2D = mapView.region.center
// This is the farthest Lat point to the left
var farthestLeft = center.latitude - span.latitudeDelta * 0.5
// This is the farthest Lat point to the Right
var farthestRight = center.latitude + span.latitudeDelta * 0.5
// This is the farthest Long point in the Upward direction
var farthestTop = center.longitude - span.longitudeDelta * 0.5
// This is the farthest Long point in the Downward direction
var farthestBottom = center.longitude + span.longitudeDelta * 0.5
var SWCoord = MKCoordinateForMapPoint(farthestBottom, farthestLeft)
var NECoord = MKCoordinateForMapPoint(farthestTop, farthestRight)
// Using visibleMapRect
var mapRect = mapView.visibleMapRect
// This is the top right Coordinate
var NECoord = getCoordinateFromMapRectanglePoint(MKMapRectGetMaxX(mapRect), y: mapRect.origin.y)
// This is the bottom left Coordinate
var SWCoord = getCoordinateFromMapRectanglePoint(mapRect.origin.x, y: MKMapRectGetMaxY(mapRect))
// Not needed but could be useful
// var NWCoord = getCoordinateFromMapRectanglePoint(MKMapRectGetMinX(mapRect), y: mapRect.origin.y)
// var SECoord = getCoordinateFromMapRectanglePoint(MKMapRectGetMaxX(mapRect), y: MKMapRectGetMaxY(mapRect))
}
func getCoordinateFromMapRectanglePoint(x: Double, y: Double) -> CLLocationCoordinate2D {
var mapPoint = MKMapPointMake(x, y)
return MKCoordinateForMapPoint(mapPoint)
}
如果您还有任何问题,请发表评论
EDIT 2 I have seen a lot of people break up the visible portion of their
mapview
into quadrants, most commonly NW,NE,SW and SE. However, I'm still not entirely sure why theyr'e doing this. I would like to know the best way to query the back end, which contains a lat and long for each location, to find the locations which exist on themapview
.
我不确定这是否是您所指的,但他们可以使用 quadtrees 作为一种有效搜索大量坐标位置的方法。
This post from Thoughtbot 演示了如何使用四叉树在地图上将数以千计的位置坐标显示为集群,它包括非常简洁地解释其工作原理的 gif:
A quad tree is a data structure comprising nodes which store a bucket of points and a bounding box. Any point which is contained within the node’s bounding box is added to its bucket. Once the bucket gets filled up, the node splits itself into four nodes, each with a bounding box corresponding to a quadrant of its parents bounding box. All points which would have gone into the parent’s bucket now go into one of its children’s buckets.
来源:How To Efficiently Display Large Amounts of Data on iOS Maps