Google地图-平移和放大区域-放大或平移时不会出现标记 [英] Google Maps - Panning and Zooming into areas - markers not appearing when I zoom in or pan

查看:129
本文介绍了Google地图-平移和放大区域-放大或平移时不会出现标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在标记的服务器端实现一些基于边界的聚类,以显示在我的Google地图上。我正在做的是,我有一个函数,它在每次地图移动,平移或缩放时都会被调用,该函数获取地图的边界并进行ajax调用,而服务器端脚本又运行一个简单的sql查询来检索标记并将它们聚类。到目前为止,聚类部分运行良好,但是有时getBounds似乎并没有发送我感觉到的正确边界。

I'm implementing some boundary based clustering on the server end of markers to display on my google maps. What I'm doing is that I have a function that is called everytime the map is moves or panned or zoomed which takes the bound of the map and makes an ajax call which in turn the server side script runs a simple sql query to retrieve the markers and clusters them. SO far teh clustering part is working nicely however sometimes the getBounds doesn't seem to be sending the correct boundaries I feel.

就像我可能在地图上平移地图一样侧面和曾经带有标记的区域现在突然没有标记,地图空白。我检查了sql查询,并从查询本身显示了超出预期范围的界限。

Like I may pan a map a bit on the side and regions which used to have markers now all of a sudden don't have markers and the map is blank. I checked the sql query and from the query itself it shows an outrageously different bounds than what would be expected.

看看下面的区域:
< img src = https://i.stack.imgur.com/qq6jM.jpg alt =原始WitH标记>

Take a look at the regions below:

第一个显示所有标记。

一个但是,后面的内容只是在顶部和左侧移动了一点,但一半的区域与上一张图片相同,但是根本没有标记。我已将问题与地图的getBounds函数隔离开了。

The one following however has just moved a bit to the top and left but half the region is the same as in the previous picture however there are no markers at all. I have isolated the issue to be with the getBounds function of the maps.

这是我的javascript代码,可获取范围并进行调用:

This is my javascript code which gets the bounds and makes the call:

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:

On my server side code this is the php used to get the items based upon the coordinates:

$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函数,但需要尽快修复。任何想法请:(

I'm suspecting its the getbounds function in the javascript but need to fix it asap. Any ideas please :(

推荐答案

我有一个页面的工作方式与您描述自己的方式完全相同。这就是我的方法边界:

I have a page that works exactly the same way that you have described yours. Here's how I get the bounds:

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();

然后将每个值发送到我以前用以下查询来检查范围内的点:

I then send each value to the server. I previously checked for points within the bounds with a query like this:

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

但是,我最近发现当边界区域包括国际日期线,这看起来不像您遇到的问题(可能是在解析边界时发生),但这绝对是您应该考虑的问题。这是我解决的方法:

However, I recently discovered that this query fails when the bounds area includes the international date line. This doesn't look like the problem that you're having (it's probably happening in parsing the bounds), but it's definitely something you should consider. Here's how I fixed it:

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)";
}

这篇关于Google地图-平移和放大区域-放大或平移时不会出现标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