Skip to content

Instantly share code, notes, and snippets.

@jabes
Last active December 13, 2015 19:48
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 jabes/4964834 to your computer and use it in GitHub Desktop.
Save jabes/4964834 to your computer and use it in GitHub Desktop.
This method provides a simple solution to creating static google maps. It can generate a map url with as little information as the address, but you can also extend the default parameters if needed.
function convert_color(&$color) {
if (strpos($color, "0x")) return;
$color = ltrim($color, "#");
if (ctype_xdigit($color)) $color = "0x" . $color; // is the color value a hexadecimal digit?
}
function static_google_map($mapAddress = null, $customParams = array(), $output = true) {
$delimiter = "&";
$pipe = "%7C";
$maxDimension = 640;
$apiPath = 'http://maps.googleapis.com/maps/api/staticmap';
$defaultMapParams = array(
'size' => "640x400", // required (between 0 and 640)x(between 0 and 640)
'scale' => 1, // optional (1|2)
'format' => "png", // optional (png8|png32|gif|jpg|jpg-baseline) note:png=png8
'type' => "roadmap", // optional (roadmap|satellite|terrain|hybrid)
'zoom' => 15, // optional
'icon' => array( // optional
'size' => "normal", // (tiny|mid|small)
'color' => "red",
'label' => null,
// if a url is defined, it will override the size, color, and label values
'url' => null // (png|gif|jpg)
),
'style' => array(
array(
'feature' => "all", // optional https://developers.google.com/maps/documentation/javascript/reference#MapTypeStyleFeatureType
'element' => "all", // optional https://developers.google.com/maps/documentation/javascript/reference#MapTypeStyleElementType
'rules' => array() // required https://developers.google.com/maps/documentation/javascript/reference#MapTypeStyler
)
),
'image' => array(
'id' => null,
'class' => 'google-static-map',
'alt' => null
),
// indicate whether your application is using a "sensor" (such as a GPS locator) to determine the user's location.
'sensor' => false // required
);
$mapParams = is_array($customParams) ? array_merge($defaultMapParams, $customParams) : $defaultMapParams; // merge custom parameters if needed
if (!isset($mapAddress, $mapParams['size'], $mapParams['sensor'], $output)) return; // reject if insufficient parameters
$imgDimension = explode("x", $mapParams['size']);
$imgWidth = ($imgDimension[0] > $maxDimension) ? $maxDimension : $imgDimension[0];
$imgHeight = ($imgDimension[1] > $maxDimension) ? $maxDimension : $imgDimension[1];
$queryPath = "?size=" . $imgWidth . "x" . $imgHeight; // let's begin!
if (isset($mapParams['scale']) && $mapParams['scale'] != $defaultMapParams['scale']) $queryPath .= $delimiter . "scale=" . $mapParams['scale'];
if (isset($mapParams['format']) && $mapParams['format'] != $defaultMapParams['format']) $queryPath .= $delimiter . "format=" . $mapParams['format'];
if (isset($mapParams['type']) && $mapParams['type'] != $defaultMapParams['type']) $queryPath .= $delimiter . "maptype=" . $mapParams['type'];
if (isset($mapParams['zoom'])) $queryPath .= $delimiter . "zoom=" . $mapParams['zoom'];
if (isset($mapParams['icon']['url'])) {
// custom marker icon
$queryPath .= $delimiter . "markers=icon:" . urlencode($mapParams['icon']['url']) . $pipe . urlencode($mapAddress);
} elseif (!empty($mapParams['icon'])) {
// default marker
$iconParams = array();
foreach ($mapParams['icon'] as $k => $v) {
if (isset($v)) {
if ($k == "color") convert_color($v);
$iconParams[] = $k . ":" . $v;
}
}
$queryPath .= $delimiter . "markers=" . implode($pipe, $iconParams) . $pipe . urlencode($mapAddress);
} else {
// no marker
$queryPath .= $delimiter . "center=" . urlencode($mapAddress);
}
if (isset($mapParams['style']) && is_array($mapParams['style']) && !empty($mapParams['style'])) {
foreach ($mapParams['style'] as $style) {
if (isset($style['rules']) && is_array($style['rules']) && !empty($style['rules'])) {
$feature = isset($style['feature']) ? $style['feature'] : "all";
$element = isset($style['element']) ? $style['element'] : "all";
$queryPath .= $delimiter . "style=feature:" . $feature . $pipe . "element:" . $element;
foreach ($style['rules'] as $k => $v) {
if ($k == "color" || $k == "hue") convert_color($v);
$queryPath .= $pipe . $k . ":" . $v;
}
}
}
}
$queryPath .= $delimiter . "sensor=" . ($mapParams['sensor'] ? "true" : "false");
if (!$output) return $apiPath . $queryPath;
$imgId = isset($mapParams['image']['id']) ? 'id="' . $mapParams['image']['id'] . '" ' : "";
$imgClass = isset($mapParams['image']['class']) ? 'class="' . $mapParams['image']['class'] . '" ' : "";
$imgAlt = isset($mapParams['image']['alt']) ? 'alt="' . $mapParams['image']['alt'] . '" ' : "";
echo '<img src="' . $apiPath . $queryPath . '" width="' . $imgWidth . '" height="' . $imgHeight . '" ' . $imgId . $imgClass . $imgAlt . '/>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment