質問編集履歴

4

修正

2020/02/03 04:40

投稿

yows
yows

スコア11

test CHANGED
File without changes
test CHANGED
@@ -297,3 +297,127 @@
297
297
  consoleには
298
298
 
299
299
  ``main.js:52 Uncaught TypeError: Cannot read property 'title' of undefined``とでています。
300
+
301
+
302
+
303
+ ###追記2
304
+
305
+ 単純に開かせる方法で、試してみたのですがうまく行きませんでした。
306
+
307
+ ```
308
+
309
+ 'use strict'
310
+
311
+
312
+
313
+ {
314
+
315
+ var map = L.map('map').setView([38.4316, 141.3094], 12);
316
+
317
+
318
+
319
+ var geojsonFeature = [];
320
+
321
+ var test_popupContents = ['sampleA', 'sampleB', 'sampleC', 'sampleD'];
322
+
323
+ var lat = [38.4316, 38.4306, 38.4299, 38.4327];
324
+
325
+ var lon = [141.3094, 141.3076, 141.3070, 141.3087];
326
+
327
+
328
+
329
+ var tileLayer = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',{
330
+
331
+ attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>',
332
+
333
+ maxZoom: 20,
334
+
335
+ autoClose:false
336
+
337
+ });
338
+
339
+ tileLayer.addTo(map);
340
+
341
+
342
+
343
+ for (var i = 0; i < test_popupContents.length; i++) {
344
+
345
+ geojsonFeature.push({
346
+
347
+ "type": "Feature",
348
+
349
+ "properties": {
350
+
351
+ "popupContent": test_popupContents[i],
352
+
353
+ },
354
+
355
+ "geometry": {
356
+
357
+ "type": "Point",
358
+
359
+ "coordinates": [lon[i], lat[i]]
360
+
361
+ },
362
+
363
+ });
364
+
365
+ }
366
+
367
+
368
+
369
+ L.geoJson(geojsonFeature,
370
+
371
+ {
372
+
373
+ onEachFeature: function(feature, layer){
374
+
375
+ if(feature.properties && feature.properties.popupContent){
376
+
377
+ // layer.bindPopup(feature.properties.popupContent, {className: feature.properties.className});
378
+
379
+ layer.bindPopup(feature.properties.popupContent);
380
+
381
+ geojsonFeature.push(layer);
382
+
383
+ }
384
+
385
+ },
386
+
387
+ pointToLayer: function (feature, latlng) {
388
+
389
+ var m = L.marker( latlng, { title : feature.properties.popupContent } );
390
+
391
+ geojsonFeature.push( m );
392
+
393
+ return m;
394
+
395
+ }
396
+
397
+ }).addTo(map);
398
+
399
+
400
+
401
+ function markerFunction() {
402
+
403
+ for (var i in geojsonFeature) {
404
+
405
+ geojsonFeature[i].openPopup();
406
+
407
+ }
408
+
409
+ }
410
+
411
+
412
+
413
+ markerFunction();
414
+
415
+
416
+
417
+ }
418
+
419
+ ```
420
+
421
+ これをしますと、consoleが
422
+
423
+ ``Uncaught TypeError: geojsonFeature[i].openPopup is not a function``になります。

3

修正

2020/02/03 04:40

投稿

yows
yows

スコア11

test CHANGED
File without changes
test CHANGED
@@ -166,7 +166,7 @@
166
166
 
167
167
 
168
168
 
169
- 2/3 追記
169
+ ##2/3 追記
170
170
 
171
171
  いただいたコメントを参考に次のコードを書いてみました。
172
172
 

2

コード追加

2020/02/03 02:46

投稿

yows
yows

スコア11

test CHANGED
File without changes
test CHANGED
@@ -163,3 +163,137 @@
163
163
 
164
164
 
165
165
  これを、すべての``test_popupContents``の配列のすべてを``openPopup()``にするためにはどうしたらよいでしょうか?
