xref: /expo/packages/expo-maps/build/Utils.js (revision ee0de234)
1import { Asset } from 'expo-asset';
2import React from 'react';
3export function isSimpleType(child) {
4    return (typeof child == 'string' ||
5        typeof child == 'boolean' ||
6        typeof child == 'number' ||
7        child === null ||
8        child === undefined);
9}
10export function isPolygon(child) {
11    if ('type' in child && String(child.type).includes('Polygon') && 'props' in child) {
12        const props = Object.keys(child.props);
13        if (props.includes('points')) {
14            return true;
15        }
16    }
17    return false;
18}
19export function isPolyline(child) {
20    if ('type' in child && String(child.type).includes('Polyline') && 'props' in child) {
21        const props = Object.keys(child.props);
22        if (props.includes('points')) {
23            return true;
24        }
25    }
26    return false;
27}
28export function isOverlay(child) {
29    if ('type' in child && String(child.type).includes('Overlay') && 'props' in child) {
30        const props = Object.keys(child.props);
31        if (props.includes('bounds') && props.includes('icon')) {
32            return true;
33        }
34    }
35    return false;
36}
37export function isCircle(child) {
38    if ('type' in child && String(child.type).includes('Circle') && 'props' in child) {
39        const props = Object.keys(child.props);
40        if (props.includes('center') && props.includes('radius')) {
41            return true;
42        }
43    }
44    return false;
45}
46export function isMarker(child) {
47    if ('type' in child && String(child.type).includes('Marker') && 'props' in child) {
48        const props = Object.keys(child.props);
49        if (props.includes('latitude') && props.includes('longitude')) {
50            return true;
51        }
52    }
53    return false;
54}
55export function isCluster(child) {
56    if ('type' in child && String(child.type).includes('Cluster') && 'props' in child) {
57        const props = Object.keys(child.props);
58        if (props.includes('name')) {
59            return true;
60        }
61    }
62    return false;
63}
64export function isKML(child) {
65    if ('type' in child && String(child.type).includes('KML') && 'props' in child) {
66        const props = Object.keys(child.props);
67        if (props.includes('filePath')) {
68            return true;
69        }
70    }
71    return false;
72}
73export function isGeoJson(child) {
74    if ('type' in child && String(child.type).includes('GeoJson') && 'props' in child) {
75        const props = Object.keys(child.props);
76        if (props.includes('geoJsonString')) {
77            return true;
78        }
79    }
80    return false;
81}
82export function isHeatmap(child) {
83    if ('type' in child && String(child.type).includes('Heatmap') && 'props' in child) {
84        const props = Object.keys(child.props);
85        if (props.includes('points')) {
86            return true;
87        }
88    }
89    return false;
90}
91export function isHexColor(color) {
92    return color.length > 0 && color[0] === '#';
93}
94export function mapColorToHexColor(color, defaultColor) {
95    let defColor = defaultColor;
96    if (defColor === undefined)
97        defColor = '#000000';
98    const colors = {
99        red: '#ff0000',
100        blue: '#0000ff',
101        green: '#00ff00',
102        black: '#000000',
103        white: '#ffffff',
104        gray: '#808080',
105        cyan: '#00ffff',
106        magenta: '#ff00ff',
107        yellow: '#ffff00',
108        lightgray: '#d3d3d3',
109        darkgray: '#a9a9a9',
110        aqua: '#00ffff',
111        fuchsia: '#ca2c92',
112        lime: '#bfff00',
113        maroon: '#800000',
114        navy: '#000080',
115        olive: '#808000',
116        purple: '#800080',
117        silver: '#c0c0c0',
118        teal: '#008080',
119        default: defColor,
120    };
121    return colors[color] || colors['default'];
122}
123export function warnIfChildIsIncompatible(child) {
124    if (typeof child == 'string' || typeof child == 'boolean' || typeof child == 'number') {
125        console.warn(`Warning! Child of type ${typeof child} isn't valid ExpoMap child!`);
126    }
127    else if (child != null && child !== undefined) {
128        console.log(child.type);
129        console.warn(`Warning! Child of type ${child.type} isn't valid ExpoMap child!`);
130    }
131}
132export function buildGeoJsonObject(child) {
133    if (child.props.defaultStyle?.marker?.color !== undefined &&
134        !isHexColor(child.props.defaultStyle.marker.color)) {
135        child.props.defaultStyle.marker.color = mapColorToHexColor(child.props.defaultStyle?.marker.color, '#ff0000');
136    }
137    if (child.props.defaultStyle?.polygon?.fillColor !== undefined &&
138        !isHexColor(child.props.defaultStyle.polygon.fillColor)) {
139        child.props.defaultStyle.polygon.fillColor = mapColorToHexColor(child.props.defaultStyle.polygon.fillColor);
140    }
141    if (child.props.defaultStyle?.polygon?.strokeColor !== undefined &&
142        !isHexColor(child.props.defaultStyle.polygon.strokeColor)) {
143        child.props.defaultStyle.polygon.strokeColor = mapColorToHexColor(child.props.defaultStyle.polygon.strokeColor);
144    }
145    if (child.props.defaultStyle?.polyline?.color !== undefined &&
146        !isHexColor(child.props.defaultStyle.polyline.color)) {
147        child.props.defaultStyle.polyline.color = mapColorToHexColor(child.props.defaultStyle.polyline.color);
148    }
149    return {
150        type: 'geojson',
151        geoJsonString: child.props.geoJsonString,
152        defaultStyle: child.props.defaultStyle,
153    };
154}
155export async function buildMarkerObject(child) {
156    let iconPath = undefined;
157    if (child.props.icon !== undefined) {
158        iconPath = await Asset.fromModule(child.props.icon).downloadAsync();
159    }
160    const markerObject = {
161        type: 'marker',
162        id: child.props.id,
163        latitude: child.props.latitude,
164        longitude: child.props.longitude,
165        markerTitle: child.props.markerTitle,
166        markerSnippet: child.props.markerSnippet,
167        icon: iconPath?.localUri,
168        color: child.props.color,
169        draggable: child.props.draggable ? child.props.draggable : false,
170        anchorU: child.props.anchorU,
171        anchorV: child.props.anchorV,
172        opacity: child.props.opacity ? child.props.opacity : 1,
173    };
174    if (markerObject.color !== undefined && !isHexColor(markerObject.color)) {
175        markerObject.color = mapColorToHexColor(markerObject.color, '#ff0000');
176    }
177    return markerObject;
178}
179export async function buildOverlayObject(child) {
180    const iconPath = await Asset.fromModule(child.props.icon).downloadAsync();
181    const overlayObject = {
182        type: 'overlay',
183        bounds: child.props.bounds,
184        icon: iconPath.localUri,
185    };
186    return overlayObject;
187}
188export function buildPolygonObject(child) {
189    const polygonObject = {
190        type: 'polygon',
191        points: child.props.points,
192        fillColor: child.props.fillColor,
193        strokeColor: child.props.strokeColor,
194        strokeWidth: child.props.strokeWidth,
195        strokePattern: child.props.strokePattern,
196        jointType: child.props.jointType,
197    };
198    if (polygonObject.fillColor !== undefined && !isHexColor(polygonObject.fillColor)) {
199        polygonObject.fillColor = mapColorToHexColor(polygonObject.fillColor);
200    }
201    return polygonObject;
202}
203export function buildPolylineObject(child) {
204    const polylineObject = {
205        type: 'polyline',
206        points: child.props.points,
207        color: child.props.color,
208        width: child.props.width,
209        pattern: child.props.pattern,
210        jointType: child.props.jointType,
211        capType: child.props.capType,
212    };
213    if (polylineObject.color !== undefined && !isHexColor(polylineObject.color)) {
214        polylineObject.color = mapColorToHexColor(polylineObject.color);
215    }
216    return polylineObject;
217}
218export function buildCircleObject(child) {
219    return {
220        type: 'circle',
221        center: child.props.center,
222        radius: child.props.radius,
223        fillColor: child.props.fillColor,
224        strokeColor: child.props.strokeColor,
225        strokeWidth: child.props.strokeWidth,
226    };
227}
228export async function buildKMLObject(child) {
229    const filePath = await Asset.fromModule(child.props.filePath).downloadAsync();
230    return {
231        type: 'kml',
232        filePath: filePath.localUri,
233    };
234}
235export async function buildClusterObject(child) {
236    const clusterChildrenArray = React.Children.map(child.props.children, async (clusterChild) => {
237        if (!isSimpleType(clusterChild)) {
238            if (isMarker(clusterChild)) {
239                return buildMarkerObject(clusterChild);
240            }
241        }
242        warnIfChildIsIncompatible(clusterChild);
243        return null;
244    });
245    if (clusterChildrenArray !== undefined) {
246        let iconPath = undefined;
247        if (child.props.icon !== undefined) {
248            iconPath = await Asset.fromModule(child.props.icon).downloadAsync();
249        }
250        // TODO(@lukmccall): remove any cast
251        const clusterPropObjects = await Promise.all(clusterChildrenArray);
252        let minimumClusterSize;
253        if (child.props.minimumClusterSize !== undefined && child.props.minimumClusterSize > 0) {
254            minimumClusterSize = child.props.minimumClusterSize;
255        }
256        else {
257            minimumClusterSize = 4;
258        }
259        const clusterObject = {
260            type: 'cluster',
261            id: child.props.id,
262            markers: clusterPropObjects,
263            name: child.props.name,
264            minimumClusterSize,
265            markerTitle: child.props.markerTitle,
266            markerSnippet: child.props.markerSnippet,
267            icon: iconPath?.localUri,
268            color: child.props.color,
269            opacity: child.props.opacity ? child.props.opacity : 1,
270        };
271        if (clusterObject.color !== undefined && !isHexColor(clusterObject.color)) {
272            clusterObject.color = mapColorToHexColor(clusterObject.color, '#ff0000');
273        }
274        return clusterObject;
275    }
276    return null;
277}
278export function buildHeatmapObject(child) {
279    return {
280        type: 'heatmap',
281        points: child.props.points,
282        radius: child.props.radius,
283        gradient: child.props.gradient,
284        opacity: child.props.opacity,
285    };
286}
287//# sourceMappingURL=Utils.js.map