/
googleStaticMapsStylerQuery.mjs
44 lines (37 loc) · 1.55 KB
/
googleStaticMapsStylerQuery.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// @ts-check
/// <reference types="@types/google.maps" />
/**
* Converts a
* [Google Maps styler array](https://developers.google.com/maps/documentation/javascript/style-reference)
* to a
* [Google Static Maps styler URL query string](https://developers.google.com/maps/documentation/maps-static/styling).
* @param {Array<google.maps.MapTypeStyle>} styles
* [Google Maps styler array](https://developers.google.com/maps/documentation/javascript/style-reference).
* @returns {string}
* [Google Static Maps styler URL query string](https://developers.google.com/maps/documentation/maps-static/styling).
* @example
* How to create a styled static map URL:
*
* ```js
* const styles = [{ elementType: "labels", stylers: [{ visibility: "off" }] }];
* const stylerQuery = googleStaticMapsStylerQuery(styles);
* const googleStaticMapSrc = `https://maps.googleapis.com/maps/api/staticmap?center=Australia&size=250x200${stylerQuery}`;
* ```
*/
export default function googleStaticMapsStylerQuery(styles) {
let query = "";
for (const style of styles) {
const component = [];
if (style.featureType) component.push("feature:" + style.featureType);
if (style.elementType) component.push("element:" + style.elementType);
for (const rule of /** @type {Array<{ [key: string]: unknown }>} */ (
style.stylers
)) {
const [name] = Object.keys(rule);
const value = String(rule[name]).replace("#", "0x");
component.push(`${name}:${value}`);
}
query += "&style=" + encodeURIComponent(component.join("|"));
}
return query;
}