-
Notifications
You must be signed in to change notification settings - Fork 817
/
index.js
62 lines (56 loc) · 1.92 KB
/
index.js
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
/**
* @license
* Copyright 2019 Google LLC. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
// [START maps_map_coordinates]
function initMap() {
const chicago = new google.maps.LatLng(41.85, -87.65);
const map = new google.maps.Map(document.getElementById("map"), {
center: chicago,
zoom: 3,
});
const coordInfoWindow = new google.maps.InfoWindow();
coordInfoWindow.setContent(createInfoWindowContent(chicago, map.getZoom()));
coordInfoWindow.setPosition(chicago);
coordInfoWindow.open(map);
map.addListener("zoom_changed", () => {
coordInfoWindow.setContent(createInfoWindowContent(chicago, map.getZoom()));
coordInfoWindow.open(map);
});
}
const TILE_SIZE = 256;
function createInfoWindowContent(latLng, zoom) {
const scale = 1 << zoom;
const worldCoordinate = project(latLng);
const pixelCoordinate = new google.maps.Point(
Math.floor(worldCoordinate.x * scale),
Math.floor(worldCoordinate.y * scale),
);
const tileCoordinate = new google.maps.Point(
Math.floor((worldCoordinate.x * scale) / TILE_SIZE),
Math.floor((worldCoordinate.y * scale) / TILE_SIZE),
);
return [
"Chicago, IL",
"LatLng: " + latLng,
"Zoom level: " + zoom,
"World Coordinate: " + worldCoordinate,
"Pixel Coordinate: " + pixelCoordinate,
"Tile Coordinate: " + tileCoordinate,
].join("<br>");
}
// The mapping between latitude, longitude and pixels is defined by the web
// mercator projection.
function project(latLng) {
let siny = Math.sin((latLng.lat() * Math.PI) / 180);
// Truncating to 0.9999 effectively limits latitude to 89.189. This is
// about a third of a tile past the edge of the world tile.
siny = Math.min(Math.max(siny, -0.9999), 0.9999);
return new google.maps.Point(
TILE_SIZE * (0.5 + latLng.lng() / 360),
TILE_SIZE * (0.5 - Math.log((1 + siny) / (1 - siny)) / (4 * Math.PI)),
);
}
window.initMap = initMap;
// [END maps_map_coordinates]