letmap:google.maps.Map;functioninitMap():void{map=newgoogle.maps.Map(document.getElementById("map")asHTMLElement,{zoom:2,center:newgoogle.maps.LatLng(2.8,-187.3),mapTypeId:"terrain",});// Create a <script> tag and set the USGS URL as the source.constscript=document.createElement("script");// This example uses a local copy of the GeoJSON stored at// http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_week.geojsonpscript.src="https://developers.google.com/maps/documentation/javascript/examples/json/earthquake_GeoJSONP.js";document.getElementsByTagName("head")[0].appendChild(script);}// Loop through the results array and place a marker for each// set of coordinates.consteqfeed_callback=function(results:any){for(leti=0;i < results.features.length;i++){constcoords=results.features[i].geometry.coordinates;constlatLng=newgoogle.maps.LatLng(coords[1],coords[0]);newgoogle.maps.Marker({position:latLng,map:map,});}};declareglobal{interfaceWindow{initMap:()=>void;eqfeed_callback:(results:any)=>void;}}window.initMap=initMap;window.eqfeed_callback=eqfeed_callback;
letmap;functioninitMap(){map=newgoogle.maps.Map(document.getElementById("map"),{zoom:2,center:newgoogle.maps.LatLng(2.8,-187.3),mapTypeId:"terrain",});// Create a <script> tag and set the USGS URL as the source.constscript=document.createElement("script");// This example uses a local copy of the GeoJSON stored at// http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_week.geojsonpscript.src="https://developers.google.com/maps/documentation/javascript/examples/json/earthquake_GeoJSONP.js";document.getElementsByTagName("head")[0].appendChild(script);}// Loop through the results array and place a marker for each// set of coordinates.consteqfeed_callback=function(results){for(leti=0;i < results.features.length;i++){constcoords=results.features[i].geometry.coordinates;constlatLng=newgoogle.maps.LatLng(coords[1],coords[0]);newgoogle.maps.Marker({position:latLng,map:map,});}};window.initMap=initMap;window.eqfeed_callback=eqfeed_callback;
/* * Always set the map height explicitly to define the size of the div element * that contains the map. */#map{height:100%;}/* * Optional: Makes the sample page fill the window. */html,body{height:100%;margin:0;padding:0;}
<html>
<head>
<title>Earthquake Markers</title>
<link rel="stylesheet" type="text/css" href="./style.css" />
<script type="module" src="./index.js"></script>
</head>
<body>
<div id="map"></div>
<!--
The `defer` attribute causes the script to execute after the full HTML
document has been parsed. For non-blocking uses, avoiding race conditions,
and consistent behavior across browsers, consider loading using Promises. See
https://developers.google.com/maps/documentation/javascript/load-maps-js-api
for more information.
-->
<script
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg&callback=initMap&v=weekly"
defer
></script>
</body>
</html>
[null,null,["上次更新時間:2025-08-27 (世界標準時間)。"],[[["\u003cp\u003eThis guide explains how to import and display GeoJSON data on Google Maps using JavaScript or TypeScript.\u003c/p\u003e\n"],["\u003cp\u003eYou can load GeoJSON data from your domain or a cross-origin domain that supports CORS.\u003c/p\u003e\n"],["\u003cp\u003eIf the remote domain only supports JSONP, you can use a script tag to fetch the data, but this approach has security risks and CORS is preferred if available.\u003c/p\u003e\n"],["\u003cp\u003eOnce loaded, GeoJSON data can be styled and customized using the Google Maps Data Layer.\u003c/p\u003e\n"],["\u003cp\u003eThis tutorial provides code examples and links to further resources for working with GeoJSON and Google Maps.\u003c/p\u003e\n"]]],[],null,["# Import GeoJSON Data into Maps\n\nOverview\n--------\n\nLearn how to import [GeoJSON data](#data) from either a local or remote\nsource, and display it on your map. This tutorial uses the map below to\nillustrate various techniques to import data into maps.\n\n\nThe section below displays the entire code you need to create the map in this\ntutorial.\n| **Tip:** Check out the [store location solutions](/maps/solutions/store-locator) to see another example of using GeoJSON data with maps.\n\n\n### TypeScript\n\n```typescript\nlet map: google.maps.Map;\n\nfunction initMap(): void {\n map = new google.maps.Map(document.getElementById(\"map\") as HTMLElement, {\n zoom: 2,\n center: new google.maps.LatLng(2.8, -187.3),\n mapTypeId: \"terrain\",\n });\n\n // Create a \u003cscript\u003e tag and set the USGS URL as the source.\n const script = document.createElement(\"script\");\n\n // This example uses a local copy of the GeoJSON stored at\n // http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_week.geojsonp\n script.src =\n \"https://developers.google.com/maps/documentation/javascript/examples/json/earthquake_GeoJSONP.js\";\n document.getElementsByTagName(\"head\")[0].appendChild(script);\n}\n\n// Loop through the results array and place a marker for each\n// set of coordinates.\nconst eqfeed_callback = function (results: any) {\n for (let i = 0; i \u003c results.features.length; i++) {\n const coords = results.features[i].geometry.coordinates;\n const latLng = new google.maps.LatLng(coords[1], coords[0]);\n\n new google.maps.Marker({\n position: latLng,\n map: map,\n });\n }\n};\n\ndeclare global {\n interface Window {\n initMap: () =\u003e void;\n eqfeed_callback: (results: any) =\u003e void;\n }\n}\nwindow.initMap = initMap;\nwindow.eqfeed_callback = eqfeed_callback;https://github.com/googlemaps/js-samples/blob/2683f7366fb27829401945d2a7e27d77ed2df8e5/samples/earthquake-markers/index.ts#L8-L48\n```\n| **Note:** Read the [guide](/maps/documentation/javascript/using-typescript) on using TypeScript and Google Maps.\n\n### JavaScript\n\n```javascript\nlet map;\n\nfunction initMap() {\n map = new google.maps.Map(document.getElementById(\"map\"), {\n zoom: 2,\n center: new google.maps.LatLng(2.8, -187.3),\n mapTypeId: \"terrain\",\n });\n\n // Create a \u003cscript\u003e tag and set the USGS URL as the source.\n const script = document.createElement(\"script\");\n\n // This example uses a local copy of the GeoJSON stored at\n // http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_week.geojsonp\n script.src =\n \"https://developers.google.com/maps/documentation/javascript/examples/json/earthquake_GeoJSONP.js\";\n document.getElementsByTagName(\"head\")[0].appendChild(script);\n}\n\n// Loop through the results array and place a marker for each\n// set of coordinates.\nconst eqfeed_callback = function (results) {\n for (let i = 0; i \u003c results.features.length; i++) {\n const coords = results.features[i].geometry.coordinates;\n const latLng = new google.maps.LatLng(coords[1], coords[0]);\n\n new google.maps.Marker({\n position: latLng,\n map: map,\n });\n }\n};\n\nwindow.initMap = initMap;\nwindow.eqfeed_callback = eqfeed_callback;https://github.com/googlemaps/js-samples/blob/2683f7366fb27829401945d2a7e27d77ed2df8e5/dist/samples/earthquake-markers/docs/index.js#L7-L41\n```\n| **Note:** The JavaScript is compiled from the TypeScript snippet.\n\n### CSS\n\n```css\n/* \n * Always set the map height explicitly to define the size of the div element\n * that contains the map. \n */\n#map {\n height: 100%;\n}\n\n/* \n * Optional: Makes the sample page fill the window. \n */\nhtml,\nbody {\n height: 100%;\n margin: 0;\n padding: 0;\n}\nhttps://github.com/googlemaps/js-samples/blob/2683f7366fb27829401945d2a7e27d77ed2df8e5/dist/samples/earthquake-markers/docs/style.css#L7-L24\n```\n\n### HTML\n\n```html\n\u003chtml\u003e\n \u003chead\u003e\n \u003ctitle\u003eEarthquake Markers\u003c/title\u003e\n\n \u003clink rel=\"stylesheet\" type=\"text/css\" href=\"./style.css\" /\u003e\n \u003cscript type=\"module\" src=\"./index.js\"\u003e\u003c/script\u003e\n \u003c/head\u003e\n \u003cbody\u003e\n \u003cdiv id=\"map\"\u003e\u003c/div\u003e\n\n \u003c!-- \n The `defer` attribute causes the script to execute after the full HTML\n document has been parsed. For non-blocking uses, avoiding race conditions,\n and consistent behavior across browsers, consider loading using Promises. See\n https://developers.google.com/maps/documentation/javascript/load-maps-js-api\n for more information.\n --\u003e\n \u003cscript\n src=\"https://maps.googleapis.com/maps/api/js?key=AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg&callback=initMap&v=weekly\"\n defer\n \u003e\u003c/script\u003e\n \u003c/body\u003e\n\u003c/html\u003ehttps://github.com/googlemaps/js-samples/blob/2683f7366fb27829401945d2a7e27d77ed2df8e5/dist/samples/earthquake-markers/docs/index.html#L8-L30\n```\n\n### Try Sample\n\n[JSFiddle.net](https://jsfiddle.net/gh/get/library/pure/googlemaps/js-samples/tree/master/dist/samples/earthquake-markers/jsfiddle) [Google Cloud Shell](https://ssh.cloud.google.com/cloudshell/editor?cloudshell_git_repo=https%3A%2F%2Fgithub.com%2Fgooglemaps%2Fjs-samples&cloudshell_git_branch=sample-earthquake-markers&cloudshell_tutorial=cloud_shell_instructions.md&cloudshell_workspace=.)\n\n\u003cbr /\u003e\n\nLoading data\n------------\n\nThis section shows you how to load data from either the same domain as your\nMaps JavaScript API application, or from a different one.\n\n### Loading data from the same domain\n\nThe [Google Maps Data Layer](/maps/documentation/javascript/datalayer)\nprovides a container for arbitrary geospatial data\n(including GeoJSON). If your data is in a file hosted on the same domain\nas your Maps JavaScript API application, you can load it\nusing the `map.data.loadGeoJson()` method. The file must be on the same domain,\nbut you can host it in a different\nsubdomain. For example, you can make a request to **files.example.com** from\n**www.example.com**. \n\n map.data.loadGeoJson('data.json');\n\n### Loading data across domains\n\nYou can also request data from a domain other than your own, if the domain's\nconfiguration allows such a request. The standard for this permission is\ncalled\n[Cross-origin resource sharing](http://en.wikipedia.org/wiki/Cross-origin_resource_sharing)\n(CORS). If a domain has allowed cross-domain requests, its response header\nshould include the following declaration: \n\n Access-Control-Allow-Origin: *\n\nUse the\n[Chrome Developer Tools (DevTools)](https://developer.chrome.com/devtools#console)\nto find out if a domain has enabled CORS.\n\nLoading data from such a domain is the same as\nloading JSON from the same domain: \n\n map.data.loadGeoJson('http://www.CORS-ENABLED-SITE.com/data.json');\n\nRequesting JSONP\n----------------\n\nThe target domain must support requests for [JSONP](#JSONP) in order to use\nthis technique.\n| **Requesting JSONP from domains outside of your control is very\n| risky.**\n|\n| Since your browser loads any code that returns as a script, you should only\n| request JSONP from a domain that you trust. In general, JSONP is replaced by\n| [CORS](#loading_data_across_domains); the latter is\n| much safer and should be your preferred choice if both are available.\n\nTo request JSONP, use `createElement()` to add a `script` tag\nto the head of your document. \n\n var script = document.createElement('script');\n script.src = 'http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_week.geojsonp';\n document.getElementsByTagName('head')[0].appendChild(script);\n\nWhen the script runs, the target domain passes the data as an argument\nto *another* script, usually named `callback()`. The target domain defines the\ncallback script name, which is the first name on the page when you load the\ntarget URL in a browser.\n\nFor example, load\n\u003chttp://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_week.geojsonp\u003e\nin your browser window to reveal the callback name as `eqfeed_callback`.\n\nYou must define the callback script in your code: \n\n function eqfeed_callback(response) {\n map.data.addGeoJson(response);\n }\n\nUse the `addGeoJson()` method to place the parsed GeoJSON data on the map.\n\nStyling the data\n----------------\n\nYou can change the appearance of your data by adding GeoJSON data to a Map\nobject.\nRead the developer's guide for more information on\n[styling your data](/maps/documentation/javascript/datalayer#style_geojson_data).\n\nLearn more\n----------\n\n- GeoJSON is a widely used open format for encoding geographic data, based on JSON (JavaScript Object Notation). JavaScript tools and methods designed for JSON data also work with GeoJSON. Read the [developer's guide](/maps/documentation/javascript/datalayer#load_geojson) for more information.\n- JSONP stands for *padded* JSON. It is a communication method used in JavaScript programs that run in web browsers, to request data from a server in a different domain."]]