2013-04-04 139 views
1

我是一種新的mongodb。Mongodb geoWithin谷歌地圖邊界

我想使用MongoDB的地理空間功能來查詢從谷歌地圖獲得的邊界位置。這就像內部框(swLng,swLat,neLng,neLat)。但是當地圖縮小時,由於neLng/neLat小於swLng/swLat,它無法給我正確的結果。看起來,這是使用x> swLng和x來計算的,並且y> swLat和y < neLat,而不是考慮地圖投影。

所以我可以用withinBox實現查詢,或我必須調整座標,或者我應該接近使用?

+0

我發現這個職位[鏈接](http://stackoverflow.com/questions/1090690/google-maps-panning-and-zooming-into-areas-markers-not-appear-when-i-zoom?rq = 1)提到了國際日期行,並給出了sql中的解決方案。 – K1Zhang 2013-04-04 04:49:15

回答

0

正如我上面的評論,在東北和西南點跨越國際日期變更線,查詢將作爲右上方的y大於左下角的Y小失敗。

如果你正在使用PHP和MongoDB的學說,你需要做兩個查詢和下面一樣在客戶端合併結果:

if ($x1 > $x2) { 
    $qb->field('coordinates')->withinBox(-180, $y1, $x2, $y2); 
    $result1 = $qb->getQuery()->execute(); 
    $qb->field('coordinates')->withinBox($x1, $y1, 180, $y2); 
    $result2 = $qb->getQuery()->execute(); 
    $result = $result1->toArray() + $result2->toArray(); 
} else { 
    $qb->field('coordinates')->withinBox($x1, $y1, $x2, $y2); 
    $result = $qb->getQuery()->execute(); 
    $result = $result->toArray(); 
}