Search code examples
javascriptruby-on-railsgoogle-mapsgmaps4rails

Gmaps4rails location set


I need to display in map current user location. I am using gem gmaps4rails. That's i have in my views, i just find tutorial on github and build the map on page, but in tutorial is nothing to say about user location function.

                            <script src="//maps.google.com/maps/api/js?key=my api key"></script>
                            <script src="//cdn.rawgit.com/mahnunchik/markerclustererplus/master/dist/markerclusterer.min.js"></script>
                            <script src='//cdn.rawgit.com/printercu/google-maps-utility-library-v3-read-only/master/infobox/src/infobox_packed.js' type='text/javascript'></script>
                        <p id="notice"><%= notice %></p>

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

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

Solution

  • using handler to initialize map i do something like

    handler.map.centerOn({ lat: 5.574076, lng: -75.5926227 })
    

    here you can find more info https://github.com/apneadiving/Google-Maps-for-Rails/wiki/Js-Methods

    function intializeMap(){
      handler = Gmaps.build('Google');
      handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
        markers = handler.addMarkers("the DATA here");
        handler.bounds.extendWith(markers);
        handler.map.centerOn({lat: 5.574076, lng: -75.5926227});
        handler.fitMapToBounds();
      });
    }

    hope this works to you.