Search code examples
ruby-on-rails-3gmaps4rails

gmaps4rails detect_location is not showing marker


I am working with gem "gmaps4rails" in rails 3.2.12, I had set the detect_location = "true" in map_options, but it is not showing any marker with user current location.

Here is code:

<%= gmaps("map_options" => {"auto_adjust" => true, "detect_location" => true, "center_on_user" => true, "auto_zoom" => false, "radius" => 2500},
  "markers" => {"data" => @json, "options" => {"list_container" => "markers_list","randomize" => true,"max_random_distance" => 10000, "draggable" => true, "dblclick" => "latLng"} }

How to show with marker for the user current location.

Please help me. Thanks in Advance


Solution

  • With the gem "gmaps4rails",

    if we give the option "detect_location" => true, when the map loads on page, it shows pop-up to user as 'allow application to use your location' and gives yes and no as options.

    If user selects 'yes', then map get centered to user's location.

    Now here you want to display marker for user's location, then you have to add marker by your own.

    This can be done as follows:

    Controller:

    geo = request.location
    @latitude  = geo.latitude
    @longitude = geo.longitude
    

    With your page load, you can add marker for user, with this callback.

    This would add marker for current user location when map loads on page.

    - content_for :scripts do
      %script{:type => "text/javascript"}
        Gmaps.map.callback = function(){
        google.maps.event.addListenerOnce(Gmaps.map.serviceObject, 'idle', function(){
        / add user pin
        var home_pin = [{picture: "current_user.png", width: 32, lat: "#{@latitude}", lng: "#{@longitude}"}];
        Gmaps.map.addMarkers(home_pin)
        }
        )};
    

    Hope this would help you.

    Let me know if you still face any issues.

    Thanks.