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

回答編集履歴

1

補足にあわせて、サンプルコードを変更

2016/03/05 12:53

投稿

CHERRY
CHERRY

スコア25234

answer CHANGED
@@ -62,4 +62,65 @@
62
62
  <script src="https://maps.googleapis.com/maps/api/js?callback=viewMap"></script>
63
63
  </body>
64
64
  </html>
65
- ```
65
+ ```
66
+ --
67
+ 追記 (2016.03.05 21:45)
68
+
69
+ 興味を持ったので、サンプルを Google Spreadsheet から、住所を取り出すようにしてみました。
70
+ KEY の部分は、Google Spreadsheet の ID です。
71
+
72
+ ```HTML
73
+ <!DOCTYPE HTML>
74
+ <html lang="ja">
75
+ <head>
76
+ <meta charset="UTF-8">
77
+ <title>Google Maps テスト</title>
78
+ <style type="text/css">
79
+ #googlemap {
80
+ width: 700px;
81
+ height: 700px;
82
+ margin: 0 auto;
83
+ }
84
+ </style>
85
+ </head>
86
+ <body>
87
+ <div id="googlemap"></div>
88
+ <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
89
+ <script type="text/javascript">
90
+ var googleSheetUrl = "https://spreadsheets.google.com/feeds/cells/[KEY]/od6/public/basic?alt=json";
91
+ var map;
92
+ var marker = [];
93
+
94
+ function viewMap() {
95
+ var latlng = new google.maps.LatLng({lat: 35.693, lng: 139.735});
96
+ map = new google.maps.Map(document.getElementById('googlemap'), {
97
+ center: latlng,
98
+ zoom: 13
99
+ });
100
+ var geocoder = new google.maps.Geocoder();
101
+ $.get({
102
+ url: googleSheetUrl,
103
+ cache: false,
104
+ dataType: "jsonp",
105
+ success : function(data, status){
106
+ $.each(data.feed.entry, function(i, item){
107
+ geocoder.geocode(
108
+ { address: item.content.$t },
109
+ function(result, status){
110
+ if(status==google.maps.GeocoderStatus.OK){
111
+ marker[i] = new google.maps.Marker({
112
+ position: result[0].geometry.location,
113
+ map: map
114
+ });
115
+ }
116
+ }
117
+ );
118
+ });
119
+ }
120
+ });
121
+ }
122
+ </script>
123
+ <script src="https://maps.googleapis.com/maps/api/js?callback=viewMap"></script>
124
+ </body>
125
+ </html>
126
+ ```