|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <meta charset="UTF-8" /> |
| 5 | + <meta http-equiv="X-UA-Compatible" content="IE=edge" /> |
| 6 | + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| 7 | + <title>Document</title> |
| 8 | + <script src="https://api.mapbox.com/mapbox-gl-js/v2.7.0/mapbox-gl.js"></script> |
| 9 | + <link |
| 10 | + href="https://api.mapbox.com/mapbox-gl-js/v2.7.0/mapbox-gl.css" |
| 11 | + rel="stylesheet" |
| 12 | + /> |
| 13 | + <style> |
| 14 | + body { |
| 15 | + margin: 0; |
| 16 | + padding: 0; |
| 17 | + } |
| 18 | + #map { |
| 19 | + position: absolute; |
| 20 | + top: 0; |
| 21 | + bottom: 0; |
| 22 | + width: 100%; |
| 23 | + } |
| 24 | + button, |
| 25 | + input, |
| 26 | + #dropdown { |
| 27 | + position: fixed; |
| 28 | + padding: 8px 20px; |
| 29 | + font-size: 16px; |
| 30 | + background: white; |
| 31 | + outline: none; |
| 32 | + border: none; |
| 33 | + border-radius: 5px; |
| 34 | + cursor: pointer; |
| 35 | + } |
| 36 | + |
| 37 | + #dropdown { |
| 38 | + padding: 0; |
| 39 | + bottom: 60px; |
| 40 | + right: 10px; |
| 41 | + max-height: 50vh; |
| 42 | + overflow: scroll; |
| 43 | + max-width: 30vh; |
| 44 | + z-index: 1; |
| 45 | + } |
| 46 | + |
| 47 | + #dropdown p { |
| 48 | + margin-left: 5px; |
| 49 | + } |
| 50 | + |
| 51 | + button:nth-last-of-type(2) { |
| 52 | + top: 10px; |
| 53 | + left: 10px; |
| 54 | + } |
| 55 | + |
| 56 | + button:nth-last-of-type(1) { |
| 57 | + right: 10px; |
| 58 | + top: 10px; |
| 59 | + } |
| 60 | + #customPopup { |
| 61 | + position: fixed; |
| 62 | + width: 300px; |
| 63 | + background-color: #ffffff; |
| 64 | + padding: 10px; |
| 65 | + border-radius: 5px; |
| 66 | + } |
| 67 | + #customGeocoder { |
| 68 | + bottom: 10px; |
| 69 | + right: 10px; |
| 70 | + } |
| 71 | + </style> |
| 72 | + </head> |
| 73 | + <body> |
| 74 | + <script src="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-directions/v4.1.0/mapbox-gl-directions.js"></script> |
| 75 | + <link |
| 76 | + rel="stylesheet" |
| 77 | + href="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-directions/v4.1.0/mapbox-gl-directions.css" |
| 78 | + type="text/css" |
| 79 | + /> |
| 80 | + <!-- Load the `mapbox-gl-geocoder` plugin. --> |
| 81 | + <script src="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v4.7.2/mapbox-gl-geocoder.min.js"></script> |
| 82 | + <link |
| 83 | + rel="stylesheet" |
| 84 | + href="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v4.7.2/mapbox-gl-geocoder.css" |
| 85 | + type="text/css" |
| 86 | + /> |
| 87 | + <div id="map"></div> |
| 88 | + <div id="customPopup"></div> |
| 89 | + <button id="toggle">Toggle</button> |
| 90 | + <button id="home">Home</button> |
| 91 | + <input id="customGeocoder" type="text" /> |
| 92 | + <div id="dropdown"></div> |
| 93 | + </body> |
| 94 | + <script> |
| 95 | + const myPosition = {}; |
| 96 | + const position = {}; |
| 97 | + const toggleBtn = document.querySelector("#toggle"); |
| 98 | + const homeBtn = document.querySelector("#home"); |
| 99 | + const customPopup = document.querySelector("#customPopup"); |
| 100 | + const customGeocoder = document.querySelector("#customGeocoder"); |
| 101 | + const dropdown = document.querySelector("#dropdown"); |
| 102 | + let map, marker; |
| 103 | + mapboxgl.accessToken = |
| 104 | + "pk.eyJ1Ijoic2VubmluZSIsImEiOiJjbDB0aHljY2UwNnE5M2lwZXA3dG02amRoIn0.OReYhfaCWigJ7ae-eGqogg"; |
| 105 | + const directionControl = new MapboxDirections({ |
| 106 | + accessToken: mapboxgl.accessToken, |
| 107 | + }); |
| 108 | + const geocoder = new MapboxGeocoder({ |
| 109 | + // Initialize the geocoder |
| 110 | + accessToken: mapboxgl.accessToken, // Set the access token |
| 111 | + mapboxgl: mapboxgl, |
| 112 | + }); |
| 113 | + |
| 114 | + customGeocoder.oninput = (e) => { |
| 115 | + dropdown.innerHTML = ""; |
| 116 | + fetch( |
| 117 | + `https://api.mapbox.com/geocoding/v5/mapbox.places/${e.target.value}.json?autocomplete=true&access_token=pk.eyJ1Ijoic2VubmluZSIsImEiOiJjbDB0aHljY2UwNnE5M2lwZXA3dG02amRoIn0.OReYhfaCWigJ7ae-eGqogg` |
| 118 | + ) |
| 119 | + .then((res) => res.json()) |
| 120 | + .then((data) => { |
| 121 | + data.features.map((ele) => { |
| 122 | + console.log(ele.place_name); |
| 123 | + const node = document.createElement("p"); |
| 124 | + const textNode = document.createTextNode(ele.place_name); |
| 125 | + node.addEventListener("click", (e) => { |
| 126 | + map.setCenter(ele.center); |
| 127 | + marker.setLngLat(ele.center); |
| 128 | + position["longitude"] = ele.center[0]; |
| 129 | + position["latitude"] = ele.center[1]; |
| 130 | + createPopup(); |
| 131 | + }); |
| 132 | + node.appendChild(textNode); |
| 133 | + dropdown.appendChild(node); |
| 134 | + }); |
| 135 | + }) |
| 136 | + .catch(() => console.log("not found")); |
| 137 | + }; |
| 138 | + |
| 139 | + toggleBtn.onclick = () => { |
| 140 | + if (map) { |
| 141 | + if (map.hasControl(directionControl)) { |
| 142 | + map.removeControl(directionControl); |
| 143 | + } else { |
| 144 | + map.addControl(directionControl, "bottom-left"); |
| 145 | + } |
| 146 | + } |
| 147 | + }; |
| 148 | + |
| 149 | + homeBtn.onclick = () => { |
| 150 | + getLocation(); |
| 151 | + }; |
| 152 | + |
| 153 | + const calcPosCustomPopup = (content) => { |
| 154 | + let markerEle = document.querySelector('[aria-label="Map marker"]'); |
| 155 | + let markerPos = markerEle.getBoundingClientRect(); |
| 156 | + if (content) { |
| 157 | + customPopup.innerHTML = content; |
| 158 | + } |
| 159 | + customPopup.style.left = markerPos.x + 30 + "px"; |
| 160 | + customPopup.style.top = markerPos.y + 30 + "px"; |
| 161 | + }; |
| 162 | + |
| 163 | + const createPopup = () => { |
| 164 | + fetch( |
| 165 | + `https://api.mapbox.com/geocoding/v5/mapbox.places/${position["longitude"]},${position["latitude"]}.json?types=poi&access_token=pk.eyJ1Ijoic2VubmluZSIsImEiOiJjbDB0aHljY2UwNnE5M2lwZXA3dG02amRoIn0.OReYhfaCWigJ7ae-eGqogg` |
| 166 | + ) |
| 167 | + .then((res) => res.json()) |
| 168 | + .then((data) => { |
| 169 | + calcPosCustomPopup(data.features[0].place_name); |
| 170 | + }); |
| 171 | + }; |
| 172 | + |
| 173 | + const renderMap = () => { |
| 174 | + map = new mapboxgl.Map({ |
| 175 | + container: "map", // container ID |
| 176 | + style: "mapbox://styles/mapbox/streets-v11", // style URL |
| 177 | + center: [position.longitude, position.latitude], // starting position [lng, lat] |
| 178 | + zoom: 15, // starting zoom |
| 179 | + }); |
| 180 | + |
| 181 | + map.on("click", (e) => { |
| 182 | + position["latitude"] = e.lngLat.lat; |
| 183 | + position["longitude"] = e.lngLat.lng; |
| 184 | + createPopup(); |
| 185 | + marker.setLngLat([position["longitude"], position["latitude"]]); |
| 186 | + }); |
| 187 | + |
| 188 | + map.on("drag", () => { |
| 189 | + calcPosCustomPopup(); |
| 190 | + }); |
| 191 | + |
| 192 | + marker = new mapboxgl.Marker({ color: "red" }) |
| 193 | + .setLngLat([position["longitude"], position["latitude"]]) |
| 194 | + .addTo(map); |
| 195 | + // map.addControl(geocoder, "bottom-right"); |
| 196 | + createPopup(); |
| 197 | + }; |
| 198 | + |
| 199 | + const success = (pos) => { |
| 200 | + position["latitude"] = pos.coords.latitude; |
| 201 | + position["longitude"] = pos.coords.longitude; |
| 202 | + renderMap(); |
| 203 | + }; |
| 204 | + |
| 205 | + const error = () => { |
| 206 | + alert("ERROR"); |
| 207 | + }; |
| 208 | + |
| 209 | + const getLocation = () => { |
| 210 | + if (navigator.geolocation) { |
| 211 | + navigator.geolocation.getCurrentPosition(success, error, { |
| 212 | + maximumAge: 60000, |
| 213 | + timeout: 10000, |
| 214 | + enableHighAccuracy: true, |
| 215 | + }); |
| 216 | + } |
| 217 | + }; |
| 218 | + |
| 219 | + getLocation(); |
| 220 | + </script> |
| 221 | +</html> |
0 commit comments