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

回答編集履歴

3

多少DRYに

2016/10/11 08:02

投稿

退会済みユーザー
answer CHANGED
@@ -248,19 +248,19 @@
248
248
  var maps = document.getElementsByClassName('map');
249
249
 
250
250
  for (var i = 0; i < maps.length; i++) {
251
+
252
+ var latlng = {
253
+ lat: parseFloat(maps[i].getAttribute('data-lat'))
254
+ , lng: parseFloat(maps[i].getAttribute('data-lng'))
255
+ };
256
+
251
257
  map = new google.maps.Map(maps[i], {
252
- center: {
258
+ center: latlng,
253
- lat: parseFloat(maps[i].getAttribute('data-lat'))
254
- , lng: parseFloat(maps[i].getAttribute('data-lng'))
255
- },
256
259
  scrollwheel: false,
257
260
  zoom: 16
258
261
  });
259
262
  new google.maps.Marker({
260
- position: {
263
+ position: latlng,
261
- lat: parseFloat(maps[i].getAttribute('data-lat'))
262
- , lng: parseFloat(maps[i].getAttribute('data-lng'))
263
- },
264
264
  map: map
265
265
  });
266
266
  }

2

修正

2016/10/11 08:02

投稿

退会済みユーザー
answer CHANGED
@@ -267,7 +267,7 @@
267
267
 
268
268
  }
269
269
  </script>
270
- <script src="//maps.googleapis.com/maps/api/js?key=AIzaSyDyCBb_QDbedUEbak9SQUedh1cwxek7G08&callback=initMap&language=ja"></script>
270
+ <script src="//maps.googleapis.com/maps/api/js?key=API_MEY&callback=initMap&language=ja"></script>
271
271
  </body>
272
272
  </html>
273
273
 

1

追記

2016/10/11 05:57

投稿

