FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
google-map-react/src/lib/index.js at develop · bkocoglu/google-map-react · GitHub
bkocoglu
/
google-map-react
Public
forked from
google-map-react/google-map-react
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
google-map-react
/
src
/
lib
/
index.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
216 lines (182 loc) · 5.16 KB
Breadcrumbs
google-map-react
/
src
/
lib
/
index.js
Copy path
File metadata and controls
216 lines (182 loc) · 5.16 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import
log2
from
'../utils/log2'
;
const
GOOGLE_TILE_SIZE
=
256
;
function
latLng2World
(
{
lat
,
lng
}
)
{
const
sin
=
Math
.
sin
(
(
lat
*
Math
.
PI
)
/
180
)
;
const
x
=
lng
/
360
+
0.5
;
let
y
=
0.5
-
(
0.25
*
Math
.
log
(
(
1
+
sin
)
/
(
1
-
sin
)
)
)
/
Math
.
PI
;
y
=
y
<
0
// eslint-disable-line
?
0
:
y
>
1
?
1
:
y
;
return
{
x
,
y
}
;
}
function
world2LatLng
(
{
x
,
y
}
)
{
const
n
=
Math
.
PI
-
2
*
Math
.
PI
*
y
;
// TODO test that this is faster
// 360 * Math.atan(Math.exp((180 - y * 360) * Math.PI / 180)) / Math.PI - 90;
return
{
lat
:
(
180
/
Math
.
PI
)
*
Math
.
atan
(
0.5
*
(
Math
.
exp
(
n
)
-
Math
.
exp
(
-
n
)
)
)
,
lng
:
x
*
360
-
180
,
}
;
}
// Thank you wiki https://en.wikipedia.org/wiki/Geographic_coordinate_system
function
latLng2MetersPerDegree
(
{
lat
}
)
{
const
phi
=
(
lat
*
Math
.
PI
)
/
180
;
const
metersPerLatDegree
=
111132.92
-
559.82
*
Math
.
cos
(
2
*
phi
)
+
1.175
*
Math
.
cos
(
4
*
phi
)
-
0.0023
*
Math
.
cos
(
6
*
phi
)
;
const
metersPerLngDegree
=
111412.84
*
Math
.
cos
(
phi
)
-
93.5
*
Math
.
cos
(
3
*
phi
)
+
0.118
*
Math
.
cos
(
5
*
phi
)
;
return
{
metersPerLatDegree
,
metersPerLngDegree
}
;
}
function
meters2LatLngBounds
(
meters
,
{
lat
,
lng
}
)
{
const
{
metersPerLatDegree
,
metersPerLngDegree
}
=
latLng2MetersPerDegree
(
{
lat
,
}
)
;
const
latDelta
=
(
0.5
*
meters
)
/
metersPerLatDegree
;
const
lngDelta
=
(
0.5
*
meters
)
/
metersPerLngDegree
;
return
{
nw
:
{
lat
:
lat
-
latDelta
,
lng
:
lng
-
lngDelta
,
}
,
se
:
{
lat
:
lat
+
latDelta
,
lng
:
lng
+
lngDelta
,
}
,
}
;
}
function
meters2WorldSize
(
meters
,
{
lat
,
lng
}
)
{
const
{
nw
,
se
}
=
meters2LatLngBounds
(
meters
,
{
lat
,
lng
}
)
;
const
nwWorld
=
latLng2World
(
nw
)
;
const
seWorld
=
latLng2World
(
se
)
;
const
w
=
Math
.
abs
(
seWorld
.
x
-
nwWorld
.
x
)
;
const
h
=
Math
.
abs
(
seWorld
.
y
-
nwWorld
.
y
)
;
return
{
w
,
h
}
;
}
function
fitNwSe
(
nw
,
se
,
width
,
height
)
{
const
EPS
=
0.000000001
;
const
nwWorld
=
latLng2World
(
nw
)
;
const
seWorld
=
latLng2World
(
se
)
;
const
dx
=
nwWorld
.
x
<
seWorld
.
x
?
seWorld
.
x
-
nwWorld
.
x
:
1
-
nwWorld
.
x
+
seWorld
.
x
;
const
dy
=
seWorld
.
y
-
nwWorld
.
y
;
if
(
dx
<=
0
&&
dy
<=
0
)
{
return
null
;
}
const
zoomX
=
log2
(
width
/
GOOGLE_TILE_SIZE
/
Math
.
abs
(
dx
)
)
;
const
zoomY
=
log2
(
height
/
GOOGLE_TILE_SIZE
/
Math
.
abs
(
dy
)
)
;
const
zoom
=
Math
.
floor
(
EPS
+
Math
.
min
(
zoomX
,
zoomY
)
)
;
// TODO find center just unproject middle world point
const
middle
=
{
x
:
nwWorld
.
x
<
seWorld
.
x
// eslint-disable-line
?
0.5
*
(
nwWorld
.
x
+
seWorld
.
x
)
:
nwWorld
.
x
+
seWorld
.
x
-
1
>
0
?
0.5
*
(
nwWorld
.
x
+
seWorld
.
x
-
1
)
:
0.5
*
(
1
+
nwWorld
.
x
+
seWorld
.
x
)
,
y
:
0.5
*
(
nwWorld
.
y
+
seWorld
.
y
)
,
}
;
const
scale
=
Math
.
pow
(
2
,
zoom
)
;
const
halfW
=
width
/
scale
/
GOOGLE_TILE_SIZE
/
2
;
const
halfH
=
height
/
scale
/
GOOGLE_TILE_SIZE
/
2
;
const
newNW
=
world2LatLng
(
{
x
:
middle
.
x
-
halfW
,
y
:
middle
.
y
-
halfH
,
}
)
;
const
newSE
=
world2LatLng
(
{
x
:
middle
.
x
+
halfW
,
y
:
middle
.
y
+
halfH
,
}
)
;
return
{
center
:
world2LatLng
(
middle
)
,
zoom
,
newBounds
:
{
nw
:
newNW
,
se
:
newSE
,
}
,
}
;
}
export
function
convertNeSwToNwSe
(
{
ne
,
sw
}
)
{
return
{
nw
:
{
lat
:
ne
.
lat
,
lng
:
sw
.
lng
,
}
,
se
:
{
lat
:
sw
.
lat
,
lng
:
ne
.
lng
,
}
,
}
;
}
export
function
convertNwSeToNeSw
(
{
nw
,
se
}
)
{
return
{
ne
:
{
lat
:
nw
.
lat
,
lng
:
se
.
lng
,
}
,
sw
:
{
lat
:
se
.
lat
,
lng
:
nw
.
lng
,
}
,
}
;
}
export
function
fitBounds
(
{
nw
,
se
,
ne
,
sw
}
,
{
width
,
height
}
)
{
let
fittedData
;
if
(
nw
&&
se
)
{
fittedData
=
fitNwSe
(
nw
,
se
,
width
,
height
)
;
}
else
{
const
calculatedNwSe
=
convertNeSwToNwSe
(
{
ne
,
sw
}
)
;
fittedData
=
fitNwSe
(
calculatedNwSe
.
nw
,
calculatedNwSe
.
se
,
width
,
height
)
;
}
return
{
...
fittedData
,
newBounds
:
{
...
fittedData
.
newBounds
,
...
convertNwSeToNeSw
(
fittedData
.
newBounds
)
,
}
,
}
;
}
// -------------------------------------------------------------------
// Helpers to calc some markers size
export
function
meters2ScreenPixels
(
meters
,
{
lat
,
lng
}
,
zoom
)
{
const
{
w
,
h
}
=
meters2WorldSize
(
meters
,
{
lat
,
lng
}
)
;
const
scale
=
Math
.
pow
(
2
,
zoom
)
;
const
wScreen
=
w
*
scale
*
GOOGLE_TILE_SIZE
;
const
hScreen
=
h
*
scale
*
GOOGLE_TILE_SIZE
;
return
{
w
:
wScreen
,
h
:
hScreen
,
}
;
}
// --------------------------------------------------
// Helper functions for working with svg tiles, (examples coming soon)
export
function
tile2LatLng
(
{
x
,
y
}
,
zoom
)
{
const
n
=
Math
.
PI
-
(
2
*
Math
.
PI
*
y
)
/
Math
.
pow
(
2
,
zoom
)
;
return
{
lat
:
(
180
/
Math
.
PI
)
*
Math
.
atan
(
0.5
*
(
Math
.
exp
(
n
)
-
Math
.
exp
(
-
n
)
)
)
,
lng
:
(
x
/
Math
.
pow
(
2
,
zoom
)
)
*
360
-
180
,
}
;
}
export
function
latLng2Tile
(
{
lat
,
lng
}
,
zoom
)
{
const
worldCoords
=
latLng2World
(
{
lat
,
lng
}
)
;
const
scale
=
Math
.
pow
(
2
,
zoom
)
;
return
{
x
:
Math
.
floor
(
worldCoords
.
x
*
scale
)
,
y
:
Math
.
floor
(
worldCoords
.
y
*
scale
)
,
}
;
}
export
function
getTilesIds
(
{
from
,
to
}
,
zoom
)
{
const
scale
=
Math
.
pow
(
2
,
zoom
)
;
const
ids
=
[
]
;
for
(
let
x
=
from
.
x
;
x
!==
(
to
.
x
+
1
)
%
scale
;
x
=
(
x
+
1
)
%
scale
)
{
for
(
let
y
=
from
.
y
;
y
!==
(
to
.
y
+
1
)
%
scale
;
y
=
(
y
+
1
)
%
scale
)
{
ids
.
push
(
[
zoom
,
x
,
y
]
)
;
}
}
return
ids
;
}
Back
|
FazBrowse Home
|
New Git URL