Ruby on rails gmap4rails仅在单击标记时加载和添加信息框

Ruby on rails gmap4rails仅在单击标记时加载和添加信息框,ruby-on-rails,google-maps,gmaps4rails,gmaps4rails2,Ruby On Rails,Google Maps,Gmaps4rails,Gmaps4rails2,我将gmap4rails v2与rails 4.1一起使用,而不是一次加载所有infobox,这是非常低效的,我只想在单击某个标记后加载该标记的信息框的内容。我想出了以下方法来选择标记 markers = handler.addMarkers(<%= raw(@hash.to_json) %>); _.each(markers, function(marker){ google.maps.event.addListener(handler.getM

我将
gmap4rails v2
rails 4.1
一起使用,而不是一次加载所有
infobox
,这是非常低效的,我只想在单击某个
标记后加载该标记的信息框的内容。我想出了以下方法来选择标记

markers = handler.addMarkers(<%= raw(@hash.to_json) %>);
        _.each(markers, function(marker){
          google.maps.event.addListener(handler.getMap(), 'click', function(){
            marker.infowindow();
          });
        }); 
加载标记信息的控制器:

return Gmaps4rails.build_markers(profiles) do |profile, marker|
  marker.lat profile.latitude
  marker.lng profile.longitude
  marker.json({ :id => profile.profile_code })
  # this is how I loaded all markers before: marker.infowindow render_to_string(partial: "info_window", locals: { profile: profile })
  marker.picture({
    url: view_context.image_path( "marker-#{ (profile == current_user.profile) ? 'green' : 'blue' }.png"),
    width:  32,
    height: 32
  })
end
是否有任何方法可以像在原始标记加载中那样向
infowindow()
函数发送一个部分,如下所示?如果是这样的话,我如何将
user.id
发送到partial(在我的例子中是
profile.profile\u code
,我将每个
标记.id
设置为与我认为的相同

更新:

好的,我现在意识到我需要向服务器发出请求,javascript不能这样做,所以我将尝试使用

markers = handler.addMarkers(<%= raw(@hash.to_json) %>);
_.each(markers, function(marker){
  google.maps.event.addListener(handler.getMap(), 'click', function(){
    var infowindow = marker.infowindow;
    $.post('<%= load_info_box_url %>', {
    }, function(data){
      infowindow.html(data).name %>)
      infowindow.open(Gmaps.map.map, marker);
    });
  });
});
markers=handler.addMarkers();
_.每个(标记、功能(标记){
google.maps.event.addListener(handler.getMap(),'click',function()){
var infowindow=marker.infowindow;
$.post(“”{
},函数(数据){
infowindow.html(数据).name%>)
打开(Gmaps.map.map,marker);
});
});
});
然后进行加载信息框路由,然后在控制器中请求必要的数据,将html发送回数据

基本上:

  • 我创建了一个自定义生成器

  • 利用js模板


因此,您的json必须包含必要的键,但infowindow html仅按需构建

@aReading有一个很好的答案,但我发现在javascript模板中实现链接、条件和样式有点困难,因此我最终使用了ajax方法

javascript:

markers = handler.addMarkers(data.markers);
        _.each(markers, function(json, index){
          json.marker = markers[index]; 
          google.maps.event.addListener(json.marker.getServiceObject(), 'click', function(){
            var infowindow = json.marker.infowindow;

            $.post('<%= load_infobox_url %>', {
              marker_id: data.markers[index].id
            }, function(data){
              infowindow.setContent(data.html);
              infowindow.open(map, google.maps.markers[index]);
            });

          });
        });
控制器操作:

def load_infobox
    profile = Profile.find_by(profile_code: params[:marker_id])
    render json: { html: render_to_string(partial: "info_window", locals: { profile: profile }) }
  end

您描述的内容默认在gemoh wait中完成,好吧,我理解,您希望避免json中重复部分?@apreading默认情况下,它为每个标记的每个信息框加载每个部分,这需要很长时间,而且效率很低。我个人使用js模板,这适合你吗?我认为这里的问题更多的是拥有大量的标记,这使得ajax成为一条发展之路(特别是因为js不会做^^)。顺便说一句,你好,本:)是的,谢谢。我只是想知道我是否可以在其中使用rails分部,或者如果不能,我如何在模板中重新创建分部。但是,是的,它是有效的,我会检查它的答案,一旦它完成。感谢您和Olivier。实际上,一些gem允许您在JST变量中放置一些模板。但是有必要吗?好的,谢谢,是的,我必须在信息框里放一张图片,根据特定的条件有不同的格式。但我现在正试着用手把它放进去。再次感谢你所做的一切。你知道让你这么做的宝石的名字吗?以防万一你想再次成为英雄。。。
post '/load_infobox', to: 'requests#load_infobox', as: 'load_infobox'
def load_infobox
    profile = Profile.find_by(profile_code: params[:marker_id])
    render json: { html: render_to_string(partial: "info_window", locals: { profile: profile }) }
  end