Leaflet.jsとVue.jsを使って保育所の地図を表示したいと考えています。
axiosを用いてjsonデータを読み込みlayersに格納したいと思っていますが、うまくいきません。
よろしくお願いします。
html
1<!doctype html> 2<html lang="en"> 3 4<head> 5 <title>Interactive Map</title> 6 <meta charset="utf-8"> 7 <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> 8 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta/css/bootstrap.min.css"> 9 <link rel="stylesheet" href="https://unpkg.com/leaflet@1.2.0/dist/leaflet.css"> 10 <link rel="stylesheet" href="style.css"> 11</head> 12 13<body> 14 <div class="container mt-3 mt-sm-5" id="app"> 15 <div class="row"> 16 <div class="col-md-9"> 17 <div class="map" id="map"></div> 18 </div> 19 <div class="col-md-3"> 20 <div 21 class="form-check" 22 v-for="layer in layers" 23 :key="layer.id" 24 > 25 <label class="form-check-label"> 26 <input 27 class="form-check-input" 28 type="checkbox" 29 v-model="layer.active" 30 @change="layerChanged(layer.id, layer.active)" 31 /> 32 {{ layer.name }} 33 </label> 34 </div> 35 </div> 36 </div> 37 </div> 38 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.4/vue.min.js"></script> 39 <script src="https://unpkg.com/leaflet@1.2.0/dist/leaflet.js"></script> 40 <script src="https://cdn.jsdelivr.net/npm/axios@0.17.1/dist/axios.min.js"></script> 41 <script src="app.js"></script> 42 43</body> 44 45</html>
css
1.map { height: 540px; }
js
1new Vue({ 2 el: '#app', 3 data: { 4 map: null, 5 tileLayer: null, 6 layers: null, 7 }, 8 created: function () { 9 axios.get('list.json').then(function (response) { 10 console.log(response) 11 this.layers = response.data 12 }.bind(this)).catch(function (e) { 13 console.error(e) 14 }) 15 }, 16 mounted() { 17 this.initMap(); 18 this.initLayers(); 19 }, 20 methods: { 21 layerChanged(layerId, active) { 22 const layer = this.layers.find(layer => layer.id === layerId); 23 layer.features.forEach((feature) => { 24 if (active) { 25 feature.leafletObject.addTo(this.map); 26 } else { 27 feature.leafletObject.removeFrom(this.map); 28 } 29 }); 30 }, 31 initLayers() { 32 this.layers.forEach((layer) => { 33 const markerFeatures = layer.features.filter(feature => feature.type === 'marker'); 34 35 markerFeatures.forEach((feature) => { 36 feature.leafletObject = L.marker(feature.coords) 37 .bindPopup(feature.name+"<br>"+feature.schoolType+"<br><a href='/'>詳細ページ</a>"); 38 }); 39 }); 40 }, 41 initMap() { 42 this.map = L.map('map').setView([34.68, 135.82], 12); 43 this.tileLayer = L.tileLayer( 44 'https://cartodb-basemaps-{s}.global.ssl.fastly.net/rastertiles/voyager/{z}/{x}/{y}.png', { 45 maxZoom: 18, 46 attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>, © <a href="https://carto.com/attribution">CARTO</a>', 47 } 48 ); 49 50 this.tileLayer.addTo(this.map); 51 }, 52 }, 53}); 54
json
1[ 2 { 3 "id":0, 4 "name": "保育園", 5 "active" : true, 6 "features": [ 7 { 8 "id": 1, 9 "name": "三笠保育園", 10 "type": "marker", 11 "coords": [ 12 34.683062, 13 135.822498 14 ] 15 } 16] 17} 18] 19
console.log responseは、data: Array(1), status: 200, statusText: "OK",と表示されていますので問題ないようなのですが、ypeError: Cannot read property 'forEach' of nullとエラーが出ており、うまくlayersに格納できていないと思われます。
よろしくお願いします。
参考にしたサイト
https://travishorn.com/interactive-maps-with-vue-leaflet-5430527353c8
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/09/19 21:03