Gmaps4rails gem: method to_gmaps4rails undefined

I am trying to dynamically update markers on google map using gmaps4rails (i.e. with an AJAX callback). I find the documentation for gmaps4rails very scattered and unclear.

I was able to display markers on the map using the build_markers method (as per video tutorial v2).

My controller code:

# GET /telemetry_recordings
def index

    @telemetry_recordings = TelemetryRecording.all
    @hash = Gmaps4rails.build_markers(@telemetry_recordings) do |telemetry_recording, marker|
            marker.lat telemetry_recording.latitude
            marker.lng telemetry_recording.longitude
            marker.title telemetry_recording.delivery_unit.driver.full_name
            marker.infowindow telemetry_recording.delivery_unit.driver.full_name
    end
end

      

My view code:

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

      

Now, to implement dynamic update, I added the following script to my view:

<script type="text/javascript">
    jQuery(function($) {
        $('body').on('click', '#replace_markers', function(){ 
            $.getJSON("/telemetry_recordings", function(data){
                Gmaps.map.replaceMarkers(data);
            });
        });
    });   
</script>

      

Just like a button:

<button id="replace_markers">Refresh</button>

      

And I added the following code to the controller:

respond_to :json, :html

# GET /telemetry_recordings
def index

    @json = TelemetryRecording.all.to_gmaps4rails
    respond_with @json

end

      

Note. The TelemetryRecording class has 3 attributes: latitude (float), longitude (float) and location_time (DateTime)

This results in the following error:

undefined method `to_gmaps4rails' for #<TelemetryRecording::ActiveRecord_Relation:0x55dee78>

      

According to the documentation, I installed the gmaps4rails gem added // = require underscore and // = requires gmaps / google for my application.js file and included the following scripts in my opinion:

<script src="//google-maps-utility-library-v3.googlecode.com/svn/tags/markerclustererplus/2.0.14/src/markerclusterer_packed.js" type="text/javascript"></script>
<script src="//maps.google.com/maps/api/js?v=3.13&sensor=false&libraries=geometry" type="text/javascript"></script>

      

Am I using to_gmaps4rails correctly here? (I understand that it will convert an object with latitude / longitude attributes to an array of markers, like [{"lat": "x", "lng": "y"}, {"lat": "x", "lng": "y"}] Why is it undefined?

+3


source to share


1 answer


You are using code from v1.x and 2.x.

to_gmaps4rails

has been removed from the codebase. So your first snapshot was fine:

def index
  @hash = Gmaps4rails.build_markers(TelemetryRecording.all) do |telemetry_recording, marker|
     marker.lat telemetry_recording.latitude
     marker.lng telemetry_recording.longitude
     marker.title telemetry_recording.delivery_unit.driver.full_name
     marker.infowindow telemetry_recording.delivery_unit.driver.full_name
  end
  respond_with @hash
end

      



and in js

<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();

        $('body').on('click', '#replace_markers', function(){ 
            $.getJSON("/telemetry_recordings", function(newMarkers){
                handler.removeMarkers(markers); // to remove previous markers
                markers = handler.addMarkers(newMarkers);
            });
        });
    });   
    });
</script>

      

+3


source







All Articles