<div id="map"></div>
<!-- Habría que cambiar la API Key por la nuestra -->
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initMap">
</script>
CSS
/* Siempre establecemos la altura del mapa explícitamente para definir el tamaño del elemento div que contiene el mapa. */
#map {
height: 100%;
}
/* Opcionalmente se puede establecer que se vea a pantalla completa */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
JavaScript
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 2,
center: new google.maps.LatLng(2.8,-187.3),
mapTypeId: 'hybrid'
});
// Creamos una etiqueta <script> y establecemos la URL del USGS mediante el atributo src como fuente de datos, en formato GeoJSON
// Con el método
var script = document.createElement('script');
script.src = 'https://developers.google.com/maps/documentation/javascript/examples/json/earthquake_GeoJSONP.js';
document.getElementsByTagName('head')[0].appendChild(script);
};
// Mediante un bucle se recorren los resultados del array de datos GeoJSON y se ubica cada marcador en cada set de coordenadas
window.eqfeed_callback = function(resultados) {
for (var i = 0; i < resultados.features.length; i++) {
var coords = resultados.features[i].geometry.coordinates;
var latLng = new google.maps.LatLng(coords[1],coords[0]);
var marker = new google.maps.Marker({
position: latLng,
map: map
});
};
/* Explicación de la función:
- Creamos una función con un parámetro (resultados), que es la variable que va a recibir los valores
- Inicializamos el bucle con 0, para que vaya al primer ítem del array (recordamos que los array son de base cero)
- Le indicamos como condición que siga recorriendo el array (i++) mientras i sea menor que el número de features del fichero GeoJSON (i<resultados.features.lenght)
- Mientras funcione el bucle:
. la var coords mandará a la función el valor que encuentre, para cada item del array (features [i]) los valores de geometry.coordinates
. la var latLng creará un nuevo valor de las coordenadas (1 y 0, Lat Long)
. la var marker creará un nuevo marcador con los valores latLng en el mapa
Con ello irán apareciendo en el mapa todos los marcadores
*/
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.