ruby-on-railsgoogle-mapsruby-on-rails-4gmaps4rails

How to show current location on Google-Maps-for-Rails


I have the gem 'gmaps4rails' installed and currently the setup is to show the location:

<div style='width: 800px;'>
  <div id="map" style='width: 800px; height: 400px;'></div>
</div>

<script>
    handler = Gmaps.build('Google');
    handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
        markers = handler.addMarkers(<%=raw @hash.to_json %>);
        handler.bounds.extendWith(markers);
        handler.fitMapToBounds();
        handler.getMap().setZoom(16);
    });
</script>

I would like to show the current location on the map as well. Any ideas on how to implement that?


Solution

  • Append current user location hash into your array of location hash and Rest will be handled by Gmap4Rails

    def show
        @las = La.find(params[:id])
        @hash = Gmaps4rails.build_markers(@las) do |la, marker|
          marker.lat la.latitude
          marker.lng la.longitude
        end
        append_cur_location 
    end 
    
    def append_cur_location
      @hash << { :lat=>action[0], :lng=>action[1]}
    end
    
    def action
       @lat_lng = cookies[:lat_lng].split("|")
    end
    

    Extras: If you want separate image for current user location.use below

    { :lat=>12.9698, :lng=>77.7499, :picture=>{:url=>user_image_path, :width=>32, :height=>32} }