Saturday, November 7, 2026
HomeiOS Developmentios - Swift CoreLocation .distance technique returning large/incorrect distances?

ios – Swift CoreLocation .distance technique returning large/incorrect distances?

[ad_1]

I’m measuring the gap between my mapView’s (Mapbox) heart coordinates and annotations on the map to take away annotations if their distance is bigger than 1 mile from the mapView’s heart coordinates.

That is how I’m eradicating the annotations (when map zoomLevel > 15.0) from the map in my case in order that when the map goes someplace else it removes annotations out of view, hundreds annotations information from API, and provides extra annotations relying on the place the mapView’s heart is.

I learn that CoreLocations .distance returns the gap in meters however I get distances which can be MASSIVE: Eradicating annotation, distance is 2719478.2671192945 when the annotation is actually inside 50 meters of the mapView’s heart coordinates???

I made positive that the coordinates are assigned the correct lat/lengthy primarily based on which place they’re saved within the array so I’m not positive what the issue is..??

Apples documentation says that distance is in meters however this distance is manner too massive, ought to I be dividing the gap to transform it? I’m not positive what unit of measurement is being returned??

THANK YOU


            if let annotations = management.mapView.annotations {
                annotations.forEach { annotation in
                    guard let annotation = annotation as? CustomAnnotation else {
                        return
                    }
                 
                    print("spot Lengthy =", annotation.spot.location.coordinates[0])
                       // -> spot Lengthy = -73.92630747771368

                    print("spot Lat =", annotation.spot.location.coordinates[1])
                       // -> spot Lat = 40.704219996932856

                    let annotationLocation = CLLocation(
                      latitude: annotation.spot.location.coordinates[1], 
                      longitude: annotation.spot.location.coordinates[0]
                    )
                    
                    let distance = 
                       management.mapVM.mapCenterCoordinate.location.distance(from: annotationLocation) 

                    print("distance",distance)
                       // -> distance 319529.6680520984
                       // different distances vary from 7000 - 2719478 ????

                    if distance > 1609.34 {
                        // removes annotations that ought to keep!
                        management.mapView.removeAnnotation(annotation)
               
                    }
                }
            }

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments