-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapbox_practice.html
More file actions
95 lines (84 loc) · 2.94 KB
/
mapbox_practice.html
File metadata and controls
95 lines (84 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>First Mapbox Map</title>
<!-- Mapbox CSS -->
<meta name='viewport' content='width=device-width, initial-scale=1' />
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v2.9.1/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v2.9.1/mapbox-gl.css' rel='stylesheet' />
<!-- Custom CSS -->
<style>
#map {
/* the width and height may be set to any size */
width: 100%;
height: 700px;
}
</style>
</head>
<body>
<h1>My First Mapbox Map!</h1>
<!-- The HTML element that serves as the Mapbox container -->
<div id='map'></div>
<button id="zoom-in">+</button>
<button id="zoom-out">-</button>
<button id="drop-marker">add marker</button>
<!-- Mapbox JS -->
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v2.9.1/mapbox-gl.js'></script>
<script src="js/keys.js"></script>
<!-- Custom JS -->
<script>
(async function() {
mapboxgl.accessToken = MAP_BOX_API_KEY;
const map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v11',
zoom: 10,
center: [-98.4916, 29.4252],
projection: 'globe'
});
// when the zoom in button is clicked, decrease the maps zoom level
const zoomIn = document.querySelector("#zoom-in")
zoomIn.addEventListener("click", function() {
let currentZoom = map.getZoom();
currentZoom++;
map.setZoom(currentZoom);
});
//
const zoomOut = document.querySelector("#zoom-out")
zoomOut.addEventListener("click", function() {
let currentZoom = map.getZoom();
currentZoom--;
map.setZoom(currentZoom);
})
document.querySelector("#drop-marker").addEventListener("click", function(event) {
let marker = new mapboxgl.Marker();
marker.setLngLat([-100, 30])
marker.addTo(map)
//add a popup to this
let popup = new mapboxgl.Popup()
.setLngLat([-98.489615, 29.426827])
.setHTML("<p>Codeup Rocks!</p>")
.addTo(map)
})
function getLatLngFromAddress(address, token = MAP_BOX_API_KEY) {
let baseUrl = 'https://api.mapbox.com';
let endPoint = '/geocoding/v5/mapbox.places/';
return fetch(baseUrl + endPoint + encodeURIComponent(address) + '.json' + "?" + 'access_token=' + token)
.then(function(res) {
return res.json();
// to get all the data from the request, comment out the following three lines...
}).then(function(data) {
// console.log(data)
return data.features[0].center;
});
}
let coords = await getLatLngFromAddress("Belton, TX")
console.log(coords);
const belton = new mapboxgl.Marker();
belton.setLngLat(coords);
belton.addTo(map);
})()
</script>
</body>
</html>