You are here

function AddressToGeo::getZoom in Geolocation Address Link 8

A method to roughly calculate the right zoom level for a place.

Uses the boundary information and an assumption about the pixel width that the map will be displayed at.

Parameters

array $boundary: An array of boundary values as returned by Google's geocoding service.

integer $map_size: The estimated pixel dimensions of the map display.

Return value

integer $zoom A zoom level that will display everything in the boundary box.

See also

https://stackoverflow.com/questions/6048975/google-maps-v3-how-to-calcul...

1 call to AddressToGeo::getZoom()
AddressToGeo::geocode in src/AddressToGeo.php
Geocode an address.

File

src/AddressToGeo.php, line 268

Class

AddressToGeo
Class AddressToGeo.

Namespace

Drupal\geolocation_address_link

Code

function getZoom($boundary, $map_size = '400x400') {

  // Map dimensions
  $dimensions = explode('x', $map_size);
  $width = $dimensions[0];
  $height = $dimensions[1];

  // The entire world fits in a 256 pixel square at zoom level 0.
  $world_height = 256;
  $world_width = 256;
  $zoom_max = 21;
  $ne_lat = $boundary['lat_north_east'];
  $ne_lng = $boundary['lng_north_east'];
  $sw_lat = $boundary['lat_south_west'];
  $sw_lng = $boundary['lng_south_west'];
  $latFraction = ($this
    ->latRad($ne_lat) - $this
    ->latRad($sw_lat)) / pi();
  $lngDiff = $ne_lng - $sw_lng;
  $lngFraction = ($lngDiff < 0 ? $lngDiff + 360 : $lngDiff) / 360;
  $latZoom = $this
    ->zoom($height, $world_height, $latFraction);
  $lngZoom = $this
    ->zoom($width, $world_width, $lngFraction);
  return min($latZoom, $lngZoom, $zoom_max);
}