/** * Calculate the bounds corresponding to a specific center and zoom level for a give map size in pixels * @param LatLng $center * @param integer $zoom * @param integer $width * @param integer $height * @return LatLngBounds */ public static function getBoundsFromCenterAndZoom(LatLng $center, $zoom, $width, $height = null) { if (is_null($height)) { $height = $width; } $centerLat = $center->getLat(); $centerLng = $center->getLng(); $pix = LatLng::latToPixels($centerLat, $zoom); $neLat = LatLng::pixelsToLat($pix - round(($height - 1) / 2), $zoom); $swLat = LatLng::pixelsToLat($pix + round(($height - 1) / 2), $zoom); $pix = LatLng::lngToPixels($centerLng, $zoom); $swLng = LatLng::pixelsToLng($pix - round(($width - 1) / 2), $zoom); $neLng = LatLng::pixelsToLng($pix + round(($width - 1) / 2), $zoom); return new self(['southWest' => new LatLng(['lat' => $swLat, 'lng' => $swLng]), 'northEast' => new LatLng(['lat' => $neLat, 'lng' => $neLng])]); }