前提・実現したいこと
以下のようにGooglemap directions APIを使って、2地点間の距離や所要時間をresponseに返すコードを実装しています。こちらのコードが、origin=渋谷 destination=恵比寿など、ベタ書きで記載すると必要なJSONの結果が返ってくることは確認できています。
記載コード
uri = URI.escape("https://maps.googleapis.com/maps/api/directions/json?origin=渋谷&destination=恵比寿&mode=walking&key=API KEY”) res = HTTP.get(uri).to_s response = JSON.parse(res)
返答結果
[1] pry(main)> response => {"geocoded_waypoints"=> [{"geocoder_status"=>"OK", "place_id"=>"ChIJ0Qgx67KMGGARd2ZbObLZHPE", "types"=>["locality", "political"]}, {"geocoder_status"=>"OK", "place_id"=>"ChIJWRK3eBSLGGARCdO8OgKkE5w", "types"=>["political", "sublocality", "sublocality_level_2"]}], "routes"=> [{"bounds"=>{"northeast"=>{"lat"=>35.6622678, "lng"=>139.7153187}, "southwest"=>{"lat"=>35.6458126, "lng"=>139.703939}}, "copyrights"=>"Map data ©2019 Google", "legs"=> [{"distance"=>{"text"=>"2.6 km", "value"=>2568}, "duration"=>{"text"=>"33 mins", "value"=>1967}, "end_address"=>"Ebisu, Shibuya City, Tokyo 150-0013, Japan", "end_location"=>{"lat"=>35.6458126, "lng"=>139.7153187}, "start_address"=>"Shibuya City, Tokyo, Japan", "start_location"=>{"lat"=>35.6622678, "lng"=>139.703939}, "steps"=> [{"distance"=>{"text"=>"0.3 km", "value"=>312}, "duration"=>{"text"=>"4 mins", "value"=>261}, "end_location"=>{"lat"=>35.6608076, "lng"=>139.7067344}, "html_instructions"=>"Head <b>east</b>", "polyline"=>{"points"=>"ehtxEs|tsYVgA@ECAh@gCHYBM@EDKBGNWT_@Xg@@EBE|@{A@EVWj@m@"}, "start_location"=>{"lat"=>35.6622678, "lng"=>139.703939}, "travel_mode"=>"WALKING"}, {"distance"=>{"text"=>"6 m", "value"=>6}, "duration"=>{"text"=>"1 min", "value"=>4}, "end_location"=>{"lat"=>35.6607444, "lng"=>139.7068565}, "html_instructions"=>"Turn <b>left</b> onto <b>青山通り</b>/<b>国道246号</b>", "maneuver"=>"turn-left", "polyline"=>{"points"=>"a_txEanusYCEPS"}, "start_location"=>{"lat"=>35.6608076, "lng"=>139.7067344}, "travel_mode"=>"WALKING"}, {"distance"=>{"text"=>"23 m", "value"=>23}, "duration"=>{"text"=>"1 min", "value"=>40}, "end_location"=>{"lat"=>35.660603, "lng"=>139.7069058}, "html_instructions"=>"Cross the road", "polyline"=>{"points"=>"s~sxE{nusY@CPQFJ"}, "start_location"=>{"lat"=>35.6607444, "lng"=>139.7068565}, "travel_mode"=>"WALKING"}, {"distance"=>{"text"=>"0.3 km", "value"=>267}, "duration"=>{"text"=>"3 mins", "value"=>196}, "end_location"=>{"lat"=>35.6587413, "lng"=>139.7085902}, "html_instructions"=>"Turn <b>left</b>", "maneuver"=>"turn-left", "polyline"=>{"points"=>"w}sxEeousYFEXU@Cb@]BC`A{@FGv@s@BD~@y@DGXUb@a@FEHKHI@E"}, "start_location"=>{"lat"=>35.660603, "lng"=>139.7069058}, "travel_mode"=>"WALKING"}, {"distance"=>{"text"=>"0.4 km", "value"=>426}, "duration"=>{"text"=>"5 mins", "value"=>300}, "end_location"=>{"lat"=>35.6553456, "lng"=>139.7065912}, "html_instructions"=>"Turn <b>right</b>", "maneuver"=>"turn-right", "polyline"=>{"points"=>"crsxEuyusYB@HDZNNDFBDBXLPNB@NHJFB@HDDBNBDFfAh@DBLFHDFBf@VNDRJDD\PLDvAr@D@RJh@XFBPJTLPH\R"}, "start_location"=>{"lat"=>35.6587413, "lng"=>139.7085902}, "travel_mode"=>"WALKING"}, {"distance"=>{"text"=>"11 m", "value"=>11}, "duration"=>{"text"=>"1 min", "value"=>8}, "end_location"=>{"lat"=>35.6553467, "lng"=>139.7064737}, "html_instructions"=>"Turn <b>right</b> onto <b>明治通り</b>/<b>都道305号</b>", "maneuver"=>"turn-right", "polyline"=>{"points"=>"}|rxEemusYGHFL"}, "start_location"=>{"lat"=>35.6553456, "lng"=>139.7065912}, "travel_mode"=>"WALKING"}, {"distance"=>{"text"=>"0.2 km", "value"=>192}, "duration"=>{"text"=>"3 mins", "value"=>167}, "end_location"=>{"lat"=>35.6538622, "lng"=>139.7055776}, "html_instructions"=>"Turn <b>left</b> at <b>並木橋(交差点)</b>", "maneuver"=>"turn-left", "polyline"=>{"points"=>"}|rxEmlusYLF?ENJFDDBLFHBB@LBFBF@JDLFFBRJDBLHz@h@j@\PL"}, "start_location"=>{"lat"=>35.6553467, "lng"=>139.7064737}, "travel_mode"=>"WALKING"}, {"distance"=>{"text"=>"0.7 km", "value"=>748}, "duration"=>{"text"=>"9 mins", "value"=>541}, "end_location"=>{"lat"=>35.6482206, "lng"=>139.710175}, "html_instructions"=>"Turn <b>left</b>", "maneuver"=>"turn-left", "polyline"=> {"points"=> "ssrxE{fusY^_@t@m@FLBCl@i@HERQLMJIDEDGBCDGBCBC@AFCBADEFETMBAFABAFCDEFGJGHGd@c@HGLKLKVYZ[DC@AFEPMDEt@i@XQHEPKd@WZO\QvAm@hAi@BADCDCFKTOXQJG@?DEBCFGHIRSBCDE`@_@DGDGDEDIHMLS@A@CBA?AEMX_@"}, "start_location"=>{"lat"=>35.6538622, "lng"=>139.7055776}, "travel_mode"=>"WALKING"}, {"distance"=>{"text"=>"0.6 km", "value"=>583}, "duration"=>{"text"=>"8 mins", "value"=>450}, "end_location"=>{"lat"=>35.6458126, "lng"=>139.7153187}, "html_instructions"=>"Continue straight onto <b>都道305号</b><div style=\"font-size:0.9em\">Destination will be on the left</div>", "maneuver"=>"straight", "polyline"=>{"points"=>"kpqxEscvsYFGt@w@j@q@DIJIJAHSzAaBDGPSFG`@c@HMp@u@FGDEFEFE?K?IDW?OJoALwA@UReCJoADMCCR{B"}, "start_location"=>{"lat"=>35.6482206, "lng"=>139.710175}, "travel_mode"=>"WALKING"}], "traffic_speed_entry"=>[], "via_waypoint"=>[]}], "overview_polyline"=> {"points"=> "ehtxEs|tsYXmACAh@gCLg@FQbAgBdAmBbAeACEPSRUFJFEZYf@a@hAcAv@s@BDdAaA|@w@PQJOx@\LFj@\b@TNHNBDFlAl@VLrAl@b@VdBx@jAj@vAv@GHFLLF?EVPRJb@Lh@RbBbA|@j@tAmAFLBCv@o@r@o@RWx@g@XMbA{@hBaBfBoA|Ay@bEkBJGFKTOd@YFEh@i@p@q@Ze@T[EO`@g@rB}BJAHS`BiBdAmAfAiAFE?KDa@J_BNmB^uEDMCCR{B"}, "summary"=>"都道305号", "warnings"=>["Walking directions are in beta. Use caution – This route may be missing sidewalks or pedestrian paths."], "waypoint_order"=>[]}],
発生している問題・エラーメッセージ
緯度経度を保持しているaddressesテーブルとstationsテーブルを用意しているので、
上記のコードにaddress.latitude、address.longitude、station.latitude、station.longitude
を代入しようとすると、なぜか結果無しと返ってきてしまい、うまくいかなくなります。
何か変数の入れ方に問題があるかと仮説立てておりますが、調べても自分で原因が発見できずにおります。
変数代入後のコード
address = Address.find(1) station = Station.find(address.station_id) uri = URI.escape("https://maps.googleapis.com/maps/api/directions/json?origin=#{address.latitude},#{address.longitude}&destination=#{station.latitude},#{station.longitude}&mode=walking&key=API KEY”) res = HTTP.get(uri).to_s response = JSON.parse(res)
エラーメッセージ
[1] pry(main)> response => {"geocoded_waypoints"=>[{}, {"geocoder_status"=>"ZERO_RESULTS"}], "routes"=>[], "status"=>"NOT_FOUND"}
もし原因の想定がつく方がいらっしゃれば、ぜひアドバイス願えないでしょうか?
何卒、よろしくお願い申し上げます。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/28 01:13