gmaps4rails v2.0.3 - no está definido

I am trying to implement the marker dragging functionality using Gmaps4rails gem following este ejemplo but I'm having problems wusing "" object that is undefined and I'm not able to add a callback for the map to handle draggable markers.

Right now the map is displaying correctly but I'm stuck on the dragging part.

I am using 'gmaps4rails', '2.0.3'

I create the markers in the controller:

@locations = Location.all
@hash = Gmaps4rails.build_markers(@locations) do |location, marker| location.latitude
  marker.lng location.longitude

En la vista:

= content_for :scripts do
  = javascript_include_tag "//"
  = javascript_include_tag '//'
  = javascript_include_tag "/assets/custom_scripts/map_scripts.js"

%div{ :style => 'width: 800px;' }
#map{ :style => 'width: 800px; height: 400px;' }

  createMap(#{raw @hash.to_json });

En el JS:

function createMap(items){
  var handler ='Google');
  handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
    markers = handler.addMarkers(items);

I am not sure if this documentation is outdated because they are still using

<%= gmaps("markers" => {"data" => @json, "options" => { "draggable" => true  } } ) %> 

and I read that the "gmap" function is not being used anymore on Gmaps4rails v2

preguntado el 27 de noviembre de 13 a las 01:11

I confirm you're mixing v1 and v2 ode. Please read the readme or watch the video -

2 Respuestas

Map objects are now attached to the handler.

  • retrieves the js proxy object the gem builds to handle the map and that you can customize
  • handler.getMap() retrieves the google map object
  • is the norm to get google object from proxy objects

The callback is now the function you pass in buildMap.

You have all markers in your markers var. They follow the norm so you can access the google marker object doing: markers[0].getServiceObject()

If you want to pass options to your markers, add them as a second argument:

handler.addMarkers(items, options);

Documentation is in the code, within builders, mire aquí

respondido 27 nov., 13:07

Thanks for the info it was really helpful. - Humphryman

I am passing the draggable option to the add markers function and it works fine, but the marker image is displayed stretched, do I need to add anything else to keep the normal marker image size? - Humphryman

See doc, its better you give picture's size - buceo en apnea

I would submit an issue on the github page:

It seems as though the repo is actively maintained, so I'm sure the repo owner wouldn't mind updating a few of his wiki pages.

That said, it seems as though the need for view helper methods has been superseded by attaching the map via DOM elements, so no helper methods may be needed at all. In other words, I bet if you just delete that line from the sample source code, it will probably work as expected.

respondido 27 nov., 13:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.