apneadiving / Google-Maps-for-Rails

Enables easy Google map + overlays creation in Ruby apps
https://apneadiving.github.io/
MIT License
2.27k stars 382 forks source link

this.clusterer is null #447

Closed gabebw closed 10 years ago

gabebw commented 10 years ago

Hi,

In my code, the Chrome debugger is telling me that this.clusterer is null in Handler.prototype.addMarker. I'm calling handler.addMarkers(<%=raw @hash.to_json %>) and the hash is correctly being generated, but it crashes on that. Any ideas?

apneadiving commented 10 years ago

Could you copy paste the generated js ?

Sent from my iPad

On 24 Jul 2014, at 23:22, Gabe Berke-Williams notifications@github.com wrote:

Hi,

In my code, the Chrome debugger is telling me that this.clusterer is null in Handler.prototype.addMarker. I'm calling handler.addMarkers(<%=raw @hash.to_json %>) and the hash is correctly being generated, but it crashes on that. Any ideas?

— Reply to this email directly or view it on GitHub.

gabebw commented 10 years ago

I think this is what you want. It's at the bottom of my view:

  $(function(){
    markers = window.handler.addMarkers([{"lat":42.3584308,"lng":-71.0597732},{"lat":40.7127837,"lng":-74.0059413},{"lat":42.2286954,"lng":-71.5225646},{"lat":42.3736158,"lng":-71.1097335}]);
  });

And here's the contents of my file, autoloaded in application.js:

$(function(){
  window.handler = Gmaps.build('Google');
  handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
    markers = handler.addMarkers([
      {
        "lat": "42.3584308",
        "lng": "-71.0636",
        "picture": {
          "url": "http://www.googlemapsmarkers.com/v1/009900/",
          "width":  36,
          "height": 36
        },
        "infowindow": "hello!"
      }
    ]);
    handler.bounds.extendWith(markers);
    handler.fitMapToBounds();
    handler.getMap().setZoom(10);
  });
});

The exact error I get is:

Uncaught TypeError: Cannot read property 'addMarker' of undefined (handler.js line 38)

It's trying to call this.clusterer.addMarker.

apneadiving commented 10 years ago

I guess application.js is called after your $() line.

Order matters in js

gabebw commented 10 years ago

That makes sense. Is there any way to guarantee that one of the handlers happens after the other one? I assume that jQuery runs each event handler in the event that they're added.

apneadiving commented 10 years ago

You should create a js function which creates the map and pass it your callback

gabebw commented 10 years ago

Thank you! That worked.

mrpadi commented 9 years ago

Can you give an example on how to do that? I've been struggling for a while Thanks a lot!

gabebw commented 9 years ago

@mrpadi It's been a while since I looked at the code, but if you link to a gist of your JS code, I can take a look.