我正在查询我的服务器,根据他的mapview的可观察部分找到最接近用户的位置列表。
我不完全确定如何做到这一点,但是我应该发送中心纬度和mapview的可见区域的长度,然后根据最近的半径搜索结果吗?
我知道MKMapView
有两个名为region
和visibleMapRect
的属性。我应该使用其中之一,如果是的话,根据我的问题哪一个更合适?
修改 我正在寻找实现与苹果地图和Yelp应用程序相同的功能,当您搜索附近的位置时,它会根据地图视图的可见部分显示相关内容。
编辑2
我看到很多人将他们mapview
的可见部分分成象限,最常见的是NW,NE,SW和SE。但是,我仍然不完全确定他们为什么这样做。我想知道查询后端的最佳方法,后端包含每个位置的lat和long,以查找mapview
上存在的位置。
我在stackoverflow上到处查找,我发现了一个类似的问题here。但是,它没有回答我的问题而且没有提到visibleMapRect
,因为它已经超过5年了。
非常感谢任何帮助。
答案 0 :(得分:4)
所以你需要提供这两个coord bottomleft和topright 4点的服务器调用。
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)
}
如果您有任何问题,请发表评论
答案 1 :(得分:1)
编辑2 我看到很多人将
mapview
的可见部分划分为象限,最常见的是NW,NE,SW和SE。 但是,我还不完全确定他们为什么要这样做。我会 想知道查询后端的最佳方法,后端包含一个lat 并且每个位置的长度,找到存在于的位置mapview
。
我不确定这是否是您所指的,但他们可能会使用quadtrees作为有效搜索大量坐标位置的方法。
This post from Thoughtbot演示如何使用四叉树在地图上显示数千个地点坐标作为聚类,它包含非常简洁地解释其工作方式的GIF:
四叉树是包括存储点桶和边界框的节点的数据结构。节点的边界框中包含的任何点都将添加到其存储桶中。一旦桶被填满,节点就会将自身分成四个节点,每个节点都有一个边界框,对应于其父节点边界框的象限。所有进入父母的桶的点现在都会进入其中一个孩子的桶中。
来源:How To Efficiently Display Large Amounts of Data on iOS Maps