|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2019 Google LLC. All Rights Reserved. |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +// [START maps_map_projection_simple] |
| 8 | +// This example defines an image map type using the Gall-Peters |
| 9 | +// projection. |
| 10 | +// https://en.wikipedia.org/wiki/Gall%E2%80%93Peters_projection |
| 11 | +const mapElement = document.querySelector("gmp-map") as google.maps.MapElement; |
| 12 | +let innerMap; |
| 13 | + |
| 14 | +async function initMap() { |
| 15 | + // Request the needed libraries. |
| 16 | + await google.maps.importLibrary("maps"); |
| 17 | + |
| 18 | + // Create a map. |
| 19 | + innerMap = mapElement.innerMap; |
| 20 | + innerMap.setOptions({ |
| 21 | + mapTypeControl: false, |
| 22 | + }); |
| 23 | + |
| 24 | + // Set the Gall-Peters map type. |
| 25 | + initGallPeters(); |
| 26 | + innerMap.mapTypes.set("gallPeters", gallPetersMapType); |
| 27 | + innerMap.setMapTypeId("gallPeters"); |
| 28 | + |
| 29 | + // Show the lat and lng under the mouse cursor. |
| 30 | + const coordsDiv = document.getElementById("coords") as HTMLElement; |
| 31 | + |
| 32 | + innerMap.addListener("mousemove", (event: google.maps.MapMouseEvent) => { |
| 33 | + coordsDiv.textContent = |
| 34 | + "lat: " + |
| 35 | + Math.round(event.latLng!.lat()) + |
| 36 | + ", " + |
| 37 | + "lng: " + |
| 38 | + Math.round(event.latLng!.lng()); |
| 39 | + }); |
| 40 | + |
| 41 | + // Add some markers to the map. |
| 42 | + innerMap.data.setStyle((feature) => { |
| 43 | + return { |
| 44 | + title: feature.getProperty("name") as string, |
| 45 | + optimized: false, |
| 46 | + }; |
| 47 | + }); |
| 48 | + innerMap.data.addGeoJson(cities); |
| 49 | +} |
| 50 | + |
| 51 | +let gallPetersMapType; |
| 52 | + |
| 53 | +function initGallPeters() { |
| 54 | + const GALL_PETERS_RANGE_X = 800; |
| 55 | + const GALL_PETERS_RANGE_Y = 512; |
| 56 | + |
| 57 | + // Fetch Gall-Peters tiles stored locally on our server. |
| 58 | + gallPetersMapType = new google.maps.ImageMapType({ |
| 59 | + getTileUrl: function (coord, zoom) { |
| 60 | + const scale = 1 << zoom; |
| 61 | + |
| 62 | + // Wrap tiles horizontally. |
| 63 | + const x = ((coord.x % scale) + scale) % scale; |
| 64 | + |
| 65 | + // Don't wrap tiles vertically. |
| 66 | + const y = coord.y; |
| 67 | + |
| 68 | + if (y < 0 || y >= scale) return ""; |
| 69 | + |
| 70 | + return ( |
| 71 | + "gall-peters_" + |
| 72 | + zoom + |
| 73 | + "_" + |
| 74 | + x + |
| 75 | + "_" + |
| 76 | + y + |
| 77 | + ".png" |
| 78 | + ); |
| 79 | + }, |
| 80 | + tileSize: new google.maps.Size(GALL_PETERS_RANGE_X, GALL_PETERS_RANGE_Y), |
| 81 | + minZoom: 0, |
| 82 | + maxZoom: 1, |
| 83 | + name: "Gall-Peters", |
| 84 | + }); |
| 85 | + |
| 86 | + // Describe the Gall-Peters projection used by these tiles. |
| 87 | + gallPetersMapType.projection = { |
| 88 | + fromLatLngToPoint: function (latLng) { |
| 89 | + const latRadians = (latLng.lat() * Math.PI) / 180; |
| 90 | + return new google.maps.Point( |
| 91 | + GALL_PETERS_RANGE_X * (0.5 + latLng.lng() / 360), |
| 92 | + GALL_PETERS_RANGE_Y * (0.5 - 0.5 * Math.sin(latRadians)) |
| 93 | + ); |
| 94 | + }, |
| 95 | + fromPointToLatLng: function (point, noWrap) { |
| 96 | + const x = point.x / GALL_PETERS_RANGE_X; |
| 97 | + const y = Math.max(0, Math.min(1, point.y / GALL_PETERS_RANGE_Y)); |
| 98 | + |
| 99 | + return new google.maps.LatLng( |
| 100 | + (Math.asin(1 - 2 * y) * 180) / Math.PI, |
| 101 | + -180 + 360 * x, |
| 102 | + noWrap |
| 103 | + ); |
| 104 | + }, |
| 105 | + }; |
| 106 | +} |
| 107 | + |
| 108 | +// GeoJSON, describing the locations and names of some cities. |
| 109 | +const cities = { |
| 110 | + type: "FeatureCollection", |
| 111 | + features: [ |
| 112 | + { |
| 113 | + type: "Feature", |
| 114 | + geometry: { type: "Point", coordinates: [-87.65, 41.85] }, |
| 115 | + properties: { name: "Chicago" }, |
| 116 | + }, |
| 117 | + { |
| 118 | + type: "Feature", |
| 119 | + geometry: { type: "Point", coordinates: [-149.9, 61.218] }, |
| 120 | + properties: { name: "Anchorage" }, |
| 121 | + }, |
| 122 | + { |
| 123 | + type: "Feature", |
| 124 | + geometry: { type: "Point", coordinates: [-99.127, 19.427] }, |
| 125 | + properties: { name: "Mexico City" }, |
| 126 | + }, |
| 127 | + { |
| 128 | + type: "Feature", |
| 129 | + geometry: { type: "Point", coordinates: [-0.126, 51.5] }, |
| 130 | + properties: { name: "London" }, |
| 131 | + }, |
| 132 | + { |
| 133 | + type: "Feature", |
| 134 | + geometry: { type: "Point", coordinates: [28.045, -26.201] }, |
| 135 | + properties: { name: "Johannesburg" }, |
| 136 | + }, |
| 137 | + { |
| 138 | + type: "Feature", |
| 139 | + geometry: { type: "Point", coordinates: [15.322, -4.325] }, |
| 140 | + properties: { name: "Kinshasa" }, |
| 141 | + }, |
| 142 | + { |
| 143 | + type: "Feature", |
| 144 | + geometry: { type: "Point", coordinates: [151.207, -33.867] }, |
| 145 | + properties: { name: "Sydney" }, |
| 146 | + }, |
| 147 | + { |
| 148 | + type: "Feature", |
| 149 | + geometry: { type: "Point", coordinates: [0, 0] }, |
| 150 | + properties: { name: "0°N 0°E" }, |
| 151 | + }, |
| 152 | + ], |
| 153 | +}; |
| 154 | + |
| 155 | +initMap(); |
| 156 | +// [END maps_map_projection_simple] |
0 commit comments