Because you can only use bounding-box queries with Couchbase spatial views, you will have to split your distance query into two steps:
- Query Couchbase for coordinates that fall within a bounding box that matches your radius.
- Filter out coordinates returned by #1 that are further than the radius you specified.
For the first step, you'll need to write a spatial view as follows:
function(doc, meta)
{
if (doc.location)
emit(doc.location, [meta.id, doc.location]);
}
Note: this is the Couchbase 3.0 version of the view, in Couchbase 4 you don't need to emit the meta.id and doc.location in the value anymore.
Now, given a starting point (lat,lon) and radius r, you need to calculate a bounding box [lat1,lon1, lat2,lon2] to query the view for a list of documents whose coordinates potentially fall within the radius you want. The bounding box query in Couchbase specifies the bottom-left and top-right coordinates.
Next, in your application, iterate over all the results and check whether they really do fall within R distance of your starting point.
Depending on how much accuracy you need, you can either assume the Earth is flat and just do the calculations on a 2D plane, which will be inaccurate but not terribly so for a distance of 1 mile. Or alternatively, use the actually accurate formulae to calculate everything, as described in this excellent article: http://janmatuschek.de/LatitudeLongitudeBoundingCoordinates
Or better yet, you can use a geolocation library for the language of your choice to calculate the bounding box and the distances. Here's one for C# and one for Java.