質問するログイン新規登録

回答編集履歴

1

修正

2019/04/09 08:12

投稿

annderber
annderber

スコア98

answer CHANGED
@@ -1,4 +1,4 @@
1
- コード量が長くなるので、こちらに記述いたしま
1
+ 再度修正しました
2
2
 
3
3
  ```JavaScript
4
4
  <!DOCTYPE html>
@@ -18,146 +18,29 @@
18
18
  </head>
19
19
  <body>
20
20
  <div id="map" style="width: 600px;height: 500px"></div>
21
+
21
22
  <script>
22
- var tiles = L.tileLayer('https://{s}.tile.osm.org/{z}/{x}/{y}.png', {
23
+ var tiles = L.tileLayer('https://{s}.tile.osm.org/{z}/{x}/{y}.png', {
23
- maxZoom: 14,
24
+ maxZoom: 14,
24
- attribution: '&copy; <a href="https://osm.org/copyright">OpenStreetMap</a> contributors'
25
+ attribution: '&copy; <a href="https://osm.org/copyright">OpenStreetMap</a> contributors'
25
- });
26
+ });
26
27
 
27
- var map = L.map('map')
28
+ var map = L.map('map')
28
- .addLayer(tiles);
29
+ .addLayer(tiles);
29
30
 
30
- var markers = L.markerClusterGroup({
31
+ var markers = L.markerClusterGroup({
31
- showCoverageOnHover: true,
32
+ showCoverageOnHover: true,
32
- spiderfyOnMaxZoom: false,
33
+ spiderfyOnMaxZoom: false,
33
- removeOutsideVisibleBounds: true,
34
+ removeOutsideVisibleBounds: true,
34
- disableClusteringAtZoom: 8
35
+ disableClusteringAtZoom: 8
35
- }
36
+ }
36
- );
37
+ );
37
- const data = {
38
- "type": "FeatureCollection",
39
- "features": [
40
- {
41
- "type": "Feature",
42
- "properties": {
43
- "marker-color": "#7e7e7e",
44
- "marker-size": "medium",
45
- "marker-symbol": "",
46
- "popup": "あああああああ"
47
- },
48
- "geometry": {
49
- "type": "Point",
50
- "coordinates": [
51
- 138.78552,
52
- 37.663027
53
- ]
54
- }
55
- },
56
- {
57
- "type": "Feature",
58
- "properties": {},
59
- "geometry": {
60
- "type": "Point",
61
- "coordinates": [
62
- 138.802069,
63
- 37.653211
64
- ]
65
- }
66
- },
67
- {
68
- "type": "Feature",
69
- "properties": {},
70
- "geometry": {
71
- "type": "Point",
72
- "coordinates": [
73
- 138.819922,
74
- 37.64502
75
- ]
76
- }
77
- },
78
- {
79
- "type": "Feature",
80
- "properties": {},
81
- "geometry": {
82
- "type": "Point",
83
- "coordinates": [
84
- 138.826511,
85
- 37.636968
86
- ]
87
- }
88
- },
89
- {
90
- "type": "Feature",
91
- "properties": {},
92
- "geometry": {
93
- "type": "Point",
94
- "coordinates": [
95
- 138.826649,
96
- 37.626823
97
- ]
98
- }
99
- },
100
- {
101
- "type": "Feature",
102
- "properties": {},
103
- "geometry": {
104
- "type": "Point",
105
- "coordinates": [
106
- 138.834801,
107
- 37.626811
108
- ]
109
- }
110
- },
111
- {
112
- "type": "Feature",
113
- "properties": {},
114
- "geometry": {
115
- "type": "Point",
116
- "coordinates": [
117
- 138.83981,
118
- 37.61816
119
- ]
120
- }
121
- },
122
- {
123
- "type": "Feature",
124
- "properties": {},
125
- "geometry": {
126
- "type": "Point",
127
- "coordinates": [
128
- 138.832363,
129
- 37.6159
130
- ]
131
- }
132
- },
133
- {
134
- "type": "Feature",
135
- "properties": {
136
- "stroke": "#d10004",
137
- "stroke-width": 10,
138
- "stroke-opacity": 1
139
- },
140
- "geometry": {
141
- "type": "LineString",
142
- "coordinates": [
143
- [
144
- 138.77723693847656,
145
- 37.66670097960347
146
- ],
147
- [
148
- 138.78976821899414,
149
- 37.662488471331706
150
- ]
151
- ]
152
- }
153
- }
154
- ]
155
- };
156
- // httpObj = new XMLHttpRequest();
157
- // httpObj.open("get", "hoge.geojson", function(data){
158
- // GeoJSON.parse(data);
159
38
 
39
+ httpObj = new XMLHttpRequest();
40
+ httpObj.open("GET", "hoge.json", true);
41
+ httpObj.addEventListener('load', function(res){
42
+ const data = JSON.parse(res.currentTarget.response);
160
- var geoJsonLayer = L.geoJson(data, {
43
+ var geoJsonLayer = L.geoJson(data, {
161
44
  onEachFeature: function (feature, layer) {
162
45
  layer.bindPopup(feature.properties.popup);
163
46
  },
@@ -185,15 +68,12 @@
185
68
  map.fitBounds(markers.getBounds());
186
69
  map.setView([35.156, 136.890], 8);
187
70
 
188
- // httpObj.send(null);
189
- // });
71
+ });
190
72
 
73
+ httpObj.send(null)
191
74
  </script>
192
75
  </body>
193
76
  </html>
194
77
  ```
195
78
 
196
-
197
- 投稿者様のスクリプトにjsonデータのままコピペて、XMLHttpRequest部分をコメントアウトしています
79
+ XMLHttpRequest行った場合で処理で編集し直しました。ファイルパスは変更してください。
198
- 恐らくこのままコピペすれば表示されると思います。
199
- なので、取得の部分で躓いているのでは無いかと思いますがいかがでしょうか