Learning basic JavaScript though the use of popular map JS API
base maps
functionality
base maps
functionality
JavaScript are scripts - sets of instructions
You still need to tell the HTML where to find these scripts and how to interpret them.
You can include the script in the HTML
or in separate files
control the map!
Add a point
<!DOCTYPE html>
<html>
<head>
<style>
#map {
height: 400px;
width: 100%;
}
</style>
</head>
<body>
<h3>Hello Tacoma</h3>
<div id="map"></div>
<script>
function initMap() {
var Tacoma = {lat: 47.325, lng: -122.46};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 11,
center: Tacoma
});
var marker = new google.maps.Marker({
position: Tacoma,
map: map
});
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBn3zquptShb1gU_Esyo4EAwJa7DDvDeto&callback=initMap">
</script>
</body>
</html>
Same thing,
slightly different code.
What are the differences?
<!DOCTYPE html>
<html>
<head>
<title>Simple Map</title>
<meta name="viewport" content="initial-scale=1.0">
<meta charset="utf-8">
<style>
/* 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;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 47.225, lng: -122.46},
zoom: 8
});
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBn3zquptShb1gU_Esyo4EAwJa7DDvDeto&callback=initMap"
async defer></script>
</body>
</html>
google.maps.MapTypeId.TERRAIN
Inside the google.maps class
Test it out!
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title></title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.41.0/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.41.0/mapbox-gl.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>
<div id='map'></div>
<script>
mapboxgl.accessToken = 'pk.eyJ1IjoiYnJpY2tlciIsImEiOiJULVRnSlZZIn0.LHt8a4ByC-_b4ytgeh7H5Q';
var map = new mapboxgl.Map({
container: 'map', // container id
style: 'mapbox://styles/mapbox/satellite-streets-v9', // basemap style and stylesheet location
center: [-122.46, 47.2587], // starting position for the map [lng, lat] - Hi Tacoma!
zoom: 9 // starting zoom
});
</script>
</body>
</html>
Text
Add a geoJSON file to the server
and add a link to it