Programming Learner
Programming Learner

Reputation: 4411

Get all locations within certain radius from SQLite Database in XCode

I have a SQLite Database which contains latitude and longitude of several locations.

I want to find the latitude and longitude of all locations which fall within 15km radius from my current location from my SQLite Database.

What will be my SQLite Query for this?

Upvotes: 1

Views: 1025

Answers (3)

Mohamed Salah
Mohamed Salah

Reputation: 1205

For anyone still looking for an answer:

You can get nearby locations with this method adapted from @breceivemail's answer [JAVA]

Here is the translated version in Swift:

private func calculateDerivedPosition(point: CGPoint, range: Double, bearing: Double)-> CGPoint {
    let EarthRadius: Double = 6371000

    let latA = Double(point.x.degreesToRadians)
    let lonA = Double(point.y.degreesToRadians)
    let angularDistance = range / EarthRadius
    let trueCourse = bearing.degreesToRadians

    var lat = asin(sin(latA) * cos(angularDistance) +
                   cos(latA) * sin(angularDistance) *
                   cos(trueCourse))

    let dlon = atan2(sin(trueCourse) * sin(angularDistance) * cos(latA),
                     cos(angularDistance) - sin(latA) * sin(lat))

    var lon = ((lonA + dlon + Double.pi).truncatingRemainder(dividingBy: (Double.pi * 2))) - Double.pi

    lat = lat.radiansToDegrees
    lon = lon.radiansToDegrees

    let newPoint = CGPoint(x: lat, y: lon)

    return newPoint
} 

radiansToDegree Extension

And You can use it like this with FMDB (or any Sqlite lib):

    let center = CGPoint(x: currentLocation.latitude, y: currentLocation.longitude)
    let mult: Double = 1 // mult = 1.1 is more reliable
    let radius: Double = 5000 //in meters

    let point1 = calculateDerivedPosition(point: center, range: radius * mult, bearing: 0)
    let point2 = calculateDerivedPosition(point: center, range: radius * mult, bearing: 90)
    let point3 = calculateDerivedPosition(point: center, range: radius * mult, bearing: 180)
    let point4 = calculateDerivedPosition(point: center, range: radius * mult, bearing: 270)

    let result = try!db.executeQuery("SELECT * FROM items WHERE lat > ? AND lat < ? AND lng < ? AND lng > ?" , values: [point3.x, point1.x, point2.y, point4.y])

Note: The results aren't sorted.

Upvotes: 2

Programming Learner
Programming Learner

Reputation: 4411

To arrange them in Ascending order, store all the distcnace in a dictionary with key. like,

 distanceDic = [[NSMutableDictionary alloc]initWithObjectsAndKeys:[NSNumber numberWithFloat:distanceVal],@"distance",nil];

 [distanceArray addObject:distanceDic];

Here, distanceDic is my Dictionary and distaceVal is string containing distance. distanceArray is NSMutableArray Containing Distance. Now Sort the distanceDic like,

NSArray *array = [NSArray arrayWithArray:[distanceArray mutableCopy]]; // Copying NSMutableArray to NSArray.
NSSortDescriptor *sortDescriptor;

sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"distance" ascending:YES];

NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];

array = [array sortedArrayUsingDescriptors:sortDescriptors];

NSLog(@"#The Final Distance Array After Sorting :%@",array);

This will give you resulting Distance Array sorted in Ascending order.

Upvotes: 1

Pradeep Singh
Pradeep Singh

Reputation: 762

One degree difference of latitude is equal to 111 km distance, and One degree difference of longitude is equal to 94 km. So Now check for all the latitude which are at (+/-)(15/111) difference and longitudes which are (+/-)(15/94) difference from your current location. Important link for reference.

Upvotes: 2

Related Questions