-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapbox-2.html
More file actions
114 lines (94 loc) · 3.75 KB
/
mapbox-2.html
File metadata and controls
114 lines (94 loc) · 3.75 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<!--<!–// TODO: Create an HTML file called mapbox-2.html. In this file, create a map centered–>-->
<!--<!–// TODO: on New York with a marker on the Statue of Liberty. Add a popup to the Statue of–>-->
<!--<!–// TODO: Liberty marker with the text "Statue of Liberty".–>-->
<!--<!–// *BONUS - when the marker is dragged and released, display the coordinates in an H1.–>-->
<!--<!DOCTYPE html>-->
<!--<html lang="en">-->
<!--<head>-->
<!-- <meta charset="UTF-8">-->
<!-- <title>MapBox Two</title>-->
<!--</head>-->
<!--<body>-->
<!--<h1>MapBox-Two</h1>-->
<!--<div id='map' style='width: 400px; height: 300px;'></div>-->
<!--<script src="js/keys.js"></script>-->
<!--<script src='https://api.mapbox.com/mapbox-gl-js/v2.2.0/mapbox-gl.js'></script>-->
<!--<script>-->
<!-- mapboxgl.accessToken = 'pk.eyJ1IjoiY2hyaXN0aW5lYXNmIiwiYSI6ImNrb2VnN2wxNDAyazEycm8xMnltbHNvOGcifQ.ruEKxBA_NCtA3lnveLBecA';-->
<!-- var map = new mapboxgl.Map({-->
<!-- container: 'map', // container ID-->
<!-- style: 'mapbox://styles/mapbox/streets-v11', // style URL-->
<!-- center: [-74.2179, 43.2994], // starting position [lng, lat]-->
<!-- zoom: 12 // starting zoom-->
<!-- });-->
<!--</script>-->
<!--</body>-->
<!--</html>-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Mini-Exercise #2</title>
<link href='https://api.mapbox.com/mapbox-gl-js/v2.2.0/mapbox-gl.css' rel='stylesheet'/>
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Xanh+Mono&display=swap" rel="stylesheet">
<style>
.font {
font-family: 'Xanh Mono', monospace;
}
</style>
</head>
<body>
<h1></h1>
<div id='map' style='width: 600px; height: 500px;'></div>
<label for="input">Type an address:</label>
<input type="text" id="input">
<button id="btn">Click me to move someplace!</button>
<script src="js/keys.js"></script>
<script src="js/jquery-3.6.0.js"></script>
<script src='https://api.mapbox.com/mapbox-gl-js/v2.2.0/mapbox-gl.js'></script>
<script src="js/mapbox-geocoder-utils.js"></script>
<script>
// Mini Exercise 2
// TODO: Create an HTML file called mapbox-2.html. In this file, create a map centered
// TODO: on New York with a marker on the Statue of Liberty. Add a popup to the Statue of
// TODO: Liberty marker with the text "Statue of Liberty".
// *BONUS - when the marker is dragged and released, display the coordinates in an H1.
mapboxgl.accessToken = MAPBOX_ACCESS_TOKEN;
var map = new mapboxgl.Map({
container: 'map', // container ID
style: 'mapbox://styles/mapbox/streets-v11', // style URL
center: [-74.2179, 43.2994], // starting position [lng, lat]
zoom: 5 // starting zoom
});
var marker = new mapboxgl.Marker({
color: 'red'
})
.setLngLat([-74.0445, 40.6892])
.addTo(map);
var popup = new mapboxgl.Popup()
.setLngLat(marker.getLngLat())
.setHTML('<h3 class="font">Statue of Liberty</h3>')
.addTo(map);
marker.setPopup(popup);
marker.setDraggable(true);
marker.on('dragend', function() {
$('h1').html(marker.getLngLat().toString())
marker.setPopup();
})
$("#btn").click(function() {
var userInput = $("#input").val();
geocode(userInput, MAPBOX_ACCESS_TOKEN).then(function (info) {
console.log(info);
var newCenter = {
lng: info[0],
lat: info[1]
};
marker.setLngLat(newCenter);
popup.setHTML('<h3 class="font">' + userInput + '</h3>');
map.flyTo({center: newCenter});
});
});
</script>
</body>
</html>