2016-03-03 51 views
0

我有一個基於2dsphere(球形),返回距離的geonear查詢。我不完全確定返回的單位度量。它是米還是弧度?我應該使用哪種「乘數」來計算英里距離?我發現這個問題的答案都不一樣。MongoDB geonear和距離轉換

回答

0

返回的結果中,通常以弧度

查詢:

db.runCommand({ 
    geoNear: "restaurants" , 
    near: [ -73.93414657, 40.82302903 ], 
    spherical: true, 

}) 

會給這樣的結果:

{ 
"dis" : 0.0001360968348384049, 
"obj" : { 
     "_id" : ObjectId("55cba2476c522cafdb054fee"), 
     "location" : { 
       "coordinates" : [ 
         -73.94387689999999, 
         40.8255961 
       ], 
       "type" : "Point" 
     }, 
     "name" : "Texas Star Snack Bar" 
} 
} 

所以在這種情況下,距離爲半徑,我們需要:

1. multiply it by 3963.2 to get distance in miles (in example we have 0.539 M) 

2. multiply it by 6371 to get distance in kilometers (in example we have 0.867 km) 

,如果你使用的是3.2,並與minDistance纔會和maxDistance查詢(在米的定義) - 提供距離是米

db.runCommand(
    { 
    geoNear: "restaurants", 
    near: { type: "Point", coordinates: [ -73.9667, 40.78 ] }, 
    spherical: true, 

    minDistance: 3000, 
    maxDistance: 7000 
    } 
) 

和示例結果:

{ 
"dis" : 338.44550608396395, 
"obj" : { 
     "_id" : ObjectId("55cba2476c522cafdb0561eb"), 
     "location" : { 
       "coordinates" : [ 
         -73.9653551, 
         40.7828647 
       ], 
       "type" : "Point" 
     }, 
     "name" : "(Public Fare) 81St Street And Central Park West (Delacorte Theatre)" 
}} 

有大約一個很好的教程here與數據集 - 你不能用於測試。