166
+
167
+
168
+
169
+ 2/3 追記
170
+
171
+ いただいたコメントを参考に次のコードを書いてみました。
172
+
173
+ ```
174
+
175
+ 'use strict'
176
+
177
+
178
+
179
+ {
180
+
181
+ var map = L.map('map').setView([38.4316, 141.3094], 12);
182
+
183
+
184
+
185
+ var geojsonFeature = [];
186
+
187
+ var test_popupContents = ['sampleA', 'sampleB', 'sampleC', 'sampleD'];
188
+
189
+ var lat = [38.4316, 38.4306, 38.4299, 38.4327];
190
+
191
+ var lon = [141.3094, 141.3076, 141.3070, 141.3087];
192
+
193
+
194
+
195
+ var tileLayer = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',{
196
+
197
+ attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>',
198
+
199
+ maxZoom: 20,
200
+
201
+ autoClose:false
202
+
203
+ });
204
+
205
+ tileLayer.addTo(map);
206
+
207
+
208
+
209
+ for (var i = 0; i < test_popupContents.length; i++) {
210
+
211
+ geojsonFeature.push({
212
+
213
+ "type": "Feature",
214
+
215
+ "properties": {
216
+
217
+ "popupContent": test_popupContents[i],
218
+
219
+ },
220
+
221
+ "geometry": {
222
+
223
+ "type": "Point",
224
+
225
+ "coordinates": [lon[i], lat[i]]
226
+
227
+ },
228
+
229
+ });
230
+
231
+ }
232
+
233
+
234
+
235
+ L.geoJson(geojsonFeature,
236
+
237
+ {
238
+
239
+ onEachFeature: function(feature, layer){
240
+
241
+ if(feature.properties && feature.properties.popupContent){
242
+
243
+ // layer.bindPopup(feature.properties.popupContent, {className: feature.properties.className});
244
+
245
+ layer.bindPopup(feature.properties.popupContent);
246
+
247
+ geojsonFeature.push(layer);
248
+
249
+ }
250
+
251
+ },
252
+
253
+ pointToLayer: function (feature, latlng) {
254
+
255
+ var m = L.marker( latlng, { title : feature.properties.popupContent } );
256
+
257
+ geojsonFeature.push( m );
258
+
259
+ return m;
260
+
261
+ }
262
+
263
+ }).addTo(map);
264
+
265
+
266
+
267
+ function markerFunction( id ) {
268
+
269
+ for ( var i in geojsonFeature ) {
270
+
271
+ var geoJsonFeatureID = geojsonFeature[i].options.title;
272
+
273
+ if ( geoJsonFeatureID == id ) {
274
+
275
+ geojsonFeature[i].openPopup();
276
+
277
+ }
278
+
279
+ }
280
+
281
+ }
282
+
283
+
284
+
285
+ markerFunction();
286
+
287
+
288
+
289
+ }
290
+
291
+ ```
292
+
293
+
294
+
295
+ これでも、うまく行きません。
296
+
297
+ consoleには
298
+
299
+ ``main.js:52 Uncaught TypeError: Cannot read property 'title' of undefined``とでています。

1

タグ、追加

2020/02/03 02:45

投稿

yows
yows

スコア11

test CHANGED
File without changes
test CHANGED
@@ -5,6 +5,66 @@
5
5
 
6
6
 
7
7
  #実際のコード
8
+
9
+ ```
10
+
11
+ <!DOCTYPE html>
12
+
13
+ <html>
14
+
15
+ <head>
16
+
17
+ <meta charset="utf-8">
18
+
19
+ <meta name="viewport" content="width=device-width, initial-scale=1">
20
+
21
+ <title></title>
22
+
23
+ <link rel="stylesheet" href="https://unpkg.com/leaflet@1.6.0/dist/leaflet.css"
24
+
25
+ integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ=="
26
+
27
+ crossorigin=""/>
28
+
29
+ <link rel="stylesheet" href="main.css">
30
+
31
+ </head>
32
+
33
+ <body class="container">
34
+
35
+ <header></header>
36
+
37
+ <main>
38
+
39
+ <div id="app">
40
+
41
+ <div id="map"></div>
42
+
43
+ </div>
44
+
45
+
46
+
47
+ </main>
48
+
49
+ <footer></footer>
50
+
51
+
52
+
53
+ <script src="https://unpkg.com/leaflet@1.6.0/dist/leaflet.js"
54
+
55
+ integrity="sha512-gZwIG9x3wUXg2hdXF6+rVkLF/0Vi9U8D2Ntg4Ga5I5BZpVkVxlJWbSQtXPSiUTtC0TjtGOmxa1AJPuV0CPthew=="
56
+
57
+ crossorigin=""></script>
58
+
59
+ <script src="https://code.jquery.com/jquery-3.3.1.js"></script>
60
+
61
+ <script src="main.js"></script>
62
+
63
+ </body>
64
+
65
+ </html>
66
+
67
+ ```
8
68
 
9
69
  ```javaScripot
10
70