2016-03-03 129 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与数据集 - 你不能用于测试。