-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapbox_maps_api.html
More file actions
148 lines (110 loc) · 4.82 KB
/
Copy pathmapbox_maps_api.html
File metadata and controls
148 lines (110 loc) · 4.82 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>MAPBOX MAPS API EXERCISE</title>
<!-- THIS SCRIPT TAG IS NEEDED FOR JS TO WORK-->
<script src='https://api.mapbox.com/mapbox-gl-js/v2.13.0/mapbox-gl.js'></script>
<!-- THIS SCRIPT TAG IS NEEDED FOR THE MAP TO ACTUALLY SHOW-->
<link href='https://api.mapbox.com/mapbox-gl-js/v2.13.0/mapbox-gl.css' rel='stylesheet' />
<style>
p {
color: deeppink;
font-weight: bolder;
}
</style>
</head>
<body>
<label for="search"></label>
<input id="search">
<button id="searchme" type="button">Search</button>
<!--REMEMBER TO ALWAYS PUT THE ID MAP IN A DIV, THIS IS HOW THE SYSTEM KNOWS WHERE TO PUT THE MAP ON YOUR PAGE-->
<!--<div id='map'></div>-->
<!--ONCE THE KEYS ARE CREATED AND SAVED MAKE SURE YOU HAVE THEM LINKED-->
<script src="js/keys.js"></script>
<script src="js/mapbox-geocoder.js"></script>
<!--MAPBOX-->
<!--<script>-->
<!-- mapboxgl.accessToken = MY_TOKEN;-->
<!-- var map = new mapboxgl.Map({-->
<!-- container: 'map', // container ID-->
<!-- style: 'mapbox://styles/mapbox/streets-v11', // style URL-->
<!-- zoom: 10, // starting zoom-->
<!-- center: [-96.7970, 32.7767] // [lng, lat]-->
<!-- });-->
<!--</script>-->
<div id='map' style='width: 500px; height: 500px;'></div>
<script>
mapboxgl.accessToken = MY_TOKEN;
const map = new mapboxgl.Map({
container: 'map', // container ID
style: 'mapbox://styles/mapbox/streets-v12', // style URL
center: [-79.991311, 32.821659, ], // starting position [lng, lat]
zoom: 10, // starting zoom
});
// mapbox gl marker//
// var marker = new mapboxgl.Marker()
//
// .setLngLat([-79.991311, 32.821659])
//
// .addTo(map);
// mapbox marker popup
// let popup = new mapboxgl.Popup()
// // setting location of pop up
// .setLngLat([-79.991311, 32.821659])
// // setting the content on my pop up
// .setHTML("<p class='wangs'>Kicken Chicken</p>")
// // this sets up popup on marker, the popup will only appear when marker is clicked on
// marker.setPopup(popup);
//TO DO Create an array of objects with information about each restaurant to accomplish this. Use a .forEach() loop rather than a for loop.///
// .each( function(index, element) { /* ... */ } )
// $('I THINK THIS SHOULD BE MARKER').each(function(index) {
// if (index % 2 !== 0) {
// $(MARKER).addTo('MAP);
// }
// });
let favRes = [
{'name': 'Kickin Chicken',
'description': 'wings are fresh always',
'coordinates': ['-79.991311', '32.821659']},
{'name': 'Fatz',
'description': 'salads are the best',
'coordinates': ['-80.02012421570004', '32.873577343406005']},
{'name': 'Zaxbys',
'description': 'food is good',
'coordinates': ['-80.01560040333929', '32.87324949205579']}
]
// I DONT NEED TO MAKE A MARKER FOR EACH COORDINATE BECAUSE THE FOREACH LOOP WILL TAKE THOSE COORDINATES WHEN IT LOOPS THRU THE ITEMS IN THE ARRAY//
favRes.forEach(function (reason){
// the console log shows all the information from favRes array that's above.
// console.log(reason.coordinates);
var marker = new mapboxgl.Marker()
.setLngLat([reason.coordinates[0], reason.coordinates[1]])
.addTo(map);
let popup = new mapboxgl.Popup()
// setting location of pop up
.setLngLat([reason.coordinates[0], reason.coordinates[1]])
// setting the content on my pop up
// remember to have tags in quotations when you concatenate here
.setHTML("<p>" + reason.name + "</p>" + "<p>" + reason.description + "</p>")
// this sets up popup on marker, the popup will only appear when marker is clicked on
marker.setPopup(popup);
})
// SEARCH BOX FUNCTIONALITY
document.getElementById("searchme").addEventListener("click", function (){
//geocoder function will return a promise, this promise has our location
let currentLocation = geocode(document.getElementById("search").value, MY_TOKEN);
//Use the then function to retrieve the information from your promise
currentLocation.then(function (results) {
//log out my results(the data within my promise)
console.log(results)
//we set data equal to center of our map
map.setCenter([results[0], results[1]])
})
})
</script>
</body>
</html>