-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathindex.ts
145 lines (134 loc) · 4.61 KB
/
index.ts
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import {assembleMarkerParams} from './assemble-marker-params';
import {assemblePathParams} from './assemble-path-params';
import {formatLocation} from './helpers';
import {StaticMapsApiOptions} from './types';
import {assembleMapTypeStyles} from './assemble-map-type-styles';
const STATIC_MAPS_BASE = 'https://maps.googleapis.com/maps/api/staticmap';
/**
* Creates a URL for the Google Static Maps API with the specified parameters.
*
* @param {Object} options - The configuration options for the static map
* @param {string} options.apiKey - Your Google Maps API key (required)
* @param {number} options.width - The width of the map image in pixels (required)
* @param {number} options.height - The height of the map image in pixels (required)
* @param {StaticMapsLocation} [options.center] - The center point of the map (lat/lng or address).
* Required if no markers or paths or "visible locations" are provided.
* @param {number} [options.zoom] - The zoom level of the map. Required if no markers or paths or "visible locations" are provided.
* @param {1|2|4} [options.scale] - The resolution of the map (1, 2, or 4)
* @param {string} [options.format] - The image format (png, png8, png32, gif, jpg, jpg-baseline)
* @param {string} [options.mapType] - The type of map (roadmap, satellite, terrain, hybrid)
* @param {string} [options.language] - The language of the map labels
* @param {string} [options.region] - The region code for the map
* @param {string} [options.map_id] - The Cloud-based map style ID
* @param {StaticMapsMarker[]} [options.markers=[]] - Array of markers to display on the map
* @param {StaticMapsPath[]} [options.paths=[]] - Array of paths to display on the map
* @param {StaticMapsLocation[]} [options.visible=[]] - Array of locations that should be visible on the map
* @param {MapTypeStyle[]} [options.style=[]] - Array of style objects to customize the map appearance
*
* @returns {string} The complete Google Static Maps API URL
*
* @throws {Error} If API key is not provided
* @throws {Error} If width or height is not provided
*
* @example
* const url = createStaticMapsUrl({
* apiKey: 'YOUR_API_KEY',
* width: 600,
* height: 400,
* center: { lat: 40.714728, lng: -73.998672 },
* zoom: 12,
* markers: [
* {
* location: { lat: 40.714728, lng: -73.998672 },
* color: 'red',
* label: 'A'
* }
* ],
* paths: [
* {
* coordinates: [
* { lat: 40.714728, lng: -73.998672 },
* { lat: 40.719728, lng: -73.991672 }
* ],
* color: '0x0000ff',
* weight: 5
* }
* ],
* style: [
* {
* featureType: 'road',
* elementType: 'geometry',
* stylers: [{color: '#00ff00'}]
* }
* ]
* });
*
* // Results in URL similar to:
* // https://maps.googleapis.com/maps/api/staticmap?key=YOUR_API_KEY
* // &size=600x400
* // ¢er=40.714728,-73.998672&zoom=12
* // &markers=color:red|label:A|40.714728,-73.998672
* // &path=color:0x0000ff|weight:5|40.714728,-73.998672|40.719728,-73.991672
* // &style=feature:road|element:geometry|color:0x00ff00
*/
export function createStaticMapsUrl({
apiKey,
width,
height,
center,
zoom,
scale,
format,
mapType,
language,
region,
mapId,
markers = [],
paths = [],
visible = [],
style = []
}: StaticMapsApiOptions) {
if (!apiKey) {
console.warn('API key is required');
}
if (!width || !height) {
console.warn('Width and height are required');
}
const params: Record<string, string | number | null> = {
key: apiKey,
size: `${width}x${height}`,
...(center && {center: formatLocation(center)}),
...(zoom && {zoom}),
...(scale && {scale}),
...(format && {format}),
...(mapType && {maptype: mapType}),
...(language && {language}),
...(region && {region}),
...(mapId && {map_id: mapId})
};
const url = new URL(STATIC_MAPS_BASE);
// Params that don't need special handling
Object.entries(params).forEach(([key, value]) => {
url.searchParams.append(key, String(value));
});
// Assemble Markers
for (const markerParam of assembleMarkerParams(markers)) {
url.searchParams.append('markers', markerParam);
}
// Assemble Paths
for (const pathParam of assemblePathParams(paths)) {
url.searchParams.append('path', pathParam);
}
// Assemble visible locations
if (visible.length) {
url.searchParams.append(
'visible',
visible.map(location => formatLocation(location)).join('|')
);
}
// Assemble Map Type Styles
for (const styleString of assembleMapTypeStyles(style)) {
url.searchParams.append('style', styleString);
}
return url.toString();
}