2013-04-04 81 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(); 
}