2009-07-07 99 views
1

我在标记的服务器端实施一些基于边界的聚类,以在我的谷歌地图上显示。我正在做的是,我有一个函数,每次映射移动或平移或缩放时都会调用该函数,该函数接受映射的边界并进行ajax调用,然后服务器端脚本运行简单的SQL查询来检索标记并将它们聚类。到目前为止,集群部分工作得很好,但有时getBounds似乎并没有发送我感觉到的正确界限。谷歌地图 - 平移和缩放区域 - 当我放大或缩小时,标记不会出现 - 帮助!

就像我可以在地图上稍微拖动一边,以前有标记的地区突然没有标记,地图也是空白的。我检查了SQL查询,并从查询本身显示了一个蛮横不同的界限比预期。

在以下地区看一看: Orginal WitH Markers http://i31.tinypic.com/xds4t0.jpg No markers http://i31.tinypic.com/2r22ez4.jpg

第一个显示所有标记。

然而,后面的一个刚刚移动了一些顶部和左侧,但一半的区域是与前一张图片相同,但根本没有标记。我已经将问题隔离到了地图的getBounds函数中。

这是我的JavaScript代码,得到边界和进行呼叫:

var bounds = map.getBounds(); 
var southWest = bounds.getSouthWest(); 

var northEast = bounds.getNorthEast(); 
var data = {ne:northEast.toUrlValue(), sw:southWest.toUrlValue(), zoom:map.getZoom()}; 
//something getting messed up in the code above this point 

$.ajax({ 
    type: "POST", 
    url: 'maps/get-markers', 
    data: {object:$.toJSON(data)}, 
    dataType: 'json', 

    success: function(response) { 
     //code to add markers 
    } 
}); 

在我的服务器端代码,这是用来获取基于坐标的项目的PHP:

$data = Zend_Json::decode(stripslashes($this->_request->getParam('object'))); 

//retrieve the variables from the GET vars 
list($nelat, $nelng) = explode(',', $data['ne']); 
list($swlat, $swlng) = explode(',',$data['sw']); 

//clean the data 
$nelng = (float)$nelng; 
$swlng = (float)$swlng; 
$nelat = (float)$nelat; 
$swlat = (float)$swlat; 

$ubound = $swlng > $nelng ? $swlng : $nelng; 
$lbound = $swlng > $nelng ? $nelng : $swlng; 

$sql = 'SELECT `a`.* FROM `locations` AS `a` WHERE (a.`longitude` > $lbound) AND (a.`longitude` < $ubound) AND'; 

$ubound = $swlat > $nelat ? $swlat : $nelat; 
$lbound = $swlat > $nelat ? $nelat : $swlat; 


$sql .= ' (a.`latitude` >= $lbound) AND (a.`latitude` <= $ubound)'; 

我怀疑它的JavaScript中的getbounds函数,但需要尽快解决它。任何想法请:(

+0

得到它固定的 - 我所有的20个000标记已经搞砸了位置:)感谢所有的答复! – Ali 2010-02-20 12:26:02

回答

1

我有工作正是你所描述你以同样的方式在页面下面是如何得到的界限:。

var bounds = map.getBounds(); 
var sw = bounds.getSouthWest(); 
var ne = bounds.getNorthEast(); 
var s = sw.lat(); 
var w = sw.lng(); 
var n = ne.lat(); 
var e = ne.lng(); 

我那么每个值发送到服务器我以前用这样的查询的范围内检查点:

WHERE (lat BETWEEN $sBound AND $nBound) AND (lng BETWEEN $wBound AND $eBound) 

不过,我最近发现,当边界区域包括国际日期变更线查询失败这看起来不像是你的问题。 (它可能发生在解析边界),b这绝对是你应该考虑的事情。以下是我固定它:

if ($wBound > $eBound) { // if bounds includes the intl. date line 
    $sql .= "WHERE (lat BETWEEN $sBound AND $nBound) AND ((lng BETWEEN -180 AND $eBound) OR (lng BETWEEN $w AND 180))"; 
} else { 
    $sql .= "WHERE (lat BETWEEN $sBound AND $nBound) AND (lng BETWEEN $wBound AND $eBound)"; 
} 
1

如果这是从字面上代码复制:

$sql .= ' (a.`latitude` >= $lbound) AND (a.`latitude` <= $ubound)'; 

那么问题可能仅仅是你使用单引号,而不是双对PHP的文字。因此,$ lbound和$ ubound插值不会发生,并且您正在使用无效的SQL查询。

尝试:

$sql .= " (a.`latitude` >= $lbound) AND (a.`latitude` <= $ubound)";