0

I'm trying to include googlemaps in my MEAN stack app. I have set up a googleMap directive and I'm pulling in the lat lng coordinates from some seeded test data. If I hard code the lat lng then the map renders however, pulling it in from the db I just get a grey square. So google maps can't find the lat lng it needs to show a map.

Schedule is my resource and I've included the relevant tags in my meta data.

My googleMap directive below:

angular.module('yogaApp')
  .directive('googleMap', googleMap);

googleMap.$inject = ['$window'];
function googleMap($window) {
  return {
    restrict: 'E',
    replace: true,
    template: '<div class="google-map">Google Map HERE!!</div>',
    scope: {
      schedule: '=',
   },
link: function(scope, element) {
  scope.$watch('schedule', () => {
    if(scope.schedule) {
      console.log(scope.schedule);
      console.log(scope.schedule.location);

      const map = new $window.google.maps.Map(element[0], {
        // center: {lat: 51.4638587, lng: -2.6113025}, THIS WORKS
        center: scope.schedule.location,
        zoom: 15,
        scrollwheel: false
      });
    }
   });
  }
 };
}

the console log marked schedule returns the promsie, with the location object (including lat lng) however from consoleLog(schedule.location) I get undefined.

see screen shot:

enter image description here console log results

This is why my map isn't rendering (obviously can't show a map without the lat lng) and I think it's due to the asynchronous nature of JS, so possibly all I need to add is a call back function?

cmac
  • 3,123
  • 6
  • 36
  • 49
  • Can you try: center: { lat: parseFloat(scope.schedule.location.lat), lng: parseFloat(scope.schedule.location.lng) } – Paul Thomas Mar 30 '17 at 12:09
  • Actually, I see the problem now. I think this answer will help you - http://stackoverflow.com/questions/16196121/angular-access-resource-value-in-controller – Paul Thomas Mar 30 '17 at 12:15
  • Thanks! That is the right answer. I did find a quicker way by adding ng-if='schedulesShow.schedule.$resolved" – Chris Norman Apr 06 '17 at 15:11

0 Answers0