查找给定纬度最近的咖啡馆和机场 rails

Find nearest cafe and airports of a given latitude rails

我有 latitudelongitude 地址,我想搜索所有机场、火车站、巴士站和咖啡馆等热门地点。我在我的应用程序中使用这些 gems :

gem 'geocoder'
gem 'gmaps4rails'
gem 'geokit-rails

现在请建议我如何解决这个问题?

假设您使用 ActiveRecord,我实际上会使用 near function of geocoder

我们可以在 type 中传递搜索参数(这些搜索字符串由 google 为地点建议)

    lat="30.987674"
    lng="-45.098753"   
    doc = JSON.parse(open("https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=#{lat},#{lng}&radius=50&type=['cafe']&name=cruise&key=xxxxxx").read)
    cords=[]
    doc['results'].each do |r|
      tmp={}
      tmp = r['geometry']['location']
      tmp["name"] = r['name']
      cords<< r['geometry']['location']
    end

这里的 cords 是 hashesarray,其中包含关于咖啡馆等地方的相关信息。

参见:https://developers.google.com/places/web-service/search

https://developers.google.com/places/supported_types