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

回答編集履歴

5

修正

2016/02/21 15:59

投稿

fagai
fagai

スコア2158

answer CHANGED
@@ -9,11 +9,11 @@
9
9
 
10
10
 
11
11
  var contentString;
12
- var infoWindow
12
+ var infowindow;
13
13
  for(var i = 0; i < shops.length; i++) {
14
14
  contentString = shops[i].title;
15
- infoWindow = new google.maps.InfoWindow({ content: contentString });
15
+ infowindow = new google.maps.InfoWindow({ content: contentString });
16
- google.maps.event.addListener(shops[i], 'click', function() { infoWindow.open(map,shops[i]); });
16
+ google.maps.event.addListener(shops[i], 'click', function() { infowindow.open(map,shops[i]); });
17
17
  }
18
18
  ```
19
19
 
@@ -25,8 +25,8 @@
25
25
  for(var i = 0; i < shops.length; i++) {
26
26
  (function(){
27
27
  var contentString = shops[i].title;
28
- var infoWindow = new google.maps.InfoWindow({ content: contentString });
28
+ var infowindow = new google.maps.InfoWindow({ content: contentString });
29
- google.maps.event.addListener(shops[i], 'click', function() { infoWindow.open(map,shops[i]); });
29
+ google.maps.event.addListener(shops[i], 'click', function() { infowindow.open(map,shops[i]); });
30
30
  })();
31
31
  }
32
32
  ```

4

修正

2016/02/21 15:59

投稿

fagai
fagai

スコア2158

answer CHANGED
@@ -7,11 +7,26 @@
7
7
  shops.push(new google.maps.Marker({ position: new google.maps.LatLng(座標), map: map, icon: 'images/icon1.png', title: '場所2' }));
8
8
  // 省略...
9
9
 
10
+
11
+ var contentString;
12
+ var infoWindow
10
13
  for(var i = 0; i < shops.length; i++) {
11
- var contentString = shops[i].title;
14
+ contentString = shops[i].title;
12
- var infowindow = new google.maps.InfoWindow({ content: contentString });
15
+ infoWindow = new google.maps.InfoWindow({ content: contentString });
13
- google.maps.event.addListener(shops[i], 'click', function() { infowindow.open(map,shops[i]); });
16
+ google.maps.event.addListener(shops[i], 'click', function() { infoWindow.open(map,shops[i]); });
14
17
  }
15
18
  ```
16
19
 
17
- このように配列にすることで、複数のshopが増えても対応できると思います。
20
+ このように配列にすることで、複数のshopが増えても対応できると思います。
21
+
22
+ 追記
23
+ もしかしたら修正部分は以下の形式でも行けるかもです。
24
+ ```
25
+ for(var i = 0; i < shops.length; i++) {
26
+ (function(){
27
+ var contentString = shops[i].title;
28
+ var infoWindow = new google.maps.InfoWindow({ content: contentString });
29
+ google.maps.event.addListener(shops[i], 'click', function() { infoWindow.open(map,shops[i]); });
30
+ })();
31
+ }
32
+ ```

3

ソース修正

2016/02/21 15:55

投稿

fagai
fagai

スコア2158

answer CHANGED
@@ -3,8 +3,8 @@
3
3
  ```javascript
4
4
  var shops = [];
5
5
 
6
- shops[] = new google.maps.Marker({ position: new google.maps.LatLng(座標), map: map, icon: 'images/icon1.png', title: '場所1' });
6
+ shops.push(new google.maps.Marker({ position: new google.maps.LatLng(座標), map: map, icon: 'images/icon1.png', title: '場所1' }));
7
- shops[] = new google.maps.Marker({ position: new google.maps.LatLng(座標), map: map, icon: 'images/icon1.png', title: '場所2' });
7
+ shops.push(new google.maps.Marker({ position: new google.maps.LatLng(座標), map: map, icon: 'images/icon1.png', title: '場所2' }));
8
8
  // 省略...
9
9
 
10
10
  for(var i = 0; i < shops.length; i++) {

2

修正

2016/02/21 15:07

投稿

fagai
fagai

スコア2158

answer CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  shops[] = new google.maps.Marker({ position: new google.maps.LatLng(座標), map: map, icon: 'images/icon1.png', title: '場所1' });
7
7
  shops[] = new google.maps.Marker({ position: new google.maps.LatLng(座標), map: map, icon: 'images/icon1.png', title: '場所2' });
8
- ...
8
+ // 省略...
9
9
 
10
10
  for(var i = 0; i < shops.length; i++) {
11
11
  var contentString = shops[i].title;

1

修正

2016/02/21 13:32

投稿

fagai
fagai

スコア2158

answer CHANGED
@@ -1,4 +1,4 @@
1
- shop1,shop2のようにする必要がなければ以下の様な対応方法があると思います。
1
+ shop1,shop2のような変数名にする必要がなければ以下の様な対応方法があると思います。
2
2
 
3
3
  ```javascript
4
4
  var shops = [];