-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathApp.tsx
More file actions
143 lines (126 loc) · 3.51 KB
/
App.tsx
File metadata and controls
143 lines (126 loc) · 3.51 KB
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
import React, { useCallback, useEffect, useRef, useState } from 'react'
import { Dimensions, StyleSheet, View } from 'react-native'
import { Marker, Region } from 'react-native-maps'
import ClusteredMapView from './components/ClusteredMapView'
import MapZoomPanel from './components/MapZoomPanel'
const getRandomLatitude = (min = 48, max = 56) => {
return Math.random() * (max - min) + min
}
const getRandomLongitude = (min = 14, max = 24) => {
return Math.random() * (max - min) + min
}
const getRegionForZoom = (lat: number, lon: number, zoom: number) => {
const distanceDelta = Math.exp(Math.log(360) - zoom * Math.LN2)
const { width, height } = Dimensions.get('window')
const aspectRatio = width / height
return {
latitude: lat,
longitude: lon,
latitudeDelta: distanceDelta * aspectRatio,
longitudeDelta: distanceDelta,
}
}
const getZoomFromRegion = (region: Region) => {
return Math.round(Math.log(360 / region.longitudeDelta) / Math.LN2)
}
interface Markers {
id: number
latitude: number
longitude: number
}
function App(): JSX.Element {
const map = useRef(null)
const [zoom, setZoom] = useState<number>(18)
const [markers, setMarkers] = useState<Markers[]>([
{ id: 0, latitude: 53.91326738786109, longitude: 27.523712915343737 },
])
const [region, setRegion] = useState<Region>({
latitude: 53.91326738786109,
longitude: 27.523712915343737,
latitudeDelta: 1.5,
longitudeDelta: 1.5,
})
const generateMarkers = useCallback((lat: number, long: number) => {
const markersArray = []
for (let i = 0; i < 50; i++) {
markersArray.push({
id: i,
latitude: getRandomLatitude(lat - 0.05, lat + 0.05),
longitude: getRandomLongitude(long - 0.05, long + 0.05),
})
}
setMarkers(markersArray)
}, [])
const mapZoomIn = () => {
if (zoom > 18) {
setZoom(18)
} else {
setZoom(zoom + 1)
const regn = getRegionForZoom(region.latitude, region.longitude, zoom + 1)
map.current.animateToRegion(regn, 200)
}
}
const mapZoomOut = () => {
if (zoom < 3) {
setZoom(3)
} else {
setZoom(zoom - 1)
const regn = getRegionForZoom(region.latitude, region.longitude, zoom - 1)
map.current.animateToRegion(regn, 200)
}
}
const onRegionChangeComplete = (newRegion: Region) => {
setZoom(getZoomFromRegion(newRegion))
setRegion(newRegion)
}
useEffect(() => {
generateMarkers(region.latitude, region.longitude)
}, [])
return (
<View style={styles.container}>
<ClusteredMapView
clusterColor="red"
ref={map}
mapType="hybrid"
style={styles.mapView}
initialRegion={region}
onRegionChangeComplete={onRegionChangeComplete}>
{markers.map((item) => (
<Marker
key={item.id}
coordinate={{
latitude: item.latitude,
longitude: item.longitude,
}}
/>
))}
</ClusteredMapView>
<MapZoomPanel
onZoomIn={() => {
mapZoomIn()
}}
onZoomOut={() => {
mapZoomOut()
}}
/>
</View>
)
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
mapView: { flex: 1, width: '100%', height: '100%' },
customMarker: {
backgroundColor: '#ffffff',
borderRadius: 8,
width: 50,
height: 50,
justifyContent: 'center',
alignItems: 'center',
},
})
export default App