1

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

Community
  • 1
  • 1
Stephane
  • 13
  • 3

1 Answers1

0

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

apneadiving
  • 114,565
  • 26
  • 219
  • 213
  • yep.Do the trick !You right it's not a LT implementation. I just wanted make my geolocation appear. Thanks for this good Gmaps4rails gem. Really helpful – Stephane Apr 15 '12 at 23:07
  • This takes a while to get it shown, is there something that is faster to make appear? – locoboy Nov 24 '12 at 07:50
  • @locoboy Yes see here: 'https://github.com/apneadiving/Google-Maps-for-Rails/blob/1.x/app/assets/javascripts/gmaps4rails/gmaps4rails.base.js.coffee#L99'. Just define `Gmaps.map.geolocationSuccess` the way you'd define `Gmaps.map.callback` – apneadiving Nov 24 '12 at 08:46