title: Google Maps API Coordinates Ranges Calculation
body: <p>Following Problem: I have a database table with pins coordinates (lat, lng). I send my maps current bounds to the backend to choose visible pins from the database. My code (cakePHP) is following:</p>
  
  <pre><code>private function getMapData($level = 1)
  {
      $x1 = $this-&gt;request-&gt;query('x1');
      $x2 = $this-&gt;request-&gt;query('x2');
      $y1 = $this-&gt;request-&gt;query('y1');
      $y2 = $this-&gt;request-&gt;query('y2');
  
      if ($x1 &gt; $x2)
      {
          $tmp = $x1;
          $x1 = $x2;
          $x2 = $tmp;
      }
      if ($y1 &gt; $y2)
      {
          $tmp = $y1;
          $y1 = $y2;
          $y2 = $tmp;
      }
  
  
      return $this-&gt;Map-&gt;find('all',
          array(
              'fields' =&gt; array(
                  'Map.lat',
                  'Map.lng',
                  'Map.level_number'
              ),
              'conditions' =&gt; array('Map.level_number' =&gt; $level,
              'Map.lat BETWEEN ? AND ?' =&gt; array($x1, $x2),
              'Map.lng BETWEEN ? AND ?' =&gt; array($y1, $y2),
              ),
  
              'group' =&gt; 'Map.lat, Map.lng, Map.level_number' //for distinct values
          ));
  }
  </code></pre>
  
  <p>The query just picks the pins with coordinates in bounds' range. It works most of the time and it doesn't matter which bound's corner comes first - left/right/top/bottom.<br>
  But sometimes this simple calculation seems not to work.<br>
  I'm getting the bounds and the center point. If I apply the calculation on the center point it's sometimes not within tne bounds which is realy strange but it's really correct data. So I suspect there is something else in this bounds calculation I didn't think about.<br>
  here is an example of parameters I get: ?x1=68.62149425024305&amp;y1=150.81473437500006&amp;x2=-16.728397127562125&amp;y2=173.31473437500006&amp;x=36.520571863374705&amp;y=-377.93526562499994&amp;z=3<br>
  x1 and y1 is corner point 1
  x2, y2 corner point 2
  x,y is center
  z is zoomlevel<br>
  this isn't a good example as center's lng = -377,9.. but I've got requests with coordinates within range lat between -90 and 90 and lng between -180 and 180 and still my calculation fails I get zero data from database and center point which I get from maps api is not within the bounds I get from the same api</p>