map api javascript code example
Example 1: google maps api javascript
<!DOCTYPE html>
<html>
<head>
<title>Simple Map</title>
<meta name="viewport" content="initial-scale=1.0">
<meta charset="utf-8">
<style>
#map {
height: 100%;
}
html, body {
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
zoom: 8
});
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap"
async defer></script>
</body>
</html>
Example 2: map javascript
function each(collection, action) {
if (Array.isArray(collection)) {
for (var i = 0; i < collection.length; i++) {
action(collection[i], i, collection);
}
} else {
for (var key in collection) {
action(collection[key], key, collection);
}
}
}
Example 3: map javascript
var miMapa = new Map();
var claveObj = {},
claveFunc = function () {},
claveCadena = "una cadena";
miMapa.set(claveCadena, "valor asociado con 'una cadena'");
miMapa.set(claveObj, "valor asociado con claveObj");
miMapa.set(claveFunc, "valor asociado with claveFunc");
miMapa.size;
miMapa.get(claveCadena);
miMapa.get(claveObj);
miMapa.get(claveFunc);
miMapa.get("una cadena");
miMapa.get({});
miMapa.get(function() {})
var myMap = new Map();
myMap.set("bar", "foo");
myMap.delete("bar");
myMap.has("bar");
Example 4: map javascript
var kvArray = [["clave1", "valor1"], ["clave2", "valor2"]];
var miMapa = new Map(kvArray);
miMapa.get("clave1");
console.log(Array.from(miMapa));
console.log(Array.from(miMapa.keys()));
Example 5: map javascript
miMapa.forEach(function(valor, clave) {
console.log(clave + ' = ' + valor);
});