我的代码:
位置模型:
class Location < ActiveRecord::Base
attr_accessible :latitude, :longitude, :timestamp, :user_id, :user
validates :latitude, :longitude, :timestamp, :user, :presence => true
belongs_to :user
def gmaps4rails_sidebar
"<span>#{user}</span>"
end
end
控制器:
@locations = []
User.all.each do |user|
if user.role?('manager')
last_location = user.locations.last
if last_location
location = {}
location[:lat] = last_location.latitude
location[:lng] = last_location.longitude
location[:title] = last_location.timestamp
location[:infowindow] = "<b>#{t(:datetime)}:</b> #{last_location.created_at} <br /><b>#{t(:latitude)}:</b> #{last_location.latitude}<br /><b>#{t(:longitude)}:</b> #{last_location.longitude}<br /><b>#{t(:user)}:</b> #{user.username}"
@locations << location
end
end
end
查看:
<%= javascript_include_tag 'locations' %>
<div class="map_container" style='width: 980px; height: 458px;'>
<div id="map" style='width: 980px; height: 458px;'></div>
</div>
<ul id="markers_list"> </ul>
<script type="text/javascript">
jQuery(document).ready(function() {
handler = Gmaps.build('Google', { markers: { maxRandomDistance: null } });
handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
markers = handler.addMarkers(<%=raw @locations.to_json %>);
handler.bounds.extendWith(markers);
handler.fitMapToBounds();
handler.getMap().setZoom(12);
});
});
</script>
我想显示标记列表。我读了它是如何在旧版gem中完成的。但是不知道如何在新版本中执行此操作。在新版本中提示为gem,您可以将标记列表到单独的列表中吗?
答案 0 :(得分:0)
我找到示例http://apneadiving.github.io/
<script type="text/javascript">
jQuery(document).ready(function() {
var locations_json = <%=raw @locations.to_json %>
handler = Gmaps.build('Google', { markers: { maxRandomDistance: null } });
handler.buildMap({ provider: {}, internal: {id: 'map'}, list_container: "markers_list"}, function(){
markers = handler.addMarkers(locations_json);
handler.bounds.extendWith(markers);
handler.fitMapToBounds();
handler.getMap().setZoom(12);
_.each(locations_json, function(json, index){
json.marker = markers[index];
});
createSidebar(locations_json);
});
function createSidebarLi(json){
return ("<li><a>" + json.name + "</a></li>");
};
function bindLiToMarker($li, marker){
$li.on('click', function(){
handler.getMap().setZoom(14);
marker.setMap(handler.getMap());
marker.panTo();
google.maps.event.trigger(marker.getServiceObject(), 'click');
})
};
function createSidebar(json_array){
_.each(locations_json, function(json){
var $li = $( createSidebarLi(json) );
$li.appendTo('#markers_list');
bindLiToMarker($li, json.marker);
});
};
});
</script>