-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapbox_maps_api.html
More file actions
102 lines (89 loc) · 2.79 KB
/
mapbox_maps_api.html
File metadata and controls
102 lines (89 loc) · 2.79 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Mapbox Exercise</title>
<link href="https://api.mapbox.com/mapbox-gl-js/v2.14.1/mapbox-gl.css" rel="stylesheet">
<script src="https://api.mapbox.com/mapbox-gl-js/v2.14.1/mapbox-gl.js"></script>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
header {
text-align: center;
}
#map {
width: 50%;
height: 400px;
margin: 15px auto;
}
.bunzPopup, .restaurantPopup {
text-align: center;
}
</style>
</head>
<body>
<header>
<h1>Mapbox API</h1>
</header>
<div id="map"></div>
<script src="js/keys.js"></script>
<script src="js/mapbox-geocoder-utils.js"></script>
<script>
const restaurants = [
{
name: "Bunz Handcrafted Burgers",
longitude: -98.49293,
latitude: 29.42631
},
{
name: "Domingo Restaurant",
longitude: -98.49112532067805,
latitude: 29.42487931317373
},
{
name: "Sushishima Japanese Restaurant",
longitude: -98.52267465374318,
latitude: 29.534891415799432
}
]
mapboxgl.accessToken = 'pk.eyJ1IjoianJ1ZWRhcyIsImEiOiJja29lZzZzaHAwYTNtMm5ucjAzajdleDkyIn0.yY13LnqWTQzsI8ESEwK-1A';
const map = new mapboxgl.Map({
container: 'map', // container ID
// Choose from Mapbox's core styles, or make your own style with Mapbox Studio
style: 'mapbox://styles/mapbox/streets-v12', // style URL
center: [-74.5, 40], // starting position [lng, lat]
zoom: 10 // starting zoom
});
// geocode("122 E Houston St, San Antonio, TX 78205", MAPBOX_API_TOKEN).
// then(coords => {
// console.log(coords);
// const bunzMarker = new mapboxgl.Marker()
// .setLngLat(coords)
// .addTo(map);
// map.setCenter(coords);
// map.setZoom(18);
// const bunzPopup = new mapboxgl.Popup().
// setHTML(`<h2 class="bunzPopup">Bunz Handcrafted Burgers</h2>`);
// bunzMarker.setPopup(bunzPopup);
//
// });
restaurants.forEach((restaurant, index)=> {
if (index === restaurants.length -1){
map.setCenter([restaurant.longitude, restaurant.latitude]);
map.setZoom(10);
}
const marker = new mapboxgl.Marker()
.setLngLat([restaurant.longitude, restaurant.latitude])
.addTo(map);
const popup = new mapboxgl.Popup()
.setHTML(`
<h2 class="restaurant-popup">${restaurant.name}</h2>
`);
marker.setPopup(popup);
})
</script>
</body>
</html>