※解決しました!
除外がうまくいかないので質問させてください。
まず今回、スクリプトで実現したいことは以下です。
- Google Place APIでレーダー検索からplace_idを取得
- place_idを元にプレイス詳細検索してjsonで別ファイルに出力
- その際に以下の不必要なデータの除外
- shopping_mallで7 Erevenの除外
- grocery_or_supermarketでconvenience storeに該当するものは除外
- (convenience storeでは除外させない)
- bankでnameにatmが含まれれば除外
- countryがマレーシアのもの以外は除外
https://developers.google.com/places/web-service/search?hl=ja
しかし、今のスクリプトでは、
- grocery_or_supermarketでconvenience storeに該当するものが除外
- (convenience storeでは除外させない)
の部分がうまくいかず、convenience storeの際もcの条件を除外してしまいます。
解決方法を教えていただきたいです。
よろしくお願いします。
ruby
1require 'net/https' 2require 'uri' 3require 'json' 4 5API_KEY = 'Your API Key' 6 7lat = '3.152917' #中心座標の緯度 8lng = '101.7038288' #中心座標の経度 9rad = '5000' #中心座標の半径(m) 10 11#カテゴリー切り替え 12types = 'convenience_store' 13# types = 'grocery_or_supermarket' 14# types = 'shopping_mall' 15# types = 'train_station' 16# types = 'light_rail_station' 17# types = 'subway_station' 18# types = 'university' 19# types = 'bank' 20# types = 'hospital' 21# types = 'doctor' 22# types = 'mosque' 23# types = 'church' 24# types = 'hindu_temple' 25# types = 'police' 26# types = 'gas_station' 27 28File.open('./place.json','w'){|file| file = nil} #前に出力した内容を削除 29 30uri = URI.parse "https://maps.googleapis.com/maps/api/place/radarsearch/json?location=#{lat},#{lng}&radius=#{rad}&types=#{types}&language=en&key=#{API_KEY}" 31 32request = Net::HTTP::Get.new(uri.request_uri) 33response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http| 34 http.request(request) 35end 36 37body = JSON.parse response.body 38results = body['results'] 39 40results.each do |result| 41 place_id = result['place_id'] 42 43 uri2 = URI.parse "https://maps.googleapis.com/maps/api/place/details/json?placeid=#{place_id}&language=en&key=#{API_KEY}" 44 45 request = Net::HTTP::Get.new(uri2.request_uri) 46 response = Net::HTTP.start(uri2.host, uri2.port, use_ssl: uri2.scheme == 'https') do |http| 47 http.request(request) 48 end 49 50 detail = JSON.parse response.body 51 place_detail = detail['result'] 52 location = place_detail['geometry']['location'] 53 address = place_detail['address_components'] 54 check_items = ["street_number", "route", "locality", "administrative_area_level_2", "administrative_area_level_1", "country"] 55 place = {} 56 57 address.each do |element| 58 result = element["types"] & check_items 59 next if result.empty? 60 place[result[0]] = element["long_name"] 61 end 62 63 answer = { 64 :name => place_detail['name'], 65 :lat => location['lat'], 66 :lng => location['lng'], 67 :formatted_address => place_detail['formatted_address'], 68 :street_number => place['street_number'], 69 :route => place['route'], 70 :locality => place['locality'], 71 :administrative_area_level_2 => place['administrative_area_level_2'], 72 :administrative_area_level_1 => place['administrative_area_level_1'], 73 :country => place['country'], 74 :types => place_detail['types'] 75 } 76 77 #不要なtypesを取り除く 78 list = ['establishment', 'point_of_interest'] 79 place_detail['types'].delete_if do |str| 80 list.include?(str) 81 end 82 83 #不要なデータを除外 84 a = place_detail['name'].match(/\satm|atm\s/i) 85 b = (['grocery_or_supermarket', 'convenience_store'] - place_detail['types']).empty? 86 c = place_detail['name'].match(/7-11|7\sEleven|7eleven|7-Eleven/i) 87 d = place_detail['types'] == 'bank' 88 e = place_detail['types'] == 'shopping_mall' 89 90 if types == 'grocery_or_supermarket' 91 if ( b || c ) 92 answer.clear 93 end 94 end 95 96 if (place['country'] == "Malaysia") 97 unless ( a && !d ) || ( c && !e ) || answer == {} 98 File.open('./place.json', 'a') do |file| 99 file.puts JSON.pretty_generate(answer) 100 end 101 end 102 end 103end
あなたの回答
tips
プレビュー