3

Hey. I have a maps application that uses google maps. I get the bounds off the map, and then I do some clustering markers on that grounds, but to be able to have the clusters stay at the same place, I'd like to know how to make the boundaries that I pass snap into the tilegrid that google uses. The quadtree algorithm they use for their map essentially what I'm asking for is

how do I get the bounds for the tiles that the viewport is in. I've tried to illustrate it :) google tiles viewport cluster

If I do the cluster calc on the tile bounds and not the viewport, when I split it up in a grid, the clusters will stay in the same place, because the grid will be absolute at each zoom level.

Also this allows for better query caching, as the queries will be a lot similar when the bounds have to "snap in" to the tiles, AND the user will be able to pan with markers in proximity being displayed already.

UPDATE

I'm starting over...

I've got this

    function TileMyBounds($sx, $sy, $nx, $ny, $zoom)
{

        function TileMyBounds($sx, $sy, $nx, $ny, $zoom)
{
    list($nmx,$nmy) = $this->LatLonToMeters($ny/1000000, $nx/1000000);
    list($ntx, $nty) = $this->MetersToTile($nmx, $nmy, $zoom);
    $nbounds = $this->TileLatLonBounds($ntx, $nty, $zoom);
    list($smx,$smy) = $this->LatLonToMeters($sy/1000000, $sx/1000000);
    list($stx, $sty) = $this->MetersToTile($smx, $smy, $zoom);
    $sbounds = $this->TileLatLonBounds($stx, $sty, $zoom);

    $step = ($sbounds[3]-$sbounds[1])*1000000;

    return array($sbounds[0]*1000000, $sbounds[1]*1000000, $nbounds[2]*1000000, $nbounds[3]*1000000, $step);
}

and the function where I use it looks like this:

function clusterGrid($zoom,$nelt,$nelg,$swlt,$swlg)
    {
    $singlemarkers = array();
    $clusters = array();

list($swlg, $swlt, $nelg, $nelt, $step) = $this->TileMyBounds($swlg, $swlt, $nelg, $nelt, $zoom);
$calcbounds = $this->TileMyBounds($swlg, $swlt, $nelg, $nelt, $zoom);
$queryconcat = "";
$length_lng = ceil(($nelg-$swlg)/$step);
$length_lat = ceil(($nelt-$swlt)/$step);
$orgnelg = $nelg;
$orgswlt = $swlt;

for($i=0;$i < $length_lng + 1; $i++) {
    $nelg -= $step;
    $temp_swlt = $swlt;

    for($j=0; $j < $length_lat + 1; $j++) {

        $temp_swlt += $step;

        if($nelg > $orgnelg) continue;
        if($temp_swlt > $nelt) continue;
        if($nelg < $swlg) continue;
        if($temp_swlt < $orgswlt) continue;

        $q = $this->db->select('
            COUNT(*) AS CO,
            (MAX(lat)+MIN(lat))/2 AS lat,
            (MAX(lng)+MIN(lng))/2 AS lng')
        ->where('`lat` BETWEEN '.$temp_swlt.' AND '.($temp_swlt+$step).' AND 
            `lng` BETWEEN '.($nelg-$step).' AND '.$nelg)
        ->get('markers');
        $queryconcat += $this->db->last_query(); 
        $result = $q->row_array();

        if($result['CO'] == 0) {
            continue;
        }
            $clusters[] = array('lat' => ($result['lat']), 'lng' => ($result['lng']), 'size' => $result['CO']);
        }
    }
return array('singlemarkers' => '', 'clustermarkers' => $clusters, 'bounds' => $calcbounds, 'lengths' => array($length_lng, $length_lat));  
}

UPDATE!!!! 12/03/2011 - Almost there The tiles are somewhat precise, yet not entirely, so when panning around, the clusters can "move around" a little. Due to the fact that the $step = ($sbounds[3]-$sbounds[1])*1000000; calculation is not always the same at each zoom level, as I would've expected because I would think that a tile would have the same width and length in lat and lon as any other tile at the same zoom level.

Jakob
  • 4,784
  • 8
  • 53
  • 79
  • Hi @Jakob, do you know on how to implement that in Java? I am developing an Android application that uses a static image map then mark the current location of the user. In order to get the pixel coordinate of the current viewport, I need to know the whole size of the google map at a specified zoom level. – eros Aug 19 '11 at 07:34

1 Answers1

1

But something's not quite right. Can anyone tell why I'm not getting the viewport coordinates when I pass in swlg,swlat,nelg,nelat and the zoom level

You want to solve the space-filling-curve equation first with all 4 bounds coordinates.

list($lng, $lat) = array ($row['lng'], $row['lat']);
list($mx, $my) = $mercator->LatLonToMeters($lat, $lng);
list($tx, $ty) = $mercator->MetersToTile($mx, $my, MAXZOOM);
list($tx, $ty) = array ($tx, ((1 << MAXZOOM) - 1) - $ty );
list($minx, $miny) = $this->PixelsToMeters( $tx*$this->tileSize, $ty*$this->tileSize, $zoom );
list($maxx, $maxy) = $this->PixelsToMeters( ($tx+1)*$this->tileSize, ($ty+1)*$this->tileSize, $zoom );
return array($minx, $miny, $maxx, $maxy);

or

list($lng, $lat) = array ($row['lng'], $row['lat']);
list($mx, $my) = $mercator->LatLonToMeters($lat, $lng);
list($tx, $ty) = $mercator->MetersToTile($mx, $my, MAXZOOM);
list($tx, $ty) = array ($tx, ((1 << MAXZOOM) - 1) - $ty );
$bounds = $this->TileBounds($tx, $ty, $zoom);
list($minLat, $minLon) = $this->MetersToLatLon($bounds[0], $bounds[1]);
list($maxLat, $maxLon) = $this->MetersToLatLon($bounds[2], $bounds[3]);
return array($minLat, $minLon, $maxLat, $maxLon);

EDIT: Problem is solved. OP answered in private Mail:

Ian Mackinnon
  • 13,381
  • 13
  • 51
  • 67
Micromega
  • 12,486
  • 7
  • 35
  • 72
  • @epitah - have you got any clues at to why the tilesize seems to differ on one zoom level, for there to be any logic in tiles shouldn't this line: $sbounds[2]-$sbounds[0] , ALWAYS give the same result, at the same ZOOM level, no matter what tile I calculate? – Jakob Mar 12 '11 at 08:47
  • @epitah please look at my edit of the original post, there's only a little bit of tweaking left. – Jakob Mar 12 '11 at 11:31
  • @Jakob: I've send you an e-mail but this looks to me like an error. Did you look at the example: foreach(range($tminy, $tmaxy) as $ty) foreach(range($tminx, $tmaxx) as $tx) { $quadtree = $this->QuadTree($tx, $ty, $zoom); $arr[$quadtree] = $this->TileLatLonBounds($tx, $ty, $zoom); } he is using a quadtree. – Micromega Mar 14 '11 at 09:56