質問編集履歴

2

解決しましたがソースコードが長いので追記させていただきます。

2019/12/15 02:43

投稿

masahirori
masahirori

スコア5

test CHANGED
File without changes
test CHANGED
@@ -30,7 +30,7 @@
30
30
 
31
31
 
32
32
 
33
- var markerData = [ // マーカーを立てる場所名・緯度・経度
33
+ var markerData = [ // マーカーを立てる場所名・住所
34
34
 
35
35
  {
36
36
 
@@ -123,3 +123,109 @@
123
123
  }
124
124
 
125
125
  ```
126
+
127
+
128
+
129
+ ### 解決時のソースコード
130
+
131
+ 解決いたしました。mapに適当な初期値を入れて初期化しておくと上手くできました。
132
+
133
+ ```javaScript
134
+
135
+ //markerDataまでは同じです。
136
+
137
+
138
+
139
+ //この関数でmapを初期化しておきます。
140
+
141
+ //Google Maps PlatformドキュメントのMaps JavaScript API/Geocodingに掲載されているものです。
142
+
143
+
144
+
145
+ function initialize() {
146
+
147
+ geocoder = new google.maps.Geocoder();
148
+
149
+ var latlng = new google.maps.LatLng(-34.397, 150.644);
150
+
151
+ var mapOptions = {
152
+
153
+ zoom: 14,
154
+
155
+ center: latlng
156
+
157
+ }
158
+
159
+ map = new google.maps.Map(document.getElementById('map'), mapOptions);
160
+
161
+ }
162
+
163
+
164
+
165
+ function initMap() {
166
+
167
+
168
+
169
+ initialize();//マップの初期化
170
+
171
+
172
+
173
+ for (let i = 0; i < markerData.length; i++) {//ピンを多数立てるためにリストの数だけ回す
174
+
175
+ geocoder.geocode({
176
+
177
+ "address" : markerData[i].address
178
+
179
+ }, function(results, status) { // 結果
180
+
181
+ if (status === google.maps.GeocoderStatus.OK) { // ステータスがOKの場合
182
+
183
+ let bounds = new google.maps.LatLngBounds();
184
+
185
+ latlng = results[0].geometry.location;
186
+
187
+ bounds.extend(latlng);
188
+
189
+
190
+
191
+ //簡易的にしていますが本来はマップの中心に持ってきたい座標でのみmap.setCenter(latlng);をするべきだと思います。
192
+
193
+ if(i === 0){
194
+
195
+ map.setCenter(latlng);
196
+
197
+ }
198
+
199
+
200
+
201
+ //マーカーを立てる
202
+
203
+ marker[i] = new google.maps.Marker({
204
+
205
+ position: latlng,
206
+
207
+ map: map
208
+
209
+ });
210
+
211
+
212
+
213
+ } else { // 失敗した場合
214
+
215
+ console.group('Error');
216
+
217
+ console.log(results);
218
+
219
+ console.log(status);
220
+
221
+ }
222
+
223
+
224
+
225
+ });
226
+
227
+ }
228
+
229
+ }
230
+
231
+ ```

1

ソースコード最後の}が抜けていたので付け足しました。

2019/12/15 02:43

投稿

masahirori
masahirori

スコア5

test CHANGED
File without changes
test CHANGED
@@ -120,4 +120,6 @@
120
120
 
121
121
  }
122
122
 
123
+ }
124
+
123
125
  ```