I need to compute MAX and MIN Latitude and Longitude values from a location with certain distance.
I have thousands of locations stored in CoreData, and I want to show only the ones within 5km from users location.
How can I approach this problem?
Latitude and longitude are a pair of numbers (coordinates) used to describe a position on the plane of a geographic coordinate system. The numbers are in decimal degrees format and range from -90 to 90 for latitude and -180 to 180 for longitude.
For this divide the values of longitude and latitude of both the points by 180/pi. The value of pi is 22/7. The value of 180/pi is approximately 57.29577951. If we want to calculate the distance between two places in miles, use the value 3, 963, which is the radius of Earth.
One degree of latitude equals approximately 364,000 feet (69 miles), one minute equals 6,068 feet (1.15 miles), and one-second equals 101 feet. One-degree of longitude equals 288,200 feet (54.6 miles), one minute equals 4,800 feet (0.91 mile), and one second equals 80 feet.
You can use distanceFromLocation to find the distance between two coordinates. Code Snippets: CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:lat1 longitude:lng1]; CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:lat2 longitude:lng2]; CLLocationDistance distance = [loc1 distanceFromLocation:loc2];
Here's a possible solution:
macros to convert Degrees to Radians
#define deg2rad(degrees) ((degrees) / 180.0 M_PI)
macros to hold my searching distance
#define searchDistance 5.00 //float value in KM
set the minimum and maximum Latitude, Longitude values
float minLat = userLocation.coordinate.latitude - (searchDistance / 69);
float maxLat = userLocation.coordinate.latitude + (searchDistance / 69);
float minLon = userLocation.coordinate.latitude - searchDistance / fabs(cos(deg2rad(userLocation.coordinate.latitude))*69);
float maxLon = userLocation.coordinate.longitude + searchDistance / fabs(cos(deg2rad(userLocation.coordinate.latitude))*69);
create predicate as follows
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"latitude <= %f AND latitude >= %f AND longitude <= %f AND longitude >= %f", maxLat, minLat, maxLon, minLon];
This will create a square around userLocation
and check if a given location falls into its coordinates.
Update: Swift 2.* implementation
First create a function to compute degrees to radians
func deg2rad(degrees:Double) -> Double{
return degrees * M_PI / 180
}
Compute and create minimum and maximum Latitude
and Longitude
values
let searchDistance:Double = 5.00 //float value in KM
let minLat = userLocation.coordinate.latitude - (searchDistance / 69)
let maxLat = userLocation.coordinate.latitude + (searchDistance / 69)
let minLon = userLocation.coordinate.longitude - searchDistance / fabs(cos(deg2rad(userLocation.coordinate.latitude))*69)
let maxLon = userLocation.coordinate.longitude + searchDistance / fabs(cos(deg2rad(userLocation.coordinate.latitude))*69)
Last create NSPredicate
to query CoreData
for locations. In my case I am querying for values latitude
and longitude
but you should change this to match your CoreData
object
let predicate = NSPredicate(format: "latitude <= \(maxLat) AND latitude >= \(minLat) AND longitude <= \(maxLon) AND longitude >= \(minLon)")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With