Search code examples
ruby-on-railsruby-on-rails-3ruby-on-rails-3.1geolocationgmaps4rails

Issue with gmaps4rails for user geolocation via browser


I am using Gmaps4rails. It's working really fine. But now I got an issue in trying geolocate my user via the browser.

My view:

   <%= gmaps(:markers => { :data => @json},
        :map_options => { :container_class => "map_container",:id => "map",:class => "gmaps4rails_map"},
        :scripts     => :none ) %>

        <% content_for :scripts do %>
        <script type="text/javascript">
         Gmaps.map.callback = function() {
          setInterval(function(){Gmaps.map.createMarker({
                        Lat: Gmaps.map.userLocation.lat(),
                        Lng: Gmaps.map.userLocation.lng(), 
                        rich_marker: null, 
                        marker_picture: "/images/icon.png",
                       })},10000)
            }


        </script>
        <% end %>

I tried a setinterval method for the browser geolocation delay as discussed in How do I display the user's location with a marker in gmaps4rails?

my gmaps coffee script

  detect_location: true  # should the browser attempt to use geolocation detection features of HTML5?
  center_on_user: true   # centers map on the location detected through the browser

I got this error in my google chrome console.

Uncaught TypeError: Cannot read property '0' of undefined Gmaps4Rails.Gmaps4Rails.createImageAnchorPositiongmaps4rails.base.js:377 Gmaps4RailsGoogle.Gmaps4RailsGoogle.createMarkergmaps4rails.googlemaps.js:127 (anonymous function)

What am I missing ?? Thanks

Stephane


Solution

  • You should do:

     setInterval( function(){
     Gmaps.map.createMarker({
       Lat: Gmaps.map.userLocation.lat(),
       Lng: Gmaps.map.userLocation.lng(), 
       rich_marker: null, 
       marker_picture: "https://a248.e.akamai.net/assets.github.com/images/modules/about_page/octocat.png?1315937507",
       marker_width: 80,
       marker_height: 80,
       marker_anchor: null,
       shadow_anchor: null,
       shadow_picture: null,
       shadow_width: null,
       shadow_height: null
       })},10000)
    

    Verbose but wasn't supposed to be used directly :)

    BTW, it's not a good implementation:

    what if the user takes more time to accept geolocation (or simply refuses)?