zakir2k
zakir2k

Reputation: 57

Meteor.js Google Maps initialize

I'm having issues initialising my google map with markers coming from the database in Meteor. I've put console.log messages everywhere to test it - it seems to be very inconsistent - with sometimes the markers being created and loaded - and at other times it enters the initialize function, but it won't enter the loop to create the markers. I have a waitOn subscription in the router to make sure that the data is loaded and is available before running the initialize function.

html template:

<template name="mapGoogle">
    <div id="map-canvas" style="width:900px; height:420px;"></div>
</template>

client js:

Template.mapGoogle.rendered = function(){
  Session.set('loading', true');
  initialize();
}

var initialize = function(){
console.log('inside initialize');

var latitude = 22.2924391;
var longitude = 94.1967784;

var map; var icon; var lat; var lng; var icon;
var location = new google.maps.LatLng(latitude, longitude);
var markers = [];

var styles = [
  {
    stylers: [
      { hue: '#3cff00' },
      { visibility: 'simplified' },
      { gamma: 0.5 },
      { weight: 0.5 }
    ]
  },
  {
    elementType: 'labels',
    stylers: [
      { visibility: 'on' }
    ]
  },
  {
    featureType: 'water',
    stylers: [
      { color: '#83bbdd' }
    ]
  }
];

var styledMap = new google.maps.StyledMapType(styles,
  {name: "Styled Map"});

// Create a map object, and include the MapTypeId to add
// to the map type control.
var mapOptions = {
  zoom: 2,
  center: new google.maps.LatLng(latitude, longitude),
  mapTypeControlOptions: {
    mapTypeIds: [google.maps.MapTypeId.TERRAIN, 'map_style']
  }
};

map = new google.maps.Map(document.getElementById('map-canvas'),
  mapOptions);

map.mapTypes.set('map_style', styledMap);
map.setMapTypeId('map_style');

  var sharkFin;

  Sharks.find().forEach(function(shark){
    console.log('inside shark for loop)
    sharkFin = '/images/fins/shark_fin.png';
    latShark = shark.latitude;
    lngShark = shark.longitude;

  marker = new google.maps.Marker({
      position: new google.maps.LatLng(latShark, lngShark),
      map: map,
    animation: google.maps.Animation.DROP,
      icon: sharkFin,
    });
  })

  Airlines.find().forEach(function(airline){

    console.log('inside airline for loop');

    icon = airline.iconAir;
    lat = airline.latitude;
    lng = airline.longitude;
    title = airline.title;
    content = airline.content;
    if(airline.webLink){webLink = airline.webLink}else{webLink=""};

    marker = new google.maps.Marker({
      position: new google.maps.LatLng(lat, lng),
      map: map,
    animation: google.maps.Animation.DROP,
      icon: icon,
      title: title,
      content: content,
      webLink: webLink
    });

    var listener3 = google.maps.event.addListener(marker, 'mouseover', function(marker) {
      console.log('inside marker listener');
      return function(){
        $('#content').find('div').remove();
        if(webLink==""){
          $('#content').append('<div><h1>'+ marker.title+'</h1><br>'+ marker.content+'<div>');
        }else{
          $('#content').append('<div><h1>'+ marker.title+'</h1><br>'+ marker.content+"<br><br>More information can be found on the airline's website <a href="+marker.webLink+'>here</a>.<div>');
        }
      }
    }(marker)); //use closure for access to variables.
  });
  console.log('session variable set to false');
  Session.set('loading',false);

}

Router.js -

Router.map(function() {

  this.route('mainPage', {path:'/',
    onBeforeAction: function(){
      console.log('inside router session to true');
      Session.set('loading',true);
    },
    waitOn: function(){
      return [Meteor.subscribe('airlines'),
              Meteor.subscribe('sharks')];
    }
  });
});

It seems as though on the first rendering of the page - the markers are created and animated correctly on the map. However, if I refresh the page, sometimes the markers are created and other times they are not.

I have also tried using google.maps.event.addDomListener(window, 'load', initialize) on the mapGoogle rendered js code. But still the same problem exists.

Any suggestions please? Thanks.

Upvotes: 0

Views: 997

Answers (1)

zakir2k
zakir2k

Reputation: 57

@Saimeunt is correct.

I needed to add default loading hook for iron-router using Router.onBeforeAction("loading"). Otherwise the waitOn subscriptions are not really applied and the page is loaded before the data has been loaded up.

Upvotes: 1

Related Questions