forked from msaat366/Ryoko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.html
188 lines (164 loc) · 5.6 KB
/
map.html
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>OpenTripMap Vector tiles</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
<script src="https://api.mapbox.com/mapbox-gl-js/v1.9.1/mapbox-gl.js"></script>
<script type="text/javascript" src="https://opentripmap.io/js/directory.en.js"></script>
<link href="https://api.mapbox.com/mapbox-gl-js/v1.9.1/mapbox-gl.css" rel="stylesheet"/>
<style>
#map {
display: block;
width: 100%;
position: absolute;
top: 0;
bottom: 0;
margin: 0;
right: 0;
left: 0;
}
img {
margin: auto;
padding: 10px;
display: block;
width: 200px;
}
h2 {
font-size: 1.25rem;
margin-bottom: .5rem;
font-weight: 500;
line-height: 1.2;
margin-top: 0;
}
@media (min-width: 600px){
img {
margin: auto;
padding: 10px;
display: block;
width: 400px;
}
.mapboxgl-popup {
max-width: 500px !important;
}
}
</style>
</head>
<body>
<div id="map"></div>
<script>
//You should get your API key at https://opentripmap.io
var apiKey = "5ae2e3f221c38a28845f05b63583c00e0e8aff14104cbcba69d1f000";
function apiGet(method, query) {
return new Promise(function (resolve, reject) {
var otmAPI =
"https://api.opentripmap.com/0.1/en/places/" +
method +
"?apikey=" +
apiKey;
if (query !== undefined) {
otmAPI += "&" + query;
}
fetch(otmAPI)
.then(response => response.json())
.then(data => resolve(data))
.catch(function (err) {
console.log("Fetch Error :-S", err);
});
});
}
//You should get your access token at https://www.mapbox.com
mapboxgl.accessToken = "pk.eyJ1IjoibWF4MzM2IiwiYSI6ImNrb3Ryc3R2eTBlNXMycGxsN2ZpaHFkbmwifQ.qpnAfEPyUXdXHIw4aVlbNA";
var map = new mapboxgl.Map({
container: "map",
style: "mapbox://styles/mapbox/streets-v11",
center: [75.7139, 19.7515],
zoom: 8
});
map.addControl(new mapboxgl.NavigationControl());
map.on("load", function () {
//Stylization
//Add pois layer to the map
map.addSource("opentripmap.pois", {
type: "vector",
attribution:
'<a href="https://opentripmap.io" target="_blank">© OpenTripMap</a>',
bounds: [-180, -85.0511, 180, 85.0511],
minzoom: 8,
maxzoom: 14,
scheme: "xyz",
tiles: [
"https://api.opentripmap.com/0.1/en/tiles/pois/{z}/{x}/{y}.pbf?kinds=museums,historic,natural,architecture,religion&rate=2&apikey=" + apiKey
]
});
map.addLayer(
{
id: "opentripmap-pois",
type: "circle",
source: "opentripmap.pois",
"source-layer": "pois",
minzoom: 8,
paint: {
"circle-color": "rgb(55,144,144)",
"circle-radius": 5,
"circle-stroke-color": "rgba(102,193,201, 0.6)",
"circle-stroke-width": 0.6
}
},
"airport-label"
);
});
//Show information by click
function onShowPOI(data, lngLat) {
let poi = document.createElement("div");
poi.innerHTML = "<h2>" + data.name + "<h2>";
poi.innerHTML += "<p><i>" + getCategoryName(data.kinds) + "</i></p>";
if (data.preview) {
poi.innerHTML += "<img src='"+data.preview.source+"'>";
}
poi.innerHTML += data.wikipedia_extracts
? data.wikipedia_extracts.html
: data.info
? data.info.descr
: "No description";
new mapboxgl.Popup().setLngLat(lngLat)
.setDOMContent(poi)
.addTo(map);
}
map.on("click", "opentripmap-pois", function (e) {
let coordinates = e.features[0].geometry.coordinates.slice();
let id = e.features[0].properties.id;
let name = e.features[0].properties.name;
console.log(coordinates);
while (Math.abs(e.lngLat.lng - coordinates[0]) > 180) {
coordinates[0] += e.lngLat.lng > coordinates[0] ? 360 : -360;
}
apiGet("xid/" + id).then(data => onShowPOI(data, e.lngLat));
});
//Show popup by mousemove
let popup = new mapboxgl.Popup({
closeButton: false,
closeOnClick: false
});
map.on("mouseenter", "opentripmap-pois", function (e) {
console.log(e);
map.getCanvas().style.cursor = "pointer";
let coordinates = e.features[0].geometry.coordinates.slice();
console.log(coordinates);
let id = e.features[0].properties.id;
let name = e.features[0].properties.name;
while (Math.abs(e.lngLat.lng - coordinates[0]) > 180) {
coordinates[0] += e.lngLat.lng > coordinates[0] ? 360 : -360;
}
popup
.setLngLat(coordinates)
.setHTML("<strong>" + name + "</strong>")
.addTo(map);
});
map.on("mouseleave", "opentripmap-pois", function () {
map.getCanvas().style.cursor = "";
popup.remove();
});
</script>
</body>
</html>