Skip to content

Commit 35f09d6

Browse files
authored
feat: Migrates 3 data layer samples (#807)
* feat: Migrates data layer samples. Change-Id: Ic8e945adfbc592ae242d8055a394ac8a04e24994 * Incorporate review comments * Incorporate review comments Refactor earthquake data handling to store data for later use if innerMap is not initialized. * Rename map CSS selector to gmp-map * Incorporate review comments * Incorporate review comments Refactor earthquake data handling to store data for later use if the map is not initialized. * Rename #map to gmp-map in style.css * Incorporate review comments * Rename CSS selector from #map to gmp-map * Refactor eqfeed_callback to use window scope * Refactor eqfeed_callback to use window object * Change script type to module for quakes.geo.json * Update index.html * Update index.ts * Refactor earthquake data handling and callback function * Remove earthquake data loading script Removed the script tag that loads earthquake data in JSONP format from the USGS feed.
1 parent 0605920 commit 35f09d6

File tree

22 files changed

+2672
-3
lines changed

22 files changed

+2672
-3
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Google Maps JavaScript Sample
2+
3+
This sample is generated from @googlemaps/js-samples located at
4+
https://github.com/googlemaps-samples/js-api-samples.
5+
6+
## Setup
7+
8+
### Before starting run:
9+
10+
`npm i`
11+
12+
### Run an example on a local web server
13+
14+
`cd samples/layer-data-quakes-red`
15+
`npm start`
16+
17+
### Build an individual example
18+
19+
`cd samples/layer-data-quakes-red`
20+
`npm run build`
21+
22+
From 'samples':
23+
24+
`npm run build --workspace=layer-data-quakes-red/`
25+
26+
### Build all of the examples.
27+
28+
From 'samples':
29+
30+
`npm run build-all`
31+
32+
### Run lint to check for problems
33+
34+
`cd samples/layer-data-quakes-red`
35+
`npx eslint index.ts`
36+
37+
## Feedback
38+
39+
For feedback related to this sample, please open a new issue on
40+
[GitHub](https://github.com/googlemaps-samples/js-api-samples/issues).
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!doctype html>
2+
<!--
3+
@license
4+
Copyright 2019 Google LLC. All Rights Reserved.
5+
SPDX-License-Identifier: Apache-2.0
6+
-->
7+
<!-- [START maps_layer_data_quakes_red] -->
8+
<html>
9+
<head>
10+
<title>Simple Data Layer: Earthquakes</title>
11+
12+
<link rel="stylesheet" type="text/css" href="./style.css" />
13+
<script type="module" src="./index.js"></script>
14+
15+
<!-- prettier-ignore -->
16+
<script>(g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})
17+
({key: "AIzaSyA6myHzS10YXdcazAFalmXvDkrYCp5cLc8", v: "weekly"});</script>
18+
</head>
19+
<body>
20+
<gmp-map center="20,-160" zoom="2"></gmp-map>
21+
</body>
22+
</html>
23+
<!-- [END maps_layer_data_quakes_red] -->
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* @license
3+
* Copyright 2019 Google LLC. All Rights Reserved.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
// [START maps_layer_data_quakes_red]
8+
let innerMap;
9+
10+
async function initMap() {
11+
(await google.maps.importLibrary("maps")) as google.maps.MapsLibrary;
12+
13+
const mapElement = document.querySelector(
14+
"gmp-map"
15+
) as google.maps.MapElement;
16+
17+
innerMap = mapElement.innerMap;
18+
19+
// Get the earthquake data (JSONP format)
20+
// This feed is a copy from the USGS feed, you can find the originals here:
21+
// http://earthquake.usgs.gov/earthquakes/feed/v1.0/geojson.php
22+
const script = document.createElement("script");
23+
24+
script.setAttribute(
25+
"src",
26+
"quakes.geo.json"
27+
);
28+
29+
document.getElementsByTagName("head")[0].appendChild(script);
30+
31+
// Add a basic style.
32+
innerMap.data.setStyle((feature) => {
33+
const mag = Math.exp(parseFloat(feature.getProperty("mag") as string)) * 0.1;
34+
return /** @type {google.maps.Data.StyleOptions} */ {
35+
icon: {
36+
path: google.maps.SymbolPath.CIRCLE,
37+
scale: mag,
38+
fillColor: "#f00",
39+
fillOpacity: 0.35,
40+
strokeWeight: 0,
41+
},
42+
};
43+
});
44+
}
45+
46+
// Defines the callback function referenced in the jsonp file.
47+
function eqfeed_callback(data: any) {
48+
innerMap.data.addGeoJson(data);
49+
}
50+
51+
window.eqfeed_callback = eqfeed_callback;
52+
initMap();
53+
// [END maps_layer_data_quakes_red]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "@js-api-samples/layer-data-quakes-red",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"build": "tsc && bash ../jsfiddle.sh layer-data-quakes-red && bash ../app.sh layer-data-quakes-red && bash ../docs.sh layer-data-quakes-red && npm run build:vite --workspace=. && bash ../dist.sh layer-data-quakes-red",
6+
"test": "tsc && npm run build:vite --workspace=.",
7+
"start": "tsc && vite build --base './' && vite",
8+
"build:vite": "vite build --base './'",
9+
"preview": "vite preview"
10+
},
11+
"dependencies": {
12+
13+
}
14+
}

