Display MKAnnotationViews that are within the visible view of the map

I request from my server a list of places that are closest to the user, based on his observed part of the map.

I'm not quite sure how to do this, but should I send the center of lat and long to the map's scope and then search for results based on the nearest radius?

I know that it MKMapViewhas two properties called regionand visibleMapRect. Should I use one of them, and if so, which one is more suitable for my question?

EDIT I'm looking to implement functionality that is identical to Apple maps and the Yelp app when you search for nearby locations, and shows what is important based on the visible part of the map view.

EDIT 2 I saw how many people split their visible part mapviewinto quadrants, most often into NW, NE, SW, and SE. However, I'm still not quite sure why they are doing this. I would like to know how best to request a back end that contains lat and long for each location to find the locations that exist in mapview.

I looked everywhere on stackoverflow, and I found a similar question here . However, he does not answer my question and is not mentioned visibleMapRect, because he is more than 5 years old.

Any help is greatly appreciated.

+4
source share
2 answers

, : 4 .

  • lat > bottomleft.lat lat < topright.lat
  • > bottomleft.long < topright.long

iOS ,

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

,

+4

EDIT 2 , mapview , NW, NE, SW SE. , , . , , , mapview.

, , , quadtrees .

Thoughtbot , gifs, , :

enter image description here enter image description here

, , . , node, . , node , , . , , .

: iOS

0

Source: https://habr.com/ru/post/1627600/


All Articles