问题
I want to create a CouchDB database with some POI's. Is there a way/query to get poi's within a certain radius (lets say 50 meters) from a given lat/long position?
I saw an extension https://github.com/couchbase/geocouch , but this means I have to recompile CouchDB, but at this moment I don't have admin access to do that.
回答1:
I have an alternative proposal for your use case. The proposal is to use geohashes.
You can store the position in the document as a geohash. The lenght of the geohash will depends on the precission you would like to store.
Let consider that you stored the position with around 150 meters precession. In this case, you'll have a geohash of 7 characters like 'gbsukp7'. Check this for testing.
Then your map function can be redefined in this way:
function (doc) {
emit([doc.geohash.substr(0,4), /* 39.1km × 19.5km bounding box */
doc.geohash.substr(0,5), /* 4.89km × 4.89km bounding box */
doc.geohash.substr(0,6), /* 1.22km × 0.61km bounding box */
doc.geohash /* 153m × 153m bounding box */
],null)
}
With this approach you can have a simple mechanism to get the documents located in the same bounding box than the point of refrence.
It is not perfect but could be an option
回答2:
Solved it and made a map function:
function (doc) {
// Leidse plein
latitude = 52.3648111;
longitude = 4.8810906;
distance = Math.acos(
Math.sin(doc.latitude * Math.PI / 180) *
Math.sin(latitude * Math.PI / 180)
+
Math.cos(doc.latitude * Math.PI / 180) *
Math.cos(latitude * Math.PI / 180) *
Math.cos((doc.longitude - longitude) * Math.PI / 180)) * 6371;
// all poi's within 5km radius
if(distance <= 5 ) {
emit([doc.title,doc.latitude,doc.longitude], distance);
}
}
来源:https://stackoverflow.com/questions/49705685/query-couchdb-to-get-poi-within-a-certain-radius