0

在我的Rails應用程序,用戶擁有多個客戶。如果用戶已登錄,在主頁上我顯示谷歌地圖與他的客戶的位置。它使用gmaps4rails gem正常工作。軌3 + gmaps4rails:在地圖中顯示客戶的當前位置?

但是,對於第一次登錄,用戶就沒有客戶,以及地圖顯示的海景。與此不同,如果沒有創建的客戶,我想顯示當前用戶的位置,從那裏他正在訪問我的Rails應用程序。

如何解決這個問題?

回答

1

很簡單

只是我必須寫

<%# 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> 
相關問題