-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
47 lines (39 loc) · 1.09 KB
/
script.js
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
mapboxgl.accessToken =
"pk.eyJ1IjoidGVzdDAwOTIiLCJhIjoiY2t4c3Z3cWM2MmRuODJva29oYTJsZHFsMCJ9.yWlUhAwJZzpiSRov95U5lA";
navigator.geolocation.getCurrentPosition(successLocation, errorLocation, {
enableHighAccuracy: true,
});
function successLocation(position) {
console.log(position);
showMap([position.coords.longitude, position.coords.latitude]);
}
function errorLocation() {
showMap([77.21, 28.64]);
}
function showMap(centerPosition) {
const map = new mapboxgl.Map({
container: "map",
style: "mapbox://styles/mapbox/streets-v11",
center: centerPosition,
zoom: 14,
});
const navigationControl = new mapboxgl.NavigationControl();
map.addControl(navigationControl, "bottom-right");
const marker = new mapboxgl.Marker().setLngLat(centerPosition);
marker.addTo(map);
// Add the control to the map.
map.addControl(
new MapboxGeocoder({
accessToken: mapboxgl.accessToken,
mapboxgl: mapboxgl,
}),
"top-right"
);
//directions
map.addControl(
new MapboxDirections({
accessToken: mapboxgl.accessToken,
}),
"top-left"
);
}