-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapbox_maps_api.html
More file actions
169 lines (167 loc) · 7.31 KB
/
mapbox_maps_api.html
File metadata and controls
169 lines (167 loc) · 7.31 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Mapbox Map</title>
<script src="js/keys.js"></script>
<script src='https://api.mapbox.com/mapbox-gl-js/v2.9.1/mapbox-gl.js'></script>
<link href='https://api.mapbox.com/mapbox-gl-js/v2.9.1/mapbox-gl.css' rel='stylesheet' />
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<style>
#map {
width: 90%;
height: 800px;
margin-top: 50px;
margin-left: 50px;
margin-right: 50px;
border-radius: 35px;
position: center;
}
body {
background-color: black;
}
h1 {
color: white;
font-family: Arial Rounded MT Bold;
}
#loc-search {
margin-left: 50px;
width: 300px;
}
#zoom-in {
margin-left: 50px;
}
#button-holder {
display: flex;
justify-content: space-between;
}
#drop-marker {
margin-right: 100px;
}
</style>
</head>
<body>
<h1 class="text-center">Matt's Maps</h1>
<hr>
<label for="loc-search"></label><input id="loc-search" type="search" placeholder="Search Matt's Maps">
<button id="search-button">search</button>
<div id='map'></div>
<div id="button-holder">
<button id="zoom-in">Zoom In</button>
<button id="zoom-out">Zoom Out</button>
<button id="drop-marker">Drop Marker</button>
</div>
<script>
//generating the map
mapboxgl.accessToken = MAP_BOX_API_KEY;
const map = new mapboxgl.Map({
container: 'map', // container ID
style: 'mapbox://styles/mattmoreno/cl5mn53fl006q14l10c6l30db', // style URL
center: [-98.4916, 29.4252], // starting position [lng, lat]
zoom: 9, // starting zoom
projection: 'globe' // display the map as a 3D globe
});
map.on('style.load', () => {
map.setFog({}); // Set the default atmosphere style
});
// Redraw map with zoom levels 5, 15, 20
// Zoom in
const zoomIn = document.querySelector("#zoom-in")
zoomIn.addEventListener("click", function () {
let currentZoom = map.getZoom();
currentZoom++;
map.setZoom(currentZoom);
});
//Zoom out
const zoomOut = document.querySelector("#zoom-out")
zoomOut.addEventListener("click", function () {
let currentZoom = map.getZoom();
currentZoom--;
map.setZoom(currentZoom);
})
//Create a marker of the exact location of your favorite restaurant
document.querySelector("#drop-marker").addEventListener("click", function (event) {
// add a popup to this
let popup = new mapboxgl.Popup();
popup.setLngLat([-98.58483, 29.54007])
popup.setHTML(
"<div class= card>" +
"<h5>Magnolia Pancake Haus</h5>" +
"<p><h6>10333 Huebner Rd, San Antonio, TX 78240</h6></p>" +
"<p><em>Hours: 7am-2pm</em></p>" +
"<img src='img/pancake haus.jpeg'>" +
"</div>"
)
//set the marker to contain the popup
let marker = new mapboxgl.Marker();
marker.setPopup(popup);
marker.setLngLat([-98.58483, 29.54007]);
marker.addTo(map);
});
//function to get the location from inputting the address into mapbox
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;
});
}
//create an array of objects with information about 3 restaurants
let restaurants = [{
'LatLng':[-98.59350,29.56576],
'address':'8823 Wurzbach Rd, San Antonio, TX 78240',
'name':"Tommy's Restaurant",
'image':src="img/tommys-tacos.jpeg",
'hours':'Hours: 7am-3pm',
'menu':'https://www.mytommys.com/',
},{
'LatLng':[-98.4318, 29.52027],
'address':'2015 NE Interstate 410 Loop, San Antonio, TX 78217',
'name':'Comfort Cafe',
'image':src="img/comfort-cafe.jpeg",
'hours':'Hours: 8am-4pm',
'menu':'https://www.serenitystar.org/menu',
},{
'LatLng':[-97.39521, 27.71926],
'address':'4201 S Padre Island Dr #4405, Corpus Christi, TX 78411',
'name':'La Playa',
'image':src="img/la-playa.jpeg",
'hours':'Hours: 11am-10pm',
'menu':'https://www.laplaya.cc/',
}]
//use a forEach loop and refactor the code
restaurants.forEach(function (restaurant) {
restaurant.marker = new mapboxgl.Marker()
.setLngLat(restaurant.LatLng)
.addTo(map);
restaurant.popup = new mapboxgl.Popup()
.setHTML(`
<div class="card">
<h5>${restaurant.name}</h5>
<p><h6>${restaurant.address}</h6></p>
<p><em>${restaurant.hours}</em></p>
<img src=${restaurant.image} alt="logo">
</div>
`)
restaurant.marker.setPopup(restaurant.popup)
})
const searchButton = document.getElementById("search-button")
searchButton.addEventListener("click", async function() {
//get address from text input
const addressInput = document.querySelector("#loc-search");
const newAddress = addressInput.value;
//get geocode info from the address
let info = await getLatLngFromAddress(newAddress);
//center the map on the new coords
map.setCenter(info.LatLng);
//make a marker for new location
})
</script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
</body>
</html>