samples/layer-data-quakes-red/public/quakes.geo.json

Lines changed: 1021 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @license
3+
* Copyright 2019 Google LLC. All Rights Reserved.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
/* [START maps_layer_data_quakes_red] */
7+
/*
8+
* Always set the map height explicitly to define the size of the div element
9+
* that contains the map.
10+
*/
11+
gmp-map {
12+
height: 100%;
13+
}
14+
15+
/*
16+
* Optional: Makes the sample page fill the window.
17+
*/
18+
html,
19+
body {
20+
height: 100%;
21+
margin: 0;
22+
padding: 0;
23+
}
24+
25+
/* [END maps_layer_data_quakes_red] */
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"compilerOptions": {
3+
"module": "esnext",
4+
"target": "esnext",
5+
"strict": true,
6+
"noImplicitAny": false,
7+
"lib": [
8+
"es2015",
9+
"esnext",
10+
"es6",
11+
"dom",
12+
"dom.iterable"
13+
],
14+
"moduleResolution": "Node",
15+
"jsx": "preserve"
16+
}
17+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Google Maps JavaScript Sample
2+
3+
This sample is generated from @googlemaps/js-samples located at
4+
https://github.com/googlemaps-samples/js-api-samples.
5+
6+
## Setup
7+
8+
### Before starting run:
9+
10+
`npm i`
11+
12+
### Run an example on a local web server
13+
14+
`cd samples/layer-data-quakes-simple`
15+
`npm start`
16+
17+
### Build an individual example
18+
19+
`cd samples/layer-data-quakes-simple`
20+
`npm run build`
21+
22+
From 'samples':
23+
24+
`npm run build --workspace=layer-data-quakes-simple/`
25+
26+
### Build all of the examples.
27+
28+
From 'samples':
29+
30+
`npm run build-all`
31+
32+
### Run lint to check for problems
33+
34+
`cd samples/layer-data-quakes-simple`
35+
`npx eslint index.ts`
36+
37+
## Feedback
38+
39+
For feedback related to this sample, please open a new issue on
40+
[GitHub](https://github.com/googlemaps-samples/js-api-samples/issues).
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!DOCTYPE html>
2+
<!--
3+
@license
4+
Copyright 2019 Google LLC. All Rights Reserved.
5+
SPDX-License-Identifier: Apache-2.0
6+
-->
7+
<!-- [START maps_layer_data_quakes_simple] -->
8+
<html>
9+
<head>
10+
<title>Default Data Layer: Earthquakes</title>
11+
12+
<link rel="stylesheet" type="text/css" href="./style.css" />
13+
<script type="module" src="./index.js"></script>
14+
15+
<!-- prettier-ignore -->
16+
<script>(g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})
17+
({key: "AIzaSyA6myHzS10YXdcazAFalmXvDkrYCp5cLc8", v: "weekly"});</script>
18+
</head>
19+
<body>
20+
<gmp-map center="20,-160" zoom="3"></gmp-map>
21+
</body>
22+
</html>
23+
<!-- [END maps_layer_data_quakes_simple] -->
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @license
3+
* Copyright 2019 Google LLC. All Rights Reserved.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
// [START maps_layer_data_quakes_simple]
8+
let innerMap;
9+
let earthquakeData;
10+
11+
async function initMap() {
12+
(await google.maps.importLibrary("maps")) as google.maps.MapsLibrary;
13+
14+
const mapElement = document.querySelector(
15+
"gmp-map"
16+
) as google.maps.MapElement;
17+
18+
innerMap = mapElement.innerMap;
19+
20+
// Get the earthquake data (JSONP format)
21+
// This feed is a copy from the USGS feed, you can find the originals here:
22+
// http://earthquake.usgs.gov/earthquakes/feed/v1.0/geojson.php
23+
const script = document.createElement("script");
24+
25+
script.setAttribute(
26+
"src",
27+
"quakes.geo.json"
28+
);
29+
30+
document.getElementsByTagName("head")[0].appendChild(script);
31+
}
32+
33+
// Defines the callback function referenced in the jsonp file.
34+
function eqfeed_callback(data: any) {
35+
innerMap.data.addGeoJson(data);
36+
}
37+
38+
window.eqfeed_callback = eqfeed_callback;
39+
initMap();
40+
// [END maps_layer_data_quakes_simple]

0 commit comments

Comments
 (0)