在我的 rails 应用程序中,用户有多个客户。如果用户已登录,我会在主页上显示带有他客户位置的谷歌地图。使用 gmaps4rails gem 可以正常工作。
但是,对于第一次登录,用户将没有客户,并且地图显示海景。与此不同,如果没有创建客户,我想显示当前用户位置,他从那里访问我的 rails 应用程序。
如何解决这个问题?
在我的 rails 应用程序中,用户有多个客户。如果用户已登录,我会在主页上显示带有他客户位置的谷歌地图。使用 gmaps4rails gem 可以正常工作。
但是,对于第一次登录,用户将没有客户,并且地图显示海景。与此不同,如果没有创建客户,我想显示当前用户位置,他从那里访问我的 rails 应用程序。
如何解决这个问题?
很简单
只是我必须写
<%# view %>
<% if current_user.customers.present? %>
<%= gmaps4rails(@json) %>
<% else %>
<%= gmaps(:map_options => {:detect_location => true, :center_on_user => true, :auto_zoom => false, :zoom => 12, :auto_adjust => true}, :markers => {:data => @json} ) %>
<% end %>
并将javascript回调添加为:
<script type="text/javascript" charset="utf-8">
// display map of current location if customers is not present
$(document).ready(function(){
Gmaps.map.callback = function() {
Gmaps.map.createMarker({Lat: Gmaps.map.userLocation.lat(),
Lng: Gmaps.map.userLocation.lng(),
rich_marker: null,
marker_picture: ""
});
}
});
</script>