質問編集履歴

1

ソースコードの追加

2021/08/15 02:22

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -11,3 +11,233 @@
11
11
  webページの作成等は今まで一切触れた事がなく,通常のアプリケーションの感覚で作ってみたら動かなくて困惑しています.
12
12
 
13
13
  どのように調べたら解決策が出てくるかもまるで検討がつきません.これ以降さらにapiを活用してmap上のポイントを追加したり,削除したりするより動的な処理をさせようとしているので初歩でつまづいて困っています.こういった場合の対処法,技術,調べ方,方針等webアプリケーション制作に詳しい方にアドバイスいただけると幸いです.
14
+
15
+
16
+
17
+ [追記]
18
+
19
+ 一応文中にサンプルのjavascript内のマップロード前にapi叩いただけだと書きましたが,どうやらわかりづらかったようなのでコードを貼っておきます.見ての通りただ本当にapiの処理を入れただけです.
20
+
21
+ ```WebMapPoint.html
22
+
23
+ <!DOCTYPE html>
24
+
25
+ <html>
26
+
27
+ <head>
28
+
29
+ <meta charset="utf-8">
30
+
31
+ <title>GeoJSONポイントの描画</title>
32
+
33
+ <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
34
+
35
+ <link href="https://api.mapbox.com/mapbox-gl-js/v2.4.0/mapbox-gl.css" rel="stylesheet">
36
+
37
+ <script src="https://api.mapbox.com/mapbox-gl-js/v2.4.0/mapbox-gl.js"></script>
38
+
39
+ <style>
40
+
41
+ body { margin: 0; padding: 0; }
42
+
43
+ #map { position: absolute; top: 0; bottom: 0; width: 100%; }
44
+
45
+ </style>
46
+
47
+ </head>
48
+
49
+ <body>
50
+
51
+ <div id="map"></div>
52
+
53
+ <script src="MapLoad.js" charset="UTF-8">
54
+
55
+ </script>
56
+
57
+
58
+
59
+ </body>
60
+
61
+ </html>
62
+
63
+ ```
64
+
65
+ ```MapLoad.js
66
+
67
+ //ここのコメントアウトを外してapiへのアクセスを試みると動かなくなる.
68
+
69
+ /*
70
+
71
+ let apiURL = "http://localhost:7071/api/mapPoint";//apiのURL
72
+
73
+
74
+
75
+ //apiリクエストの作成
76
+
77
+ var request = new XMLHttpRequest();
78
+
79
+ request.open("GET", apiURL, true);
80
+
81
+ request.responseType = "json";
82
+
83
+
84
+
85
+ //apiからPOINT情報をGETで全て取得する
86
+
87
+ request.onload = function () {
88
+
89
+ var data = this.response;
90
+
91
+ }
92
+
93
+ request.send();
94
+
95
+
96
+
97
+ var jsondata = [
98
+
99
+ {
100
+
101
+ "type": "geojson",
102
+
103
+ "data": data
104
+
105
+ }
106
+
107
+ ]
108
+
109
+ */
110
+
111
+
112
+
113
+ // TO MAKE THE MAP APPEAR YOU MUST
114
+
115
+ // ADD YOUR ACCESS TOKEN FROM
116
+
117
+ // https://account.mapbox.com
118
+
119
+ mapboxgl.accessToken = "<トークン>";
120
+
121
+ const map = new mapboxgl.Map({
122
+
123
+ container: "map",
124
+
125
+ style: "mapbox://styles/mapbox/light-v10",
126
+
127
+ center: [-96, 37.8],
128
+
129
+ zoom: 2
130
+
131
+ });
132
+
133
+
134
+
135
+ //let jsonObject=JSON.stringify(jsondata);
136
+
137
+
138
+
139
+ map.on("load", () => {
140
+
141
+ // Add an image to use as a custom marker
142
+
143
+ map.loadImage(
144
+
145
+ "https://docs.mapbox.com/mapbox-gl-js/assets/custom_marker.png",
146
+
147
+ (error, image) => {
148
+
149
+ if (error) throw error;
150
+
151
+ map.addImage("custom-marker", image);
152
+
153
+ // Add a GeoJSON source with 2 points
154
+
155
+ map.addSource("points",
156
+
157
+ {
158
+
159
+ "type": "geojson",
160
+
161
+ "data": {
162
+
163
+ "type": "FeatureCollection",
164
+
165
+ "features": [
166
+
167
+ {
168
+
169
+ "type": "Feature",
170
+
171
+ "geometry": {
172
+
173
+ "type": "Point",
174
+
175
+ "coordinates": [36.28649301434915, -112.09412822905652]
176
+
177
+ },
178
+
179
+ "properties": {
180
+
181
+ "id": 1,
182
+
183
+ "name": "Grand Canyon National Park",
184
+
185
+ "address": "Arizona, United States",
186
+
187
+ "phone": "+19286387888",
188
+
189
+ "url": "http://www.nps.gov/grca/"
190
+
191
+ }
192
+
193
+ }
194
+
195
+ ]
196
+
197
+ }
198
+
199
+ });
200
+
201
+
202
+
203
+ // Add a symbol layer
204
+
205
+ map.addLayer({
206
+
207
+ "id": "points",
208
+
209
+ "type": "symbol",
210
+
211
+ "source": "points",
212
+
213
+ "layout": {
214
+
215
+ "icon-image": "custom-marker",
216
+
217
+ // get the title name from the source"s "title" property
218
+
219
+ "text-field": ["get", "name"],
220
+
221
+ "text-font": [
222
+
223
+ "Open Sans Semibold",
224
+
225
+ "Arial Unicode MS Bold"
226
+
227
+ ],
228
+
229
+ "text-offset": [0, 1.25],
230
+
231
+ "text-anchor": "top"
232
+
233
+ }
234
+
235
+ });
236
+
237
+ }
238
+
239
+ );
240
+
241
+ });
242
+
243
+ ```