【问题标题】:Displaying MKAnnotationViews that are within the bounds of the visible mapview显示在可见地图视图范围内的 MKAnnotationViews
【发布时间】:2016-02-06 21:05:00
【问题描述】:

我正在根据地图视图的可观察部分向我的服务器查询最接近用户的位置列表。

我不完全确定如何执行此操作,但我是否应该发送地图视图可见区域的中心纬度和经度,然后根据最近的半径搜索结果?

我知道MKMapView 有两个属性,分别称为regionvisibleMapRect。我应该使用其中一种吗?如果可以,根据我的问题,哪一种更合适?

编辑 当您搜索附近的位置时,我希望实现与苹果地图和 Yelp 应用程序相同的功能,它会根据地图视图的可见部分向您显示相关内容。

编辑 2 我见过很多人将mapview 的可见部分分成象限,最常见的是 NW、NE、SW 和 SE。但是,我仍然不完全确定他们为什么要这样做。我想知道查询后端的最佳方法,该后端包含每个位置的经纬度,以查找mapview 上存在的位置。

我在 stackoverflow 上到处查看,发现了一个类似的问题 here。但是,它没有回答我的问题,也没有提到visibleMapRect,因为它已经超过 5 年了。

非常感谢任何帮助。

【问题讨论】:

  • 那么你的位置经理至少在工作吗?
  • 当然可以。位置列表存储在服务器端的 mysql 数据库中。此外,我的问题与是否设置了位置管理器无关,而是如何根据 mkmapview 的可查看部分检索位置存储服务器端的问题。
  • 我的回答是基于您有位置管理器工作的事实,因为您想显示当前位置附近的位置,对吗?
  • 不一定在用户当前位置附近,而是在他查看地图的任何位置。例如,我的位置可能在洛杉矶,但如果我向上滚动到旧金山,它会更新我正在查看的位置。这有意义吗?

标签: ios swift location mkmapview mapkit


【解决方案1】:

因此,您需要为您的服务器调用提供这 2 个坐标的左下角和右上角 4 个点。

  1. 如果位置的纬度 > bottomleft.lat 并且如果位置的纬度
  2. if long of location > bottomleft.long 并且 if long of location

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)
}

如果您还有问题,请发表评论

【讨论】:

  • 你为什么不使用 visibleMapRect,那不是更容易吗?
  • 此属性表示与区域属性相同的基本信息,但指定为地图矩形而不是区域。如果你愿意,你可以使用 visibleMapRect 来代替它,你仍然需要获得 4 个边界点
  • 您能否编辑代码以使用 region 或 visibleMapRect?现在,您将其基于用户所在的位置,这无法回答我的问题。之后我很乐意奖励你积分。
  • 这看起来像我想要的。我还没有实施它,因为我得了流感。但如果我有任何其他问题,我会告诉你。给你50分。谢谢杰克!
【解决方案2】:

编辑 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-06
    • 1970-01-01
    相关资源
    最近更新 更多