Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4059214
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:10:46+00:00 2026-05-20T15:10:46+00:00

Hey. I have a maps application that uses google maps. I get the bounds

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T15:10:47+00:00Added an answer on May 20, 2026 at 3:10 pm

    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:

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey i have this div that shows as a popup: <div class=popup> </div> Then
Hey right now I'm using jQuery and I have some global variables to hold
Hey. I have an object that has a string property called BackgroundColor. This string
Hey I have this code right here: http://pastie.org/534470 And on line 109 I get
Hey I have a really simple question that needs more of just an explanation
Hey I have a view that has a button that when pressed should modally
Hey i have a iframe that i am sizing to its content with the
Hey so I have a list, with some of the items containing other lists.
Hey all- I have looked this up on here and Google but none of
Hey I have a windows server running python CGI scripts and I'm having a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.