dburles / meteor-google-maps

🗺 Meteor package for the Google Maps Javascript API v3
https://atmospherejs.com/dburles/google-maps
MIT License
196 stars 49 forks source link

Re-render map on coordinate change #141

Closed healthycola closed 7 years ago

healthycola commented 7 years ago

Hello!

Is it possible to re render the map if the coordinate (lat/long) change in the backend. If the location().lat/location().long change, I would ideally like the map to update. Is this possible?

Thanks!

locationData: function() {
    if (GoogleMaps.loaded() && this.location()) {
        var output = {
            center: new google.maps.LatLng(this.location().lat, this.location().long),
            zoom: 8
        }

        return output;
    }
  }
dburles commented 7 years ago

Yes, rather than updating in the helper, you'll want to do that within the ready callback. Something like:

Template.myMap.onCreated(function() {
  GoogleMaps.ready('myMap', (map) => {
    this.autorun(() => {
      // reference reactive things here like findOne or Session, reactive-var etc
      const lat = Session.get('lat');
      const lng = Session.get('lng');
      // I *think* this is the correct method, you might want to refer to the Google docs
      map.instance.setCenter(..., ...);
    });
  });
});