Question

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!

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top