質問編集履歴
10
解決
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
※解決しました!
|
2
|
+
|
3
|
+
|
4
|
+
|
1
5
|
除外がうまくいかないので質問させてください。
|
2
6
|
|
3
7
|
|
9
エラーなし
test
CHANGED
File without changes
|
test
CHANGED
@@ -40,232 +40,218 @@
|
|
40
40
|
|
41
41
|
|
42
42
|
|
43
|
+
解決方法を教えていただきたいです。
|
44
|
+
|
43
|
-
|
45
|
+
よろしくお願いします。
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
|
51
|
+
require 'net/https'
|
52
|
+
|
53
|
+
require 'uri'
|
54
|
+
|
55
|
+
require 'json'
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
API_KEY = 'Your API Key'
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
lat = '3.152917' #中心座標の緯度
|
64
|
+
|
65
|
+
lng = '101.7038288' #中心座標の経度
|
66
|
+
|
67
|
+
rad = '5000' #中心座標の半径(m)
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
#カテゴリー切り替え
|
72
|
+
|
73
|
+
types = 'convenience_store'
|
74
|
+
|
75
|
+
# types = 'grocery_or_supermarket'
|
76
|
+
|
77
|
+
# types = 'shopping_mall'
|
78
|
+
|
79
|
+
# types = 'train_station'
|
80
|
+
|
81
|
+
# types = 'light_rail_station'
|
82
|
+
|
83
|
+
# types = 'subway_station'
|
84
|
+
|
85
|
+
# types = 'university'
|
86
|
+
|
87
|
+
# types = 'bank'
|
88
|
+
|
89
|
+
# types = 'hospital'
|
90
|
+
|
91
|
+
# types = 'doctor'
|
92
|
+
|
93
|
+
# types = 'mosque'
|
94
|
+
|
95
|
+
# types = 'church'
|
96
|
+
|
97
|
+
# types = 'hindu_temple'
|
98
|
+
|
99
|
+
# types = 'police'
|
100
|
+
|
101
|
+
# types = 'gas_station'
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
File.open('./place.json','w'){|file| file = nil} #前に出力した内容を削除
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
uri = URI.parse "https://maps.googleapis.com/maps/api/place/radarsearch/json?location=#{lat},#{lng}&radius=#{rad}&types=#{types}&language=en&key=#{API_KEY}"
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
request = Net::HTTP::Get.new(uri.request_uri)
|
114
|
+
|
115
|
+
response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
|
116
|
+
|
117
|
+
http.request(request)
|
118
|
+
|
119
|
+
end
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
body = JSON.parse response.body
|
124
|
+
|
125
|
+
results = body['results']
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
results.each do |result|
|
130
|
+
|
131
|
+
place_id = result['place_id']
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
uri2 = URI.parse "https://maps.googleapis.com/maps/api/place/details/json?placeid=#{place_id}&language=en&key=#{API_KEY}"
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
request = Net::HTTP::Get.new(uri2.request_uri)
|
140
|
+
|
141
|
+
response = Net::HTTP.start(uri2.host, uri2.port, use_ssl: uri2.scheme == 'https') do |http|
|
142
|
+
|
143
|
+
http.request(request)
|
144
|
+
|
145
|
+
end
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
detail = JSON.parse response.body
|
150
|
+
|
151
|
+
place_detail = detail['result']
|
152
|
+
|
153
|
+
location = place_detail['geometry']['location']
|
154
|
+
|
155
|
+
address = place_detail['address_components']
|
156
|
+
|
157
|
+
check_items = ["street_number", "route", "locality", "administrative_area_level_2", "administrative_area_level_1", "country"]
|
158
|
+
|
159
|
+
place = {}
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
address.each do |element|
|
164
|
+
|
165
|
+
result = element["types"] & check_items
|
166
|
+
|
167
|
+
next if result.empty?
|
168
|
+
|
169
|
+
place[result[0]] = element["long_name"]
|
170
|
+
|
171
|
+
end
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
answer = {
|
176
|
+
|
177
|
+
:name => place_detail['name'],
|
178
|
+
|
179
|
+
:lat => location['lat'],
|
180
|
+
|
181
|
+
:lng => location['lng'],
|
182
|
+
|
183
|
+
:formatted_address => place_detail['formatted_address'],
|
184
|
+
|
185
|
+
:street_number => place['street_number'],
|
186
|
+
|
187
|
+
:route => place['route'],
|
188
|
+
|
189
|
+
:locality => place['locality'],
|
190
|
+
|
191
|
+
:administrative_area_level_2 => place['administrative_area_level_2'],
|
192
|
+
|
193
|
+
:administrative_area_level_1 => place['administrative_area_level_1'],
|
194
|
+
|
195
|
+
:country => place['country'],
|
196
|
+
|
197
|
+
:types => place_detail['types']
|
198
|
+
|
199
|
+
}
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
#不要なtypesを取り除く
|
204
|
+
|
205
|
+
list = ['establishment', 'point_of_interest']
|
206
|
+
|
207
|
+
place_detail['types'].delete_if do |str|
|
208
|
+
|
209
|
+
list.include?(str)
|
210
|
+
|
211
|
+
end
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
#不要なデータを除外
|
216
|
+
|
217
|
+
a = place_detail['name'].match(/\satm|atm\s/i)
|
218
|
+
|
219
|
+
b = (['grocery_or_supermarket', 'convenience_store'] - place_detail['types']).empty?
|
220
|
+
|
221
|
+
c = place_detail['name'].match(/7-11|7\sEleven|7eleven|7-Eleven/i)
|
222
|
+
|
223
|
+
d = place_detail['types'] == 'bank'
|
224
|
+
|
225
|
+
e = place_detail['types'] == 'shopping_mall'
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
if types == 'grocery_or_supermarket'
|
230
|
+
|
231
|
+
if ( b || c )
|
232
|
+
|
233
|
+
answer.clear
|
234
|
+
|
235
|
+
end
|
236
|
+
|
237
|
+
end
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
if (place['country'] == "Malaysia")
|
242
|
+
|
243
|
+
unless ( a && !d ) || ( c && !e ) || answer == {}
|
244
|
+
|
245
|
+
File.open('./place.json', 'a') do |file|
|
246
|
+
|
247
|
+
file.puts JSON.pretty_generate(answer)
|
248
|
+
|
249
|
+
end
|
250
|
+
|
251
|
+
end
|
252
|
+
|
253
|
+
end
|
254
|
+
|
255
|
+
end
|
44
256
|
|
45
257
|
```
|
46
|
-
|
47
|
-
lace.rb:58:in `block in <main>': undefined method `[]' for nil:NilClass (NoMethodError)
|
48
|
-
|
49
|
-
from place.rb:46:in `each'
|
50
|
-
|
51
|
-
from place.rb:46:in `<main>'
|
52
|
-
|
53
|
-
```
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
これらの解決方法を教えていただきたいです。
|
58
|
-
|
59
|
-
よろしくお願いします。
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
```ruby
|
64
|
-
|
65
|
-
require 'net/https'
|
66
|
-
|
67
|
-
require 'uri'
|
68
|
-
|
69
|
-
require 'json'
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
API_KEY = 'Your API Key'
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
lat = '3.152917' #中心座標の緯度
|
78
|
-
|
79
|
-
lng = '101.7038288' #中心座標の経度
|
80
|
-
|
81
|
-
rad = '5000' #中心座標の半径(m)
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
#カテゴリー切り替え
|
86
|
-
|
87
|
-
types = 'convenience_store'
|
88
|
-
|
89
|
-
# types = 'grocery_or_supermarket'
|
90
|
-
|
91
|
-
# types = 'shopping_mall'
|
92
|
-
|
93
|
-
# types = 'train_station'
|
94
|
-
|
95
|
-
# types = 'light_rail_station'
|
96
|
-
|
97
|
-
# types = 'subway_station'
|
98
|
-
|
99
|
-
# types = 'university'
|
100
|
-
|
101
|
-
# types = 'bank'
|
102
|
-
|
103
|
-
# types = 'hospital'
|
104
|
-
|
105
|
-
# types = 'doctor'
|
106
|
-
|
107
|
-
# types = 'mosque'
|
108
|
-
|
109
|
-
# types = 'church'
|
110
|
-
|
111
|
-
# types = 'hindu_temple'
|
112
|
-
|
113
|
-
# types = 'police'
|
114
|
-
|
115
|
-
# types = 'gas_station'
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
File.open('./place.json','w'){|file| file = nil} #前に出力した内容を削除
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
uri = URI.parse "https://maps.googleapis.com/maps/api/place/radarsearch/json?location=#{lat},#{lng}&radius=#{rad}&types=#{types}&language=en&key=#{API_KEY}"
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
request = Net::HTTP::Get.new(uri.request_uri)
|
128
|
-
|
129
|
-
response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
|
130
|
-
|
131
|
-
http.request(request)
|
132
|
-
|
133
|
-
end
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
body = JSON.parse response.body
|
138
|
-
|
139
|
-
results = body['results']
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
results.each do |result|
|
144
|
-
|
145
|
-
place_id = result['place_id']
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
uri2 = URI.parse "https://maps.googleapis.com/maps/api/place/details/json?placeid=#{place_id}&language=en&key=#{API_KEY}"
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
request = Net::HTTP::Get.new(uri2.request_uri)
|
154
|
-
|
155
|
-
response = Net::HTTP.start(uri2.host, uri2.port, use_ssl: uri2.scheme == 'https') do |http|
|
156
|
-
|
157
|
-
http.request(request)
|
158
|
-
|
159
|
-
end
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
detail = JSON.parse response.body
|
164
|
-
|
165
|
-
place_detail = detail['result']
|
166
|
-
|
167
|
-
location = place_detail['geometry']['location']
|
168
|
-
|
169
|
-
address = place_detail['address_components']
|
170
|
-
|
171
|
-
check_items = ["street_number", "route", "locality", "administrative_area_level_2", "administrative_area_level_1", "country"]
|
172
|
-
|
173
|
-
place = {}
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
address.each do |element|
|
178
|
-
|
179
|
-
result = element["types"] & check_items
|
180
|
-
|
181
|
-
next if result.empty?
|
182
|
-
|
183
|
-
place[result[0]] = element["long_name"]
|
184
|
-
|
185
|
-
end
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
answer = {
|
190
|
-
|
191
|
-
:name => place_detail['name'],
|
192
|
-
|
193
|
-
:lat => location['lat'],
|
194
|
-
|
195
|
-
:lng => location['lng'],
|
196
|
-
|
197
|
-
:formatted_address => place_detail['formatted_address'],
|
198
|
-
|
199
|
-
:street_number => place['street_number'],
|
200
|
-
|
201
|
-
:route => place['route'],
|
202
|
-
|
203
|
-
:locality => place['locality'],
|
204
|
-
|
205
|
-
:administrative_area_level_2 => place['administrative_area_level_2'],
|
206
|
-
|
207
|
-
:administrative_area_level_1 => place['administrative_area_level_1'],
|
208
|
-
|
209
|
-
:country => place['country'],
|
210
|
-
|
211
|
-
:types => place_detail['types']
|
212
|
-
|
213
|
-
}
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
#不要なtypesを取り除く
|
218
|
-
|
219
|
-
list = ['establishment', 'point_of_interest']
|
220
|
-
|
221
|
-
place_detail['types'].delete_if do |str|
|
222
|
-
|
223
|
-
list.include?(str)
|
224
|
-
|
225
|
-
end
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
#不要なデータを除外
|
230
|
-
|
231
|
-
a = place_detail['name'].match(/\satm|atm\s/i)
|
232
|
-
|
233
|
-
b = (['grocery_or_supermarket', 'convenience_store'] - place_detail['types']).empty?
|
234
|
-
|
235
|
-
c = place_detail['name'].match(/7-11|7\sEleven|7eleven|7-Eleven/i)
|
236
|
-
|
237
|
-
d = place_detail['types'] == 'bank'
|
238
|
-
|
239
|
-
e = place_detail['types'] == 'shopping_mall'
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
if types == 'grocery_or_supermarket'
|
244
|
-
|
245
|
-
if ( b || c )
|
246
|
-
|
247
|
-
answer.clear
|
248
|
-
|
249
|
-
end
|
250
|
-
|
251
|
-
end
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
if (place['country'] == "Malaysia")
|
256
|
-
|
257
|
-
unless ( a && !d ) || ( c && !e ) || answer == {}
|
258
|
-
|
259
|
-
File.open('./place.json', 'a') do |file|
|
260
|
-
|
261
|
-
file.puts JSON.pretty_generate(answer)
|
262
|
-
|
263
|
-
end
|
264
|
-
|
265
|
-
end
|
266
|
-
|
267
|
-
end
|
268
|
-
|
269
|
-
end
|
270
|
-
|
271
|
-
```
|
8
訂正
test
CHANGED
File without changes
|
test
CHANGED
@@ -36,7 +36,7 @@
|
|
36
36
|
|
37
37
|
|
38
38
|
|
39
|
-
の部分がうまくいかず、convenience storeの際も
|
39
|
+
の部分がうまくいかず、convenience storeの際もcの条件を除外してしまいます。
|
40
40
|
|
41
41
|
|
42
42
|
|
7
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -74,10 +74,6 @@
|
|
74
74
|
|
75
75
|
|
76
76
|
|
77
|
-
require 'net/https'
|
78
|
-
|
79
|
-
|
80
|
-
|
81
77
|
lat = '3.152917' #中心座標の緯度
|
82
78
|
|
83
79
|
lng = '101.7038288' #中心座標の経度
|
6
内容変更
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
Rubyの条件分岐について
|
1
|
+
Rubyの条件分岐時のエラーについて
|
test
CHANGED
@@ -1,9 +1,3 @@
|
|
1
|
-
※解決しました。
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
1
|
除外がうまくいかないので質問させてください。
|
8
2
|
|
9
3
|
|
@@ -42,240 +36,240 @@
|
|
42
36
|
|
43
37
|
|
44
38
|
|
39
|
+
の部分がうまくいかず、convenience storeの際も7-11を表示してしまいます。
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
また下記のようなエラーも発生します。
|
44
|
+
|
45
|
+
```
|
46
|
+
|
47
|
+
lace.rb:58:in `block in <main>': undefined method `[]' for nil:NilClass (NoMethodError)
|
48
|
+
|
49
|
+
from place.rb:46:in `each'
|
50
|
+
|
51
|
+
from place.rb:46:in `<main>'
|
52
|
+
|
53
|
+
```
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
これらの解決方法を教えていただきたいです。
|
58
|
+
|
45
|
-
|
59
|
+
よろしくお願いします。
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
一旦grocery_or_supermarketでconvenience storeに該当するものは空にし、その後空のものは表示させないといったような内容にしているつもりなのですが、
|
50
60
|
|
51
61
|
|
52
62
|
|
53
63
|
```ruby
|
54
64
|
|
65
|
+
require 'net/https'
|
66
|
+
|
67
|
+
require 'uri'
|
68
|
+
|
69
|
+
require 'json'
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
API_KEY = 'Your API Key'
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
require 'net/https'
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
lat = '3.152917' #中心座標の緯度
|
82
|
+
|
83
|
+
lng = '101.7038288' #中心座標の経度
|
84
|
+
|
85
|
+
rad = '5000' #中心座標の半径(m)
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
#カテゴリー切り替え
|
90
|
+
|
91
|
+
types = 'convenience_store'
|
92
|
+
|
93
|
+
# types = 'grocery_or_supermarket'
|
94
|
+
|
95
|
+
# types = 'shopping_mall'
|
96
|
+
|
97
|
+
# types = 'train_station'
|
98
|
+
|
99
|
+
# types = 'light_rail_station'
|
100
|
+
|
101
|
+
# types = 'subway_station'
|
102
|
+
|
103
|
+
# types = 'university'
|
104
|
+
|
105
|
+
# types = 'bank'
|
106
|
+
|
107
|
+
# types = 'hospital'
|
108
|
+
|
109
|
+
# types = 'doctor'
|
110
|
+
|
111
|
+
# types = 'mosque'
|
112
|
+
|
113
|
+
# types = 'church'
|
114
|
+
|
115
|
+
# types = 'hindu_temple'
|
116
|
+
|
117
|
+
# types = 'police'
|
118
|
+
|
119
|
+
# types = 'gas_station'
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
File.open('./place.json','w'){|file| file = nil} #前に出力した内容を削除
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
uri = URI.parse "https://maps.googleapis.com/maps/api/place/radarsearch/json?location=#{lat},#{lng}&radius=#{rad}&types=#{types}&language=en&key=#{API_KEY}"
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
request = Net::HTTP::Get.new(uri.request_uri)
|
132
|
+
|
133
|
+
response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
|
134
|
+
|
135
|
+
http.request(request)
|
136
|
+
|
137
|
+
end
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
body = JSON.parse response.body
|
142
|
+
|
143
|
+
results = body['results']
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
results.each do |result|
|
148
|
+
|
149
|
+
place_id = result['place_id']
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
uri2 = URI.parse "https://maps.googleapis.com/maps/api/place/details/json?placeid=#{place_id}&language=en&key=#{API_KEY}"
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
request = Net::HTTP::Get.new(uri2.request_uri)
|
158
|
+
|
159
|
+
response = Net::HTTP.start(uri2.host, uri2.port, use_ssl: uri2.scheme == 'https') do |http|
|
160
|
+
|
161
|
+
http.request(request)
|
162
|
+
|
163
|
+
end
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
detail = JSON.parse response.body
|
168
|
+
|
169
|
+
place_detail = detail['result']
|
170
|
+
|
171
|
+
location = place_detail['geometry']['location']
|
172
|
+
|
173
|
+
address = place_detail['address_components']
|
174
|
+
|
175
|
+
check_items = ["street_number", "route", "locality", "administrative_area_level_2", "administrative_area_level_1", "country"]
|
176
|
+
|
177
|
+
place = {}
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
address.each do |element|
|
182
|
+
|
183
|
+
result = element["types"] & check_items
|
184
|
+
|
185
|
+
next if result.empty?
|
186
|
+
|
187
|
+
place[result[0]] = element["long_name"]
|
188
|
+
|
189
|
+
end
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
answer = {
|
194
|
+
|
195
|
+
:name => place_detail['name'],
|
196
|
+
|
197
|
+
:lat => location['lat'],
|
198
|
+
|
199
|
+
:lng => location['lng'],
|
200
|
+
|
201
|
+
:formatted_address => place_detail['formatted_address'],
|
202
|
+
|
203
|
+
:street_number => place['street_number'],
|
204
|
+
|
205
|
+
:route => place['route'],
|
206
|
+
|
207
|
+
:locality => place['locality'],
|
208
|
+
|
209
|
+
:administrative_area_level_2 => place['administrative_area_level_2'],
|
210
|
+
|
211
|
+
:administrative_area_level_1 => place['administrative_area_level_1'],
|
212
|
+
|
213
|
+
:country => place['country'],
|
214
|
+
|
215
|
+
:types => place_detail['types']
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
#不要なtypesを取り除く
|
222
|
+
|
223
|
+
list = ['establishment', 'point_of_interest']
|
224
|
+
|
225
|
+
place_detail['types'].delete_if do |str|
|
226
|
+
|
227
|
+
list.include?(str)
|
228
|
+
|
229
|
+
end
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
#不要なデータを除外
|
234
|
+
|
235
|
+
a = place_detail['name'].match(/\satm|atm\s/i)
|
236
|
+
|
237
|
+
b = (['grocery_or_supermarket', 'convenience_store'] - place_detail['types']).empty?
|
238
|
+
|
239
|
+
c = place_detail['name'].match(/7-11|7\sEleven|7eleven|7-Eleven/i)
|
240
|
+
|
241
|
+
d = place_detail['types'] == 'bank'
|
242
|
+
|
243
|
+
e = place_detail['types'] == 'shopping_mall'
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
if types == 'grocery_or_supermarket'
|
248
|
+
|
249
|
+
if ( b || c )
|
250
|
+
|
251
|
+
answer.clear
|
252
|
+
|
253
|
+
end
|
254
|
+
|
255
|
+
end
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
if (place['country'] == "Malaysia")
|
260
|
+
|
55
|
-
unless ( a && !d ) || ( c && !e ) || answer == {}
|
261
|
+
unless ( a && !d ) || ( c && !e ) || answer == {}
|
262
|
+
|
263
|
+
File.open('./place.json', 'a') do |file|
|
264
|
+
|
265
|
+
file.puts JSON.pretty_generate(answer)
|
266
|
+
|
267
|
+
end
|
268
|
+
|
269
|
+
end
|
270
|
+
|
271
|
+
end
|
272
|
+
|
273
|
+
end
|
56
274
|
|
57
275
|
```
|
58
|
-
|
59
|
-
の部分でor検索がうまくいかずanswerで空にしたはずのものが表示されてしまいます。
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
一旦空にする必要がなければ、grocery_or_supermarketで検索した際にconvenience storeに該当するものがあれば表示しないといった方法のほうがいいです。
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
よろしくお願いします。
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
```ruby
|
72
|
-
|
73
|
-
require 'net/https'
|
74
|
-
|
75
|
-
require 'uri'
|
76
|
-
|
77
|
-
require 'json'
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
API_KEY = 'Your API Key'
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
lat = '3.152917' #中心座標の緯度
|
86
|
-
|
87
|
-
lng = '101.7038288' #中心座標の経度
|
88
|
-
|
89
|
-
rad = '5000' #中心座標の半径(m)
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
#カテゴリー切り替え
|
96
|
-
|
97
|
-
# types = 'convenience_store'
|
98
|
-
|
99
|
-
types = 'grocery_or_supermarket'
|
100
|
-
|
101
|
-
# types = 'shopping_mall'
|
102
|
-
|
103
|
-
# types = 'train_station'
|
104
|
-
|
105
|
-
# types = 'light_rail_station'
|
106
|
-
|
107
|
-
# types = 'subway_station'
|
108
|
-
|
109
|
-
# types = 'university'
|
110
|
-
|
111
|
-
# types = 'bank'
|
112
|
-
|
113
|
-
# types = 'hospital'
|
114
|
-
|
115
|
-
# types = 'doctor'
|
116
|
-
|
117
|
-
# types = 'mosque'
|
118
|
-
|
119
|
-
# types = 'church'
|
120
|
-
|
121
|
-
# types = 'hindu_temple'
|
122
|
-
|
123
|
-
# types = 'police'
|
124
|
-
|
125
|
-
# types = 'gas_station'
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
File.open('./place.json','w'){|file| file = nil} #前に出力した内容を削除
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
uri = URI.parse "https://maps.googleapis.com/maps/api/place/radarsearch/json?location=#{lat},#{lng}&radius=#{rad}&types=#{types}&language=en&key=#{API_KEY}"
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
request = Net::HTTP::Get.new(uri.request_uri)
|
138
|
-
|
139
|
-
response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
|
140
|
-
|
141
|
-
http.request(request)
|
142
|
-
|
143
|
-
end
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
body = JSON.parse response.body
|
148
|
-
|
149
|
-
results = body['results']
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
results.each do |result|
|
154
|
-
|
155
|
-
place_id = result['place_id']
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
uri2 = URI.parse "https://maps.googleapis.com/maps/api/place/details/json?placeid=#{place_id}&language=en&key=#{API_KEY}"
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
request = Net::HTTP::Get.new(uri2.request_uri)
|
164
|
-
|
165
|
-
response = Net::HTTP.start(uri2.host, uri2.port, use_ssl: uri2.scheme == 'https') do |http|
|
166
|
-
|
167
|
-
http.request(request)
|
168
|
-
|
169
|
-
end
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
detail = JSON.parse response.body
|
174
|
-
|
175
|
-
place_detail = detail['result']
|
176
|
-
|
177
|
-
location = place_detail['geometry']['location']
|
178
|
-
|
179
|
-
address = place_detail['address_components']
|
180
|
-
|
181
|
-
check_items = ["street_number", "route", "locality", "administrative_area_level_2", "administrative_area_level_1", "country"]
|
182
|
-
|
183
|
-
place = {}
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
address.each do |element|
|
188
|
-
|
189
|
-
result = element["types"] & check_items
|
190
|
-
|
191
|
-
next if result.empty?
|
192
|
-
|
193
|
-
place[result[0]] = element["long_name"]
|
194
|
-
|
195
|
-
end
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
answer = {
|
200
|
-
|
201
|
-
:name => place_detail['name'],
|
202
|
-
|
203
|
-
:lat => location['lat'],
|
204
|
-
|
205
|
-
:lng => location['lng'],
|
206
|
-
|
207
|
-
:formatted_address => place_detail['formatted_address'],
|
208
|
-
|
209
|
-
:street_number => place['street_number'],
|
210
|
-
|
211
|
-
:route => place['route'],
|
212
|
-
|
213
|
-
:locality => place['locality'],
|
214
|
-
|
215
|
-
:administrative_area_level_2 => place['administrative_area_level_2'],
|
216
|
-
|
217
|
-
:administrative_area_level_1 => place['administrative_area_level_1'],
|
218
|
-
|
219
|
-
:country => place['country'],
|
220
|
-
|
221
|
-
:types => place_detail['types']
|
222
|
-
|
223
|
-
}
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
#不要なtypesを取り除く
|
228
|
-
|
229
|
-
list = ['establishment', 'point_of_interest']
|
230
|
-
|
231
|
-
place_detail['types'].delete_if do |str|
|
232
|
-
|
233
|
-
list.include?(str)
|
234
|
-
|
235
|
-
end
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
#不要なデータを除外
|
240
|
-
|
241
|
-
a = place_detail['name'].match(/\satm|atm\s/i)
|
242
|
-
|
243
|
-
b = (['grocery_or_supermarket', 'convenience_store'] - place_detail['types']).empty?
|
244
|
-
|
245
|
-
c = place_detail['name'].match(/7-11|7\sEleven|7eleven|7-Eleven/i)
|
246
|
-
|
247
|
-
d = place_detail['types'] == 'bank'
|
248
|
-
|
249
|
-
e = place_detail['types'] == 'shopping_mall'
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
if types == 'grocery_or_supermarket'
|
254
|
-
|
255
|
-
if b
|
256
|
-
|
257
|
-
answer.clear
|
258
|
-
|
259
|
-
end
|
260
|
-
|
261
|
-
end
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
if (place['country'] == "Malaysia")
|
266
|
-
|
267
|
-
unless ( a && !d ) || ( c && !e ) || answer == {}
|
268
|
-
|
269
|
-
File.open('./place.json', 'a') do |file|
|
270
|
-
|
271
|
-
file.puts JSON.pretty_generate(answer)
|
272
|
-
|
273
|
-
end
|
274
|
-
|
275
|
-
end
|
276
|
-
|
277
|
-
end
|
278
|
-
|
279
|
-
end
|
280
|
-
|
281
|
-
```
|
5
解決
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
※解決しました。
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
|
6
|
+
|
1
7
|
除外がうまくいかないので質問させてください。
|
2
8
|
|
3
9
|
|
4
ハッシュにしました
test
CHANGED
File without changes
|
test
CHANGED
@@ -192,27 +192,27 @@
|
|
192
192
|
|
193
193
|
answer = {
|
194
194
|
|
195
|
-
name
|
195
|
+
:name => place_detail['name'],
|
196
|
-
|
196
|
+
|
197
|
-
lat
|
197
|
+
:lat => location['lat'],
|
198
|
-
|
198
|
+
|
199
|
-
lng
|
199
|
+
:lng => location['lng'],
|
200
|
-
|
200
|
+
|
201
|
-
formatted_address
|
201
|
+
:formatted_address => place_detail['formatted_address'],
|
202
|
-
|
202
|
+
|
203
|
-
street_number
|
203
|
+
:street_number => place['street_number'],
|
204
|
-
|
204
|
+
|
205
|
-
route
|
205
|
+
:route => place['route'],
|
206
|
-
|
206
|
+
|
207
|
-
locality
|
207
|
+
:locality => place['locality'],
|
208
|
-
|
208
|
+
|
209
|
-
administrative_area_level_2
|
209
|
+
:administrative_area_level_2 => place['administrative_area_level_2'],
|
210
|
-
|
210
|
+
|
211
|
-
administrative_area_level_1
|
211
|
+
:administrative_area_level_1 => place['administrative_area_level_1'],
|
212
|
-
|
212
|
+
|
213
|
-
country
|
213
|
+
:country => place['country'],
|
214
|
-
|
214
|
+
|
215
|
-
types
|
215
|
+
:types => place_detail['types']
|
216
216
|
|
217
217
|
}
|
218
218
|
|
@@ -246,9 +246,9 @@
|
|
246
246
|
|
247
247
|
if types == 'grocery_or_supermarket'
|
248
248
|
|
249
|
-
|
249
|
+
if b
|
250
|
-
|
250
|
+
|
251
|
-
answer
|
251
|
+
answer.clear
|
252
252
|
|
253
253
|
end
|
254
254
|
|
3
訂正
test
CHANGED
File without changes
|
test
CHANGED
@@ -72,7 +72,7 @@
|
|
72
72
|
|
73
73
|
|
74
74
|
|
75
|
-
|
75
|
+
API_KEY = 'Your API Key'
|
76
76
|
|
77
77
|
|
78
78
|
|
2
わかりやすく修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,4 +1,8 @@
|
|
1
|
+
除外がうまくいかないので質問させてください。
|
2
|
+
|
3
|
+
|
4
|
+
|
1
|
-
今回、スクリプトで実現したいことは以下です。
|
5
|
+
まず今回、スクリプトで実現したいことは以下です。
|
2
6
|
|
3
7
|
|
4
8
|
|
@@ -6,11 +10,11 @@
|
|
6
10
|
|
7
11
|
- place_idを元にプレイス詳細検索してjsonで別ファイルに出力
|
8
12
|
|
9
|
-
- その際に不必要なデータの除外
|
13
|
+
- その際に以下の不必要なデータの除外
|
10
14
|
|
11
15
|
0. shopping_mallで7 Erevenの除外
|
12
16
|
|
13
|
-
0. grocery_or_supermarketでconvenience storeに該当するもの
|
17
|
+
0. grocery_or_supermarketでconvenience storeに該当するものは除外
|
14
18
|
|
15
19
|
0. (convenience storeでは除外させない)
|
16
20
|
|
@@ -20,17 +24,23 @@
|
|
20
24
|
|
21
25
|
|
22
26
|
|
27
|
+
https://developers.google.com/places/web-service/search?hl=ja
|
28
|
+
|
29
|
+
|
30
|
+
|
23
|
-
今のスクリプトでは、
|
31
|
+
しかし、今のスクリプトでは、
|
24
32
|
|
25
33
|
> 2. grocery_or_supermarketでconvenience storeに該当するものが除外
|
26
34
|
|
27
35
|
> 3. (convenience storeでは除外させない)
|
28
36
|
|
37
|
+
|
38
|
+
|
29
39
|
の部分がうまくいきません。
|
30
40
|
|
31
41
|
|
32
42
|
|
33
|
-
一旦grocery_or_supermarketでconvenience storeに該当するものは空にし、その後空のものは表示させないといったような内容にしてい
|
43
|
+
一旦grocery_or_supermarketでconvenience storeに該当するものは空にし、その後空のものは表示させないといったような内容にしているつもりなのですが、
|
34
44
|
|
35
45
|
|
36
46
|
|
@@ -40,13 +50,11 @@
|
|
40
50
|
|
41
51
|
```
|
42
52
|
|
43
|
-
|
44
|
-
|
45
53
|
の部分でor検索がうまくいかずanswerで空にしたはずのものが表示されてしまいます。
|
46
54
|
|
47
55
|
|
48
56
|
|
49
|
-
|
57
|
+
一旦空にする必要がなければ、grocery_or_supermarketで検索した際にconvenience storeに該当するものがあれば表示しないといった方法のほうがいいです。
|
50
58
|
|
51
59
|
|
52
60
|
|
1
見やすく変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,49 +1,45 @@
|
|
1
|
-
|
1
|
+
今回、スクリプトで実現したいことは以下です。
|
2
|
-
|
2
|
+
|
3
|
+
|
4
|
+
|
3
|
-
Google Place APIでレーダー検索からplace_idを取得
|
5
|
+
- Google Place APIでレーダー検索からplace_idを取得
|
4
|
-
|
6
|
+
|
5
|
-
place_idを元にプレイス詳細検索してjsonで別ファイルに出力
|
7
|
+
- place_idを元にプレイス詳細検索してjsonで別ファイルに出力
|
6
|
-
|
7
|
-
|
8
|
-
|
8
|
+
|
9
|
-
その際に不必要なデータの除外
|
9
|
+
- その際に不必要なデータの除外
|
10
|
-
|
10
|
+
|
11
|
-
shopping_mallで7 Erevenの除外
|
11
|
+
0. shopping_mallで7 Erevenの除外
|
12
|
-
|
12
|
+
|
13
|
-
grocery_or_supermarketでconvenience storeに該当するものが除外
|
13
|
+
0. grocery_or_supermarketでconvenience storeに該当するものが除外
|
14
|
-
|
14
|
+
|
15
|
-
(convenience storeでは除外させない)
|
15
|
+
0. (convenience storeでは除外させない)
|
16
|
-
|
16
|
+
|
17
|
-
bankでnameにatmが含まれれば除外
|
17
|
+
0. bankでnameにatmが含まれれば除外
|
18
|
-
|
18
|
+
|
19
|
-
countryがマレーシアのもの以外は除外
|
19
|
+
0. countryがマレーシアのもの以外は除外
|
20
|
-
|
21
|
-
|
22
|
-
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
-
|
23
|
+
今のスクリプトでは、
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
24
|
+
|
28
|
-
|
29
|
-
grocery_or_supermarketでconvenience storeに該当するものが除外
|
25
|
+
> 2. grocery_or_supermarketでconvenience storeに該当するものが除外
|
30
|
-
|
26
|
+
|
31
|
-
(convenience storeでは除外させない)
|
27
|
+
> 3. (convenience storeでは除外させない)
|
32
|
-
|
33
|
-
|
34
28
|
|
35
29
|
の部分がうまくいきません。
|
36
30
|
|
37
31
|
|
38
32
|
|
39
|
-
|
40
|
-
|
41
|
-
|
33
|
+
一旦grocery_or_supermarketでconvenience storeに該当するものは空にし、その後空のものは表示させないといったような内容にしていますが、
|
34
|
+
|
35
|
+
|
36
|
+
|
42
|
-
|
37
|
+
```ruby
|
43
|
-
|
44
38
|
|
45
39
|
unless ( a && !d ) || ( c && !e ) || answer == {}
|
46
40
|
|
41
|
+
```
|
42
|
+
|
47
43
|
|
48
44
|
|
49
45
|
の部分でor検索がうまくいかずanswerで空にしたはずのものが表示されてしまいます。
|
@@ -72,8 +68,6 @@
|
|
72
68
|
|
73
69
|
|
74
70
|
|
75
|
-
|
76
|
-
|
77
71
|
lat = '3.152917' #中心座標の緯度
|
78
72
|
|
79
73
|
lng = '101.7038288' #中心座標の経度
|