Quentin Rth
Quentin Rth

Reputation: 305

MapBox get visibleCoordinateBounds in a CGRect

I'm working on an iOS app with a MapBox map.

I'm displaying MGLAnnotations but I only want to create the annotations that can be seen on the screen at a current moment.

Problem is : I have a UIView on the bottom of my screen so the bounds I get with mapView.visibleCoordinateBounds function is not really accurate because I can't see the bottom of my map.

I can't just resize my map because the bottom view I talked about doesn't cover the entire map, we still see parts of the map behind the bottom view.

So my question is, how can I get the visibleCoordinateBounds for a CGRect over my MapView ?

My current solution works only for the entire map view bounds and not the bounds of the CGRect area I want to display the annotations :

if ((mapView.visibleCoordinateBounds.sw).latitude...(mapView.visibleCoordinateBounds.ne).latitude ~= latX && (mapView.visibleCoordinateBounds.sw).longitude...(mapView.visibleCoordinateBounds.ne).longitude ~= lngY) {
    print("VISIBLE")
}

Thanks

Upvotes: 1

Views: 844

Answers (1)

Quentin Rth
Quentin Rth

Reputation: 305

I finally found a way to do it.

I just calculate the percentage of my mapview hidden by the bottom view and I minus this number to the south latitude :

var valueToRemoveBottom = (mapView.visibleCoordinateBounds.ne).latitude - (mapView.visibleCoordinateBounds.sw).latitude
let percentageUsedByBottom = (bottomView.frame.height * 100 / mapView.frame.height) / 100
valueToRemoveBottom = valueToRemoveBottom * Double(percentageUsedByBottom)

if (((mapView.visibleCoordinateBounds.sw).latitude + valueToRemoveBottom)...((mapView.visibleCoordinateBounds.ne).latitude) ~= latX && (mapView.visibleCoordinateBounds.sw).longitude...(mapView.visibleCoordinateBounds.ne).longitude ~= lngY) {
// visible
}

Upvotes: 1

Related Questions