退会済みユーザー
answer CHANGED
@@ -1,3 +1,274 @@
1
1
  [ホットペッパーAPIサンプル](http://hpapi.jlamp.net/?food=&budget=&credit_card=&large_service_area=&service_area=&name_kana=&keyword=%E6%9D%B1%E4%BA%AC%E9%83%BD&start=1)
2
2
 
3
- ![イメージ説明](cfc87c7d8d092979e692cb04255be0a1.png)
3
+ ![イメージ説明](cfc87c7d8d092979e692cb04255be0a1.png)
4
+
5
+ ---
6
+
7
+ ```php
8
+ <?php
9
+ /**
10
+ * index.php
11
+ *
12
+ * @since 2016/08/06
13
+ */
14
+ require_once './Hpepper.class.php';
15
+
16
+ /**
17
+ * エスケープ
18
+ * @param string $string
19
+ * @return string
20
+ */
21
+ function h($string)
22
+ {
23
+ return htmlspecialchars($string, ENT_QUOTES, 'utf-8');
24
+ }
25
+
26
+ // 料理カテゴリ
27
+ $food_category = Hpepper::getFoodCategory();
28
+
29
+ // 予算
30
+ $budget = Hpepper::getBudget();
31
+
32
+ // クレジットカード
33
+ $credit_card = Hpepper::getCreditCard();
34
+
35
+ // 大サービスエリア
36
+ $large_service_area = Hpepper::getLargeServiceArea();
37
+
38
+ // サービスエリア
39
+ $service_area = Hpepper::getServiceArea();
40
+
41
+ // レストラン検索
42
+ $restaurants = Hpepper::getRestaurants();
43
+ ?>
44
+ <!DOCTYPE HTML>
45
+ <html lang="ja">
46
+ <head>
47
+ <meta charset="UTF-8">
48
+ <title>ホットペッパーAPIテスト(アホでも設置するだけで動く)</title>
49
+ <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
50
+ <style type="text/css">
51
+ .map {
52
+ height: 300px;
53
+ }
54
+ .shop {
55
+ margin-bottom: 2em;
56
+ }
57
+ .shop > div {
58
+ border: 1px solid #CCC;
59
+ padding: 1em;
60
+ }
61
+ </style>
62
+ </head>
63
+ <body>
64
+ <div class="container">
65
+ <h1>ホットペッパーAPIサンプル</h1>
66
+
67
+ <form method="get">
68
+ <div class="row">
69
+ <div class="col-md-4">
70
+ <div class="form-group">
71
+ <label for="food_category">料理カテゴリ</label>
72
+ <select class="form-control" name="food" id="food_category">
73
+ <option value="">料理カテゴリ</option>
74
+ <?php foreach ($food_category->food_category as $food_category) : ?>
75
+ <?php if ($food_category->code == filter_input(INPUT_GET, 'food')): ?>
76
+ <option value="<?= h($food_category->code); ?>" selected="selected">
77
+ <?= h($food_category->name); ?>
78
+ </option>
79
+ <?php else: ?>
80
+ <option value="<?= h($food_category->code); ?>">
81
+ <?= h($food_category->name); ?>
82
+ </option>
83
+ <?php endif; ?>
84
+ <?php endforeach; ?>
85
+ </select>
86
+ </div>
87
+
88
+ <div class="form-group">
89
+ <label for="budget">予算</label>
90
+ <select class="form-control" name="budget" id="service_area">
91
+ <option value="">予算</option>
92
+ <?php foreach ($budget->budget as $budget) : ?>
93
+ <?php if ($budget->code == filter_input(INPUT_GET, 'budget')): ?>
94
+ <option value="<?= h($budget->code); ?>" selected="selected">
95
+ <?= h($budget->name); ?>
96
+ </option>
97
+ <?php else: ?>
98
+ <option value="<?= h($budget->code); ?>">
99
+ <?= h($budget->name); ?>
100
+ </option>
101
+ <?php endif; ?>
102
+ <?php endforeach; ?>
103
+ </select>
104
+ </div>
105
+
106
+ <div class="form-group">
107
+ <label for="credit_card">クレジットカード</label>
108
+ <select class="form-control" name="credit_card" id="credit_card">
109
+ <option value="">クレジットカード</option>
110
+ <?php foreach ($credit_card->credit_card as $credit_card) : ?>
111
+ <?php if ($credit_card->code == filter_input(INPUT_GET, 'credit_card')): ?>
112
+ <option value="<?= h($credit_card->code); ?>" selected="selected">
113
+ <?= h($credit_card->name); ?>
114
+ </option>
115
+ <?php else: ?>
116
+ <option value="<?= h($credit_card->code); ?>">
117
+ <?= h($credit_card->name); ?>
118
+ </option>
119
+ <?php endif; ?>
120
+ <?php endforeach; ?>
121
+ </select>
122
+ </div>
123
+ </div>
124
+
125
+ <div class="col-md-4">
126
+ <div class="form-group">
127
+ <label for="large_service_area">大サービスエリア</label>
128
+ <select class="form-control" name="large_service_area" id="large_service_area">
129
+ <option value="">大サービスエリア</option>
130
+ <?php foreach ($large_service_area->large_service_area as $large_service_area) : ?>
131
+ <?php if ($large_service_area->code == filter_input(INPUT_GET, 'large_service_area')): ?>
132
+ <option value="<?= h($large_service_area->code); ?>" selected="selected">
133
+ <?= h($large_service_area->name); ?>
134
+ </option>
135
+ <?php else: ?>
136
+ <option value="<?= h($large_service_area->code); ?>">
137
+ <?= h($large_service_area->name); ?>
138
+ </option>
139
+ <?php endif; ?>
140
+ <?php endforeach; ?>
141
+ </select>
142
+ </div>
143
+
144
+ <div class="form-group">
145
+ <label for="service_area">サービスエリア</label>
146
+ <select class="form-control" name="service_area" id="service_area">
147
+ <option value="">サービスエリア</option>
148
+ <?php foreach ($service_area->service_area as $pref) : ?>
149
+ <?php if ($pref->code == filter_input(INPUT_GET, 'service_area')): ?>
150
+ <option value="<?= h($pref->code); ?>" selected="selected">
151
+ <?= h($pref->name); ?>
152
+ </option>
153
+ <?php else: ?>
154
+ <option value="<?= h($pref->code); ?>">
155
+ <?= h($pref->name); ?>
156
+ </option>
157
+ <?php endif; ?>
158
+ <?php endforeach; ?>
159
+ </select>
160
+ </div>
161
+ </div>
162
+
163
+ <div class="col-md-4">
164
+ <div class="form-group">
165
+ <label for="name_kana">店名 or ふりがな</label>
166
+ <input class="form-control" type="text" name="name_kana" id="name_kana" value="<?= h(filter_input(INPUT_GET, 'name_kana')); ?>" />
167
+ </div>
168
+
169
+ <div class="form-group">
170
+ <label for="keyword">フリーワード</label>
171
+ <input class="form-control" type="text" name="keyword" id="keyword" value="<?= h(filter_input(INPUT_GET, 'keyword')); ?>" />
172
+ </div>
173
+
174
+ </div>
175
+ </div>
176
+
177
+ <div class="row">
178
+ <div class="col-md-12">
179
+ <div class="form-group">
180
+ <button type="submit" class="btn btn-primary">検索</button>
181
+ </div>
182
+ </div>
183
+ </div>
184
+ </form>
185
+ <?php if (is_object($restaurants)): ?>
186
+
187
+ <p>検索結果 <?= h(number_format((int) $restaurants->results_available)); ?> 件</p>
188
+
189
+ <?php if (isset($restaurants->error)): ?>
190
+
191
+ <div>
192
+ <?php foreach ($restaurants->error as $err) : ?>
193
+ <h2><?= $err->code; ?></h2>
194
+ <p><?= $err->message; ?></p>
195
+ <?php endforeach; ?>
196
+ </div>
197
+
198
+ <?php else: ?>
199
+
200
+ <?php $i = 0; ?>
201
+ <?php foreach ($restaurants->shop as $rest) : ?>
202
+ <?php if ($i % 2 == 0): ?>
203
+ <div class="row">
204
+ <?php endif; ?>
205
+ <div class="col-md-6 shop">
206
+ <div>
207
+ <dl>
208
+ <dt>ID</dt>
209
+ <dd><?= h($rest->id); ?></dd>
210
+
211
+ <dt>店名</dt>
212
+ <dd><?= h($rest->name); ?></dd>
213
+
214
+ <dt>ジャンル</dt>
215
+ <dd><?= h($rest->genre->name); ?></dd>
216
+
217
+ <dt>所在地</dt>
218
+ <dd><?= h($rest->address); ?></dd>
219
+ </dl>
220
+ <div class="map" data-lat="<?= h($rest->lat); ?>" data-lng="<?= h($rest->lng); ?>">
221
+ </div>
222
+ </div>
223
+ </div>
224
+ <?php if ($i % 2 == 1 || count($restaurants->shop) - 1 == $i): ?>
225
+ </div>
226
+ <?php endif; ?>
227
+ <?php $i++; ?>
228
+
229
+ <?php endforeach; ?>
230
+
231
+ <div class="row">
232
+ <div class="col-md-12">
233
+ <ul class="pagination pagination-sm no-margin">
234
+ <?= Hpepper::pagination($restaurants->results_available); ?>
235
+ </ul>
236
+ </div>
237
+ </div>
238
+
239
+ <?php endif; ?>
240
+ <?php endif; ?>
241
+
242
+ </div>
243
+
244
+ <script type="text/javascript" src="//www.google.com/jsapi"></script>
245
+ <script type="text/javascript">
246
+ var map;
247
+ function initMap() {
248
+ var maps = document.getElementsByClassName('map');
249
+
250
+ for (var i = 0; i < maps.length; i++) {
251
+ map = new google.maps.Map(maps[i], {
252
+ center: {
253
+ lat: parseFloat(maps[i].getAttribute('data-lat'))
254
+ , lng: parseFloat(maps[i].getAttribute('data-lng'))
255
+ },
256
+ scrollwheel: false,
257
+ zoom: 16
258
+ });
259
+ new google.maps.Marker({
260
+ position: {
261
+ lat: parseFloat(maps[i].getAttribute('data-lat'))
262
+ , lng: parseFloat(maps[i].getAttribute('data-lng'))
263
+ },
264
+ map: map
265
+ });
266
+ }
267
+
268
+ }
269
+ </script>
270
+ <script src="//maps.googleapis.com/maps/api/js?key=AIzaSyDyCBb_QDbedUEbak9SQUedh1cwxek7G08&callback=initMap&language=ja"></script>
271
+ </body>
272
+ </html>
273
+
274
+ ```