1

I have a coordinate, assume any latitude and longitude values.

I have a domain setup on SimpleDB that has many items (simple strings) with attributes of 'Latitude' and 'Longitude'. Now what I want to do is query SimpleDB and see if the current location coordinates are 'x' meters apart from SimpleDB's items' coordinates. 'x' should be 10.

My app uploads an item to SimpleDB with an attribute that contains the latitude and longitude. I detect the users location, get the coordinates and I want to use a select expression to see if the coordinates are 'x' meters apart. So is their a better approach to doing this? Or is this is the best way, if so, how can I do it?

Here is an example of what the select expression may look like, I just have no idea how to use it in this case and what the '%@' values would be filled in by. This whole format could be off, its just my idea.

select * from test-app-simpledb where Latitude >= '%@' AND Latitude <= '%@' AND Longitude >= '%@' AND Longitude <= '%@'

So "test-app-simpledb" is my SimpleDB domain name, Latitude and Longitude are the attributes I compare to the coordinates. They are all converted to string.

So, how can I do location comparative select expressions. Querying if the item's coordinate (latitude and longitude attribute) are 'x' (in this case 10) meters apart.

Any way to do this? Thanks!

MCKapur
  • 9,127
  • 9
  • 58
  • 101
  • @Rob Thanks, in that answer, what would the 'angle' parameter be? Also what would the 'bearingDegrees' mean? – MCKapur Jan 03 '13 at 14:15
  • @Rob So I iterate bearingDegrees from 0 to pi*2 – MCKapur Jan 03 '13 at 14:41
  • @Rob is the 'bearingDegrees' the angle you are reffering to? The one I iterate from 0 to M_pi * 2.0? – MCKapur Jan 03 '13 at 15:09
  • @Rob Because we are iterating and we include 0, would the method every return the original coordinate that gets passed? – MCKapur Jan 03 '13 at 16:51
  • @Rob It is Amazon SimpleDB, I use select expressions – MCKapur Jan 04 '13 at 03:17
  • @Rob I have updated my question to be more centered around SimpleDB and have a SimpleDB tag. I think distanceFromLocation will be a more efficient way to do it non-server side – MCKapur Jan 04 '13 at 03:37

1 Answers1

4

In the revised question, it has become clear that the actual question is how to conduct a query against a Amazon SimpleDB database to see if locations in the database are within a certain radius of a location provided by an iOS app.

As you correctly identify, you really want to do this server-side, rather than client-side, if possible. And the particular solutions will be highly dependent upon the particular database technology (SimpleDB in this case).

This question is touched upon in Spatial queries on AWS SimpleDB. I would suggest checking that out for more information.

As a proxy for a proper distance algorithm, you could translate the distance in meters into a ranges of latitudes and longitudes. Thus, you could, in iOS, calculate a minimum and maximum for both latitude and longitude and then pass those along in the WHERE clause to your remote database. Then the server could filter results based upon those criteria. That admittedly gives you a square-shaped region (rather than a circular region that you get by calculating distances properly), but it makes it really easy to quickly limit the result set with no special geolocation logic required on the server. To do this, you could define a region with MKCoordinateRegionMakeWithDistance, and then grab its span.

MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(originalCoordinate, 200, 200);
MKCoordinateSpan span = region.span;

That gives you span.latitudeDelta and span.longitudeDelta which you can center around a given location's latitude and longitude to define a square shaped region around a location. To demonstrate that in action, here the center pin is my location at Times Square, and the shaded area is a region (constructed using the above span which is 200m wide and tall), which covers longitudes between 100m east and 100m west of my current location, as well as latitudes 100m north and 100m south of my current location.

using span to define a square region

This is a way to employ MapKit.framework functions to greatly streamline your SQL queries for remote databases to filter locations based upon geographic distance. If the square region is problematic, you could also further filter the results once they're downloaded to the iOS app using the CLLocation instance method, distanceFromLocation to determine the actual distance. But by limiting the longitudes and latitudes of locations retrieved by the server, you dramatically streamline the server retrieval process while not encumbering it with too much geographic location calculations.

But you really want to have SimpleDB do the full, proper distance calculation, I'll have to leave that to others.


Below, is my original answer. In the original question, I misinterpreted it as being "how do I construct a list of coordinates going in a circle around a particular location?" As made clear by the revised question, that was not the issue at all, but I'll keep my old answer here for historical reference.

Original answer:

If you used Calculate new coordinate x meters and y degree away from one coordinate, the implementation might look like:

NSInteger numberOfPoints = 10;

for (double bearing = 0.0; bearing < 360.0; bearing += (360.0 / numberOfPoints))
{
    CLLocationCoordinate2D coordinate = [self coordinateFromCoord:originalCoordinate
                                                     atDistanceKm:distanceKm 
                                                 atBearingDegrees:bearing];
    // do whatever you want with this coordinate
}

It seems to work fine. For example, I had an app use this routine to drop 10 pins 100m from me in Times Square:

times square

Community
  • 1
  • 1
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • Starting fresh! In that answer for the question you gave me, he provides a select expression comparing with minLat, maxLat, maxLon and minLon, this would work, but there needs to be a way to calculate the minimum latitude, minimum longitude, maximum latitude and maximum longitude 10 meters from a single latitude/longitude. Is there? – MCKapur Jan 04 '13 at 04:07
  • Is there a formula for this? You told me latitude is easy but longitude isn't – MCKapur Jan 04 '13 at 04:24
  • Thanks, so latitudeDelta and longitudeDelta would be what? The minimum latitude or longitude or the maximum? And how can I use it? – MCKapur Jan 04 '13 at 04:38
  • @RohanKapur Those are delta values that you'd add to your desired location's actual latitude and longitude. So your `minLongitude` might be `originalCoordinate.longitude - span.longitudeDelta` and the `maxLongitude` would be `originalCoordinate.longitude + span.longitudeDelta`. It probably doesn't work on the international date line, but otherwise you're good. You'd do the same with the latitudes (and again, maybe not perfect on equator or the poles). In my revised question, I have a pin on Times Square, and then pins around the four corners adding and subtracting the appropriate deltas. – Rob Jan 04 '13 at 04:46
  • Haha thanks, this is awesome, such an awesome answer, deserves a bounty award tomorow! the fact that it doesn't work on the international date line is somewhat concerning, but great job! – MCKapur Jan 04 '13 at 04:53
  • You could make it work on the international date line by just checking for greater than 180 or less than -180 and add the additional `WHERE` clause for that. You just need to decide whether it's worth the effort. But it's easy. – Rob Jan 04 '13 at 04:56
  • Ok thanks.... however ON or very near to the international date line might not be worth the effort; there probably wouldnt be any users there – MCKapur Jan 04 '13 at 04:57