android 已知有N個坐標(經緯度),現在求坐標(x,y(經緯度))在半徑不超過X米並且在N個坐標(經緯度)中有那些坐標(經緯度)點???
我問這個問題有2個點想知道
2個坐標(經緯度)怎麼求距離
難道每次求,都要遍歷N個坐標算一次距離嗎?
Location
對象帶和距離相關的方法,求兩個坐標距離: public static void distanceBetween (double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results)
Parameters
startLatitude the starting latitude
startLongitude the starting longitude
endLatitude the ending latitude
endLongitude the ending longitude
results an array of floats to hold the results
詳細解釋見這裡: distanceBetween(double, double, double, double, float[])
如果你用Mongodb,獲取最近的坐標很簡單,Mongodb自帶geoNearCommand
,可以參照這裡:GeospatialIndexing-geoNearCommand
> db.runCommand( { geoNear : "places" , near : [50,50], num : 10 } );
> db.runCommand({geoNear:"asdf", near:[50,50]})
{
"ns" : "test.places",
"near" : "1100110000001111110000001111110000001111110000001111",
"results" : [
{
"dis" : 69.29646421910687,
"obj" : {
"_id" : ObjectId("4b8bd6b93b83c574d8760280"),
"y" : [
1,
1
],
"category" : "Coffee"
}
},
{
"dis" : 69.29646421910687,
"obj" : {
"_id" : ObjectId("4b8bd6b03b83c574d876027f"),
"y" : [
1,
1
]
}
}
],
"stats" : {
"time" : 0,
"btreelocs" : 1,
"btreelocs" : 1,
"nscanned" : 2,
"nscanned" : 2,
"objectsLoaded" : 2,
"objectsLoaded" : 2,
"avgDistance" : 69.29646421910687
},
"ok" : 1
}
PS. 這裡有很多計算GEO距離相關的公式:http://www.movable-type.co.uk/scripts/latlong.html