|
| 1 | +import React, { useRef, useState, useMemo } from 'react'; |
| 2 | +import MapWrapper from '../components/MapWrapper'; |
| 3 | +import ControlPanel from '../components/ControlPanel'; |
| 4 | +import type { |
| 5 | + GoogleMapsViewRef, |
| 6 | + RNMarker, |
| 7 | + RNMarkerSvg, |
| 8 | +} from 'react-native-google-maps-plus'; |
| 9 | +import { useClusterer } from 'react-native-clusterer'; |
| 10 | +import type Supercluster from 'react-native-clusterer/lib/typescript/types'; |
| 11 | +import { randomCoordinates } from '../utils/mapGenerators'; |
| 12 | + |
| 13 | +export default function ClusteringScreen() { |
| 14 | + const mapRef = useRef<GoogleMapsViewRef | null>(null); |
| 15 | + const [coordinates] = useState( |
| 16 | + Array.from({ length: 500 }, () => |
| 17 | + randomCoordinates(37.7749, -122.4194, 0.2) |
| 18 | + ) |
| 19 | + ); |
| 20 | + const [region, setRegion] = useState({ |
| 21 | + center: { |
| 22 | + latitude: 37.7749, |
| 23 | + longitude: -122.4194, |
| 24 | + }, |
| 25 | + latitudeDelta: 0.4, |
| 26 | + longitudeDelta: 0.4, |
| 27 | + }); |
| 28 | + |
| 29 | + const mapDimensions = useMemo(() => ({ width: 400, height: 800 }), []); |
| 30 | + |
| 31 | + const data = useMemo< |
| 32 | + Array< |
| 33 | + Supercluster.PointFeature<{ |
| 34 | + id: string; |
| 35 | + svgIcon: RNMarkerSvg; |
| 36 | + }> |
| 37 | + > |
| 38 | + >( |
| 39 | + () => |
| 40 | + coordinates.map((e, i) => { |
| 41 | + return { |
| 42 | + type: 'Feature', |
| 43 | + geometry: { |
| 44 | + type: 'Point', |
| 45 | + coordinates: [e.longitude, e.latitude], |
| 46 | + }, |
| 47 | + properties: { |
| 48 | + id: `sf-${i}`, |
| 49 | + svgIcon: { |
| 50 | + width: 32, |
| 51 | + height: 44, |
| 52 | + svgString: ` |
| 53 | + <svg xmlns="http://www.w3.org/2000/svg" width="66" height="88" viewBox="0 0 64 88"> |
| 54 | + <path |
| 55 | + d="M32 2c-14.36 0-26 11.64-26 26 0 18.2 20.67 38.86 24.82 43.02a1.7 1.7 0 0 0 2.36 0C37.33 66.86 58 46.2 58 28 58 13.64 46.36 2 32 2z" |
| 56 | + fill="red" |
| 57 | + /> |
| 58 | + <circle cx="32" cy="28" r="10" fill="#FFFFFF" /> |
| 59 | + <ellipse cx="32" cy="82" rx="14" ry="4" fill="#000000" opacity="0.15" /> |
| 60 | + </svg> |
| 61 | + `, |
| 62 | + }, |
| 63 | + }, |
| 64 | + }; |
| 65 | + }), |
| 66 | + [coordinates] |
| 67 | + ); |
| 68 | + |
| 69 | + const clusterRegion = useMemo( |
| 70 | + () => ({ |
| 71 | + latitude: region.center.latitude, |
| 72 | + longitude: region.center.longitude, |
| 73 | + latitudeDelta: region.latitudeDelta, |
| 74 | + longitudeDelta: region.longitudeDelta, |
| 75 | + }), |
| 76 | + [region] |
| 77 | + ); |
| 78 | + |
| 79 | + const clusterOptions = useMemo( |
| 80 | + () => ({ radius: 60, maxZoom: 16, minZoom: 0 }), |
| 81 | + [] |
| 82 | + ); |
| 83 | + |
| 84 | + const [points] = useClusterer( |
| 85 | + data, |
| 86 | + mapDimensions, |
| 87 | + clusterRegion, |
| 88 | + clusterOptions |
| 89 | + ); |
| 90 | + |
| 91 | + const markers: RNMarker[] = useMemo(() => { |
| 92 | + return points.map((feature, i) => { |
| 93 | + const [lng, lat] = feature.geometry.coordinates as [number, number]; |
| 94 | + const isCluster = 'cluster' in feature.properties; |
| 95 | + // @ts-ignore |
| 96 | + const count = feature.properties?.point_count ?? 0; |
| 97 | + const icon = isCluster |
| 98 | + ? { |
| 99 | + width: 36, |
| 100 | + height: 36, |
| 101 | + svgString: `<svg viewBox="0 0 64 64" width="48" height="48" xmlns="http://www.w3.org/2000/svg"> |
| 102 | + <circle cx="32" cy="32" r="28" fill="#7C4DFF"/> |
| 103 | + <text x="32" y="40" text-anchor="middle" font-size="22" font-family="Arial" fill="#fff" font-weight="bold"> |
| 104 | + ${count} |
| 105 | + </text> |
| 106 | + </svg>`, |
| 107 | + } |
| 108 | + : feature.properties.svgIcon; |
| 109 | + |
| 110 | + console.log(feature); |
| 111 | + |
| 112 | + return { |
| 113 | + id: feature.id?.toString() ?? i.toString(), |
| 114 | + coordinate: { latitude: lat, longitude: lng }, |
| 115 | + iconSvg: icon, |
| 116 | + } as RNMarker; |
| 117 | + }); |
| 118 | + }, [points]); |
| 119 | + |
| 120 | + return ( |
| 121 | + <MapWrapper mapRef={mapRef} markers={markers} onCameraChange={setRegion}> |
| 122 | + <ControlPanel mapRef={mapRef} buttons={[]} /> |
| 123 | + </MapWrapper> |
| 124 | + ); |
| 125 | +} |
0 commit comments