Search code examples
ruby-on-railsrubygoogle-mapsruby-on-rails-3.2gmaps4rails

rails 3 + gmaps4rails: display current location of the customer in the map?


In my rails application, users have multiple customers. If user is logged in, in the home page I am displaying google map with the location of his customers. It is working fine using gmaps4rails gem.

But, for the first login, user will have no customers, and the map is displaying ocean view. Unlike this, If there are no customer created, I want to display current user location, from where he is accessing my rails application.

How to fix this problem?


Solution

  • Pretty simple

    just i have to write

    <%# 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 %>
    

    And add javascript call-back as:

    <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>