在Rails搜索字段中搜索city或zip并在新标签中打开Goog​​le Maps?

时间:2014-02-24 22:39:26

标签: ruby-on-rails google-maps

我有一个搜索字段“查找您附近的公园”。我们的想法是,当有人进入邮政编码或城市时,会打开一个新标签,用户将被定向到Google地图。在谷歌地图中,​​你可以搜索“35209附近的公园”等,它会显示一张地图,附近有这个邮编或城市的公园。

我有办法接受用户输入,例如邮政编码或城市,并在新标签页中打开Goog​​le地图并搜索“Parks near ...”吗?

它不一定非常干净和精确。我真的只需要一个新标签打开谷歌地图并搜索该区域。

我的搜索字段如下:

%input{ type: "text", placeholder: 'Search by zip code or city', class: 'park-search' }

2 个答案:

答案 0 :(得分:0)

我认为你应该这样做:

gem'geocoder'

在你的模特中

  attr_accessible :city_name, :latitude, :longitude
  geocoded_by :city_name
  after_validation :geocode

  private
  def city_name
    self.city.name
  end
在您的视图中

<%= image_tag "http://maps.google.com/maps/api/staticmap?size=450x300&sensor=false&zoom=16&markers=#{@location.latitude}%2C#{@location.longitude}" %>

<h3>Nearby locations</h3>
<ul>
<% for location in @location.nearbys(10) %>
  <li><%= link_to location.address, location %> (<%= location.distance.round(2) %> miles)</li>
<% end %>
</ul>

http://www.rubygeocoder.com/

http://railscasts.com/episodes/273-geocoder

答案 1 :(得分:0)

用户输入和谷歌地图网址的简单字符串连接最终成为最佳解决方案。

var mapRedirect = function() {
    $('.map-button').on('click', function() {
        var input = document.getElementById('park-search').value;
        var googleLink = "https://www.google.com/maps/search/parks+near+" + input;
        window.open(googleLink);
        return false;
    });
}