MCPcopy Index your code
hub / github.com/google-map-react/google-map-react / fitNwSe

Function fitNwSe

src/lib/index.js:74–122  ·  view source on GitHub ↗
(nw, se, width, height)

Source from the content-addressed store, hash-verified

72}
73
74function fitNwSe(nw, se, width, height) {
75 const EPS = 0.000000001;
76 const nwWorld = latLng2World(nw);
77 const seWorld = latLng2World(se);
78 const dx =
79 nwWorld.x < seWorld.x ? seWorld.x - nwWorld.x : 1 - nwWorld.x + seWorld.x;
80 const dy = seWorld.y - nwWorld.y;
81
82 if (dx <= 0 && dy <= 0) {
83 return null;
84 }
85
86 const zoomX = log2(width / GOOGLE_TILE_SIZE / Math.abs(dx));
87 const zoomY = log2(height / GOOGLE_TILE_SIZE / Math.abs(dy));
88 const zoom = Math.floor(EPS + Math.min(zoomX, zoomY));
89
90 // TODO find center just unproject middle world point
91 const middle = {
92 x: nwWorld.x < seWorld.x // eslint-disable-line
93 ? 0.5 * (nwWorld.x + seWorld.x)
94 : nwWorld.x + seWorld.x - 1 > 0
95 ? 0.5 * (nwWorld.x + seWorld.x - 1)
96 : 0.5 * (1 + nwWorld.x + seWorld.x),
97 y: 0.5 * (nwWorld.y + seWorld.y),
98 };
99
100 const scale = Math.pow(2, zoom);
101 const halfW = width / scale / GOOGLE_TILE_SIZE / 2;
102 const halfH = height / scale / GOOGLE_TILE_SIZE / 2;
103
104 const newNW = world2LatLng({
105 x: middle.x - halfW,
106 y: middle.y - halfH,
107 });
108
109 const newSE = world2LatLng({
110 x: middle.x + halfW,
111 y: middle.y + halfH,
112 });
113
114 return {
115 center: world2LatLng(middle),
116 zoom,
117 newBounds: {
118 nw: newNW,
119 se: newSE,
120 },
121 };
122}
123
124export function convertNeSwToNwSe({ ne, sw }) {
125 return {

Callers 1

fitBoundsFunction · 0.85

Calls 2

latLng2WorldFunction · 0.85
world2LatLngFunction · 0.85

Tested by

no test coverage detected