Skip to content

Instantly share code, notes, and snippets.

@maximishchenko
Last active May 19, 2017 11:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maximishchenko/44dfcd5faaf76fa09e5374b7e7fe54d5 to your computer and use it in GitHub Desktop.
Save maximishchenko/44dfcd5faaf76fa09e5374b7e7fe54d5 to your computer and use it in GitHub Desktop.
Download Google Maps Image by Coordinates
<?php
/**
* Get Image from Google Maps API by place coordinates and download it.
* @author Maxim Ishchenko <maxim.ishchenko@gmail.com>
* @version 1.0*
* @uses
*
* $map = new GoogleMaps(
* array(
* 'coordinates' => '<latitude,longitude>',
* 'scale' => <image_scale>,
* 'maptype' => '<type_of_map>',
* 'zoom' => <zoom_value>,
* 'language' => 'ru',
* 'format' => '<output_image_format>',
* 'name' => '<output_image_name>',
* 'size' => '<output_image_size>',
* )
* );
* $imagePath = $map->getMapsImage();
* <img src="<?php echo $imagePath; ?>" />
*
* <latitude,longitude> - latitude and longitude place coordinates
* <image_scale> - scale image quality (1, 2), in Premium plan can be 4
* <type_of_map> - type of map on output image file ('roadmap', 'satellite', 'hybrid', 'terrain')
* <zoom_value> - level of zoom (1 - world, 5 - continent, 10 - city, 15 - streets, 20 - buildings)
* <language> - international language code ('en', 'ru', etc ...)
* <format> - extension of image ('jpg', 'jpeg', 'png', 'gif')
* <name> - image name prefix
* <size> - output image size, widthxheight
*
* @property string $coordinates
* @property integer $scale
* @property string $maptype
* @property integer $zoom
* @property string $language
* @property string $format
* @property string $name
* @property string $size
*/
class GoogleMaps
{
const FORMAT = 'png';
const FORMAT_ARRAY = array('gif', 'jpeg', 'jpg', 'png');
const MAPTYPE = 'roadmap';
const MAPTYPES_ARRAY = array('roadmap', 'satellite', 'hybrid', 'terrain');
const ZOOM = 1;
const MAX_ZOOM = 30;
const LANGUAGE = 'ru';
const IMAGE_FOLDER = 'images/';
const NAME = 'google-map-';
const SIZE = '600x400';
const LABEL_COLOR = 'red';
const DEFAULT_SCALE = 1;
const SCALES_ARRAY = array(1,2);
/**
* @access private
* @property string $imageFolder
* @property string $name
* @property string $format
* @property string $size
* @property string $coordinates
* @property string $labelColor
* @property integer $scale
* @property string $maptype
* @property integer $zoom
* @property string $language
*/
private $imageFolder;
private $name;
private $format;
private $size;
private $coordiates;
private $labelColor;
private $scale;
private $maptype;
private $zoom;
private $language;
function __construct($args = array())
{
/**
* Coordinates required
* @return Exception if not exist
*/
if(!isset($args['coordinates']))
{
throw new Exception("Coordinates required!", 1);
}
/**
* @var string $coordinates coordinates string for set center and place label
*/
$this->coordinates = $args['coordinates'];
/**
* @var integer $scale value for image scale set scale if argument exist and argument value in array from self::SCALES_ARRAY, else set value from self::DEFAULT_SCALE constant
*/
$this->scale = (isset($args['scale']) && in_array($args['scale'], self::SCALES_ARRAY)) ? $args['scale'] : self::DEFAULT_SCALE;
/**
* @var string $format output image format. Set format if argument exist and argument value in array from self::FORMAT_ARRAY, else set value from self::FORMAT constant
*/
$this->format = (isset($args['format']) && in_array(strtolower($args['format']), self::FORMAT_ARRAY)) ? $args['format'] : self::FORMAT;
/**
* @var string $maptype type of generated from Google Maps Api Static Map. Set format if argument exist and argument value in array from self::MAPTYPES_ARRAY, else set value from self::MAPTYPE constant
*/
$this->maptype = (isset($args['maptype']) && in_array(strtolower($args['maptype']), self::MAPTYPES_ARRAY)) ? $args['maptype'] : self::MAPTYPE;
/**
* @var integer $zoom zoom level value. Set level if argument exist and argument less then self::ZOOM constant value and more than self::MAX_ZOOM constant value, else set value from self::ZOOM constant
*/
$this->zoom = (isset($args['zoom']) && $args['zoom'] >= self::ZOOM && $args['zoom'] <= self::MAX_ZOOM) ? $args['zoom'] : self::ZOOM;
/**
* @var string $language international language code. Set language code if argument exist, else return self::LANGUAGE constant value
*/
$this->language = isset($args['language']) ? $args['language'] : self::LANGUAGE;
/**
* @var string $name prefix of output image name. Name contains a prefix and md5-hash of current time in milliseconds
*/
$this->name = isset($args['name']) ? $args['name'] : self::NAME;
/**
* @var string $imageFolder path to folder where images were stored. Need to be a real path. Set an argument if it exist, else set self::IMAGE_FOLDER constant value
*/
$this->imageFolder = isset($args['imageFolder']) ? $args['imageFolder'] : self::IMAGE_FOLDER;
/**
* @var string $size size (width x height format) of output image
*/
$this->size = isset($args['size']) ? $args['size'] : self::SIZE;
}
/**
* @var string $name image name prefix
* @var string $format output image format
* @access private
* @return string full image name with extension, based on inputed arguments or __construct method
*/
private function getImageName()
{
return $this->name . md5(time()). '.' .$this->format;
}
/**
* @var string $imageFolder real path to store downloaded images
* @var string $getImageName result of getImagename() function
* @access private
* @return string|Exception return full image path or Exception if not exist
*/
private function getImagePath()
{
if(is_dir($this->imageFolder))
{
return $this->imageFolder.$this->getImageName();
}
throw new Exception("Incorrect Path", 1);
}
/**
* @var string $coordinates coordinates value for string google map static api url
* @var string $maptype maptype value for string google map static api url
* @var string $language language paramenter for string google map static api url
* @var integer $zoom zoom value for string google maps static api url
* @var integer $scale scale value for string google maps static api url
* @var string $format format value for string google maps static api url
* @var string $size size value for string google maps static api url
* @access private
* @return url generated image url
*/
private function getAPIString()
{
$src = 'https://maps.googleapis.com/maps/api/staticmap?center='.$this->coordinates.'&maptype=' . $this->maptype . '&markers=color:red%7Clabel%7C'.$this->coordinates.'&language=' . $this->language . '&zoom=' . $this->zoom . '&scale=' . $this->scale . '&format=' . $this->format . '&size=' . $this->size;
return $src;
}
/**
* @var string $getImagePath full path to stored image
* @var string $getAPIString url to api-generated image
* @access public
* @return string path to saved image
*/
public function getMapsImage()
{
$imagePath = $this->getImagePath();
file_put_contents($imagePath, file_get_contents($this->getAPIString()));
return $imagePath;
}
}
$a = new GoogleMaps(
array(
'coordinates' => '44.220189,%2043.066332',
'scale' => 2,
'maptype' => 'terrain',
'zoom' => 14,
'language' => 'ru',
'format' => 'png',
'name' => 'gmi-',
'size' => '1146x671',
)
);
$imagePath = $a->getMapsImage();
?>
<img src="<?php echo $imagePath; ?>" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment