【问题标题】:gmaps4rails: How to implement a clickable link on a markergmaps4rails:如何在标记上实现可点击链接
【发布时间】:2013-07-31 03:36:48
【问题描述】:

使用 Rails 3.2.13、ruby 1.9.3p374 和 Gmaps4rails 1.5.6。

我一直在努力解决一些问题,起初我认为应该相当直截了当:我想显示一个标记,当单击该标记时,它将重定向到特定路径,并在同一窗口中加载该路径。

Gmaps4rails 在他们的description 中没有简单的选择。所以我花了无数个小时在网上搜索,我在 StackOverflow 中找到了两个最好的点击:this 闻起来是正确的路径,但this one 似乎也很有用。

最后,我的控制器看起来像:

@gmapsjson = current_user.houses.all.to_gmaps4rails do | house, marker |
  marker.title house.reference
  marker.json({:link => polymorphic_url(house, :routing_type => :path)})
end

这是正确的(我认为)在处理后的 html 中生成以下内容:

<script src="//maps.google.com/maps/api/js?v=3.8&amp;sensor=false&amp;client=&amp;key=&amp;libraries=geometry&amp;language=&amp;hl=&amp;region=" type="text/javascript"></script>

<script type="text/javascript">
  Gmaps.map = new Gmaps4RailsGoogle();
  Gmaps.load_map = function() {
    Gmaps.map.map_options.auto_zoom = false;
    Gmaps.map.map_options.zoom = 3;
    Gmaps.map.initialize();
    Gmaps.map.markers = [{"title":"First House","link":"/houses/1","lat":-3.4671425,"lng":12.5264373},{"title":"Second House","link":"/houses/2","lat":-4.5543296,"lng":-3.4151647}];
    Gmaps.map.create_markers();
    Gmaps.map.adjustMapToBounds();
    Gmaps.map.callback();
 };
 Gmaps.oldOnload = window.onload;
  window.onload = function() { Gmaps.triggerOldOnload(); Gmaps.loadMaps(); };
</script>

标记出现在正确的位置。但是现在,解释first relevant reference 上的描述,我直接在视图中输入,确保它会出现在生成的 html 中所有其他 javascripts Gmaps4Rails 转储的下方:

<script type="text/javascript">
  function redirect_to(url) {
    window.location = url
  };
  Gmaps.callback = function() {
    for (var i = 0; i < Gmaps.map.markers.length; ++i) {
      google.maps.event.addListener(Gmaps.map.markers[i].google_object, 'click', redirect_to(Gmaps.map.markers[i].link));
  }
};
</script>

但是现在,每当我加载页面时,我的 Firefox javascript 控制台都会显示:

TypeError: Gmaps[load_function_name] is not a function

for (key in Gmaps) {
  value = Gmaps[key];
  searchLoadIncluded = key.search(/load/);
  if (searchLoadIncluded === -1) {
    load_function_name = "load_" + key;
    _results.push(Gmaps[load_function_name]());         <=== Points to this line
  } else {
    _results.push(void 0);
  }
} 

那么,有什么想法是错误的吗?

提前致谢,

马可

【问题讨论】:

  • 你试过这个答案吗? stackoverflow.com/questions/8608602/…
  • 我做到了,是的。它在原始问题中被称为“但这似乎也有用”。但感谢您提醒我注意这一点。 :) 这种方法在 javascript 控制台中给了我一个未知的“google”。

标签: ruby-on-rails ruby ruby-on-rails-3 google-maps gmaps4rails


【解决方案1】:

您可以只生成所有的 html,包括链接,而不是在 JavaScript 中执行此操作,而是您的控制器。 注意:为了正确生成链接,我单独设置并将其分配给变量

这是我的 locations_controller.rb 包含的内容:

  @json = Location.all.to_gmaps4rails do |location, marker|
    location_link = view_context.link_to location.name, location_path(location)
    marker.title location.name
    marker.infowindow "<h4><u>#{location_link}</u></h4> 
                       <i>#{location.address}</i>"
end

当你正确设置它时,你会生成看起来像这样的哈希

{"title":"Home",
 "description":"<h4><u><a href=\"/locations/1\">Place</a></u></h4> 
 <i>123 E Main St. Anytown, USA</i>",
 "lat":29.585019,
 "lng":-81.319479}

维基页面here 帮助了我。希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-04
    • 1970-01-01
    • 2013-08-24
    • 2017-04-06
    • 1970-01-01
    • 2012-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多