• Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
MagicMirror Forum
  • Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
A New Chapter for MagicMirror: The Community Takes the Lead
Read the statement by Michael Teeuw here.

Including socketNotificationReceived in getDom function

Scheduled Pinned Locked Moved Development
8 Posts 2 Posters 3.1k Views 2 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    yours.mukul
    last edited by Oct 30, 2017, 8:45 PM

    Hi! I’m trying to hack down the google maps modules. I made a node_helper.js to capture the latitude and longitude of desired location, using url.

    the code of main file looks like this

    getDom: function() {
           
           
    		var wrapper = document.createElement("div");
            wrapper.setAttribute("id", "map");
    
            wrapper.style.height = this.config.height;
            wrapper.style.width = this.config.width;
    
            var script = document.createElement("script");
            script.type = "text/javascript";
            script.src = "https://maps.googleapis.com/maps/api/js?key=" + this.config.key;
            document.body.appendChild(script);
            script.onload = function () {
                var map = new google.maps.Map(document.getElementById("map"), {
                	zoom: 13,
                	center: {
                		lat: lat,
                		lng: lng
                	}
                });
    
                var trafficLayer = new google.maps.TrafficLayer();
                trafficLayer.setMap(map);
            };
    
    		return wrapper;
    	}
    
    

    The latitude and longitude is stored in payload. All I require is ho to use socketNotificationReceived to parse both values as “lat” and “long” in script??

    ? 2 Replies Last reply Oct 30, 2017, 11:46 PM Reply Quote 0
    • ? Offline
      A Former User @yours.mukul
      last edited by Oct 30, 2017, 11:46 PM

      @yours.mukul
      getDom() in node_helper.js?

      Y 1 Reply Last reply Oct 31, 2017, 1:58 AM Reply Quote 0
      • Y Offline
        yours.mukul @Guest
        last edited by Oct 31, 2017, 1:58 AM

        @Sean getDom() in Maps.js

        1 Reply Last reply Reply Quote 0
        • ? Offline
          A Former User @yours.mukul
          last edited by Oct 31, 2017, 9:14 AM

          @yours.mukul
          try this

          in your node_helper.js

          var geo = {
            lat: 12.34567
            lng: 23.45678
          }
          this.sendSocketNotification("REFRESH_GEO", geo)
          

          in your Main Module

          start: function(){
            this.geo = {
              lat: 0, //default latitude
              lng: 0, //default longitude
            }
          },
          
          socketNotificationReceived: function(noti, payload) {
            if (noti == "REFRESH_GEO") {
              this.geo.lat = payload.lat
              this.geo.lng = payload.lng
              this.updateDom()
            }
          },
          
          getDom: function() {
            var lat = this.geo.lat
            var lng = this.geo.lng
          
            var wrapper = ...
          },
          
          1 Reply Last reply Reply Quote 1
          • Y Offline
            yours.mukul
            last edited by Nov 1, 2017, 7:44 AM

            @Sean said in Including socketNotificationReceived in getDom function:

            var lat = this.geo.lat
            var lng = this.geo.lng

            not working, it always shows up 0,0 latitude and longitude…

            ? 1 Reply Last reply Nov 1, 2017, 8:06 AM Reply Quote 0
            • ? Offline
              A Former User @yours.mukul
              last edited by A Former User Nov 1, 2017, 8:09 AM Nov 1, 2017, 8:06 AM

              @yours.mukul said in Including socketNotificationReceived in getDom function:

              @Sean said in Including socketNotificationReceived in getDom function:

              var lat = this.geo.lat
              var lng = this.geo.lng

              not working, it always shows up 0,0 latitude and longitude…

              It means, your socketNotificationReceived() in module is not working properly. Try this.

              socketNotificationReceived: function(noti, payload) {
                console.log("NOTIFICATION IS FIRED:", noti)
                if (noti == "REFRESH_GEO") {
                  console.log("PAYLOAD IS TREANFERED:", payload)
                  this.geo.lat = payload.lat
                  this.geo.lng = payload.lng
                  console.log("I'LL UPDATE DOM BY REFRESHED GEO")
                  this.updateDom()
                }
              },
              

              See how the log says. Check correct Socket Notification is fired with correct payload. If not, check your this.sendSocketNotification() in node_helper.js

              Y 1 Reply Last reply Nov 1, 2017, 10:09 AM Reply Quote 0
              • Y Offline
                yours.mukul @Guest
                last edited by Nov 1, 2017, 10:09 AM

                @Sean
                node_helper.js

                var NodeHelper = require("node_helper");
                module.exports = NodeHelper.create({
                start: function() {
                
                var geo = {
                  lat: 12.34567,
                  lng: 23.45678
                }
                this.sendSocketNotification("REFRESH_GEO", geo);
                }
                }
                );
                

                main.js

                /* global Module */
                
                /* Magic Mirror
                 * Module: MMM-GoogleMapsTraffic
                 *
                 * By Victor Mora
                 * MIT Licensed.
                 */
                
                Module.register("MMM-GoogleMapsTraffic", {
                       start: function(){
                  this.geo = {
                    lat: 0, //default latitude
                    lng: 0 //default longitude
                  }
                     },
                socketNotificationReceived: function(noti, payload) {
                  console.log("NOTIFICATION IS FIRED:", noti)
                  if (noti == "REFRESH_GEO") {
                    console.log("PAYLOAD IS TREANFERED:", payload)
                    this.geo.lat = payload.lat,
                    this.geo.lng = payload.lng
                    console.log("I'LL UPDATE DOM BY REFRESHED GEO")
                    this.updateDom();
                  }
                },
                	getDom: function() {
                        var lat = this.geo.lat;
                        var lng = this.geo.lng;
                
                		var wrapper = document.createElement("div");
                        wrapper.setAttribute("id", "map");
                
                        wrapper.style.height = this.config.height;
                        wrapper.style.width = this.config.width;
                
                        var script = document.createElement("script");
                        script.type = "text/javascript";
                        script.src = "https://maps.googleapis.com/maps/api/js?key=" + this.config.key;
                        document.body.appendChild(script);
                
                        script.onload = function () {
                            var map = new google.maps.Map(document.getElementById("map"), {
                            	zoom: 13,
                            	center: {
                            		lat: lat,
                            		lng: lng
                            	}
                            });
                
                            var trafficLayer = new google.maps.TrafficLayer();
                            trafficLayer.setMap(map);
                        };
                
                		return wrapper;
                	}
                
                });
                
                ? 1 Reply Last reply Nov 1, 2017, 10:44 AM Reply Quote 0
                • ? Offline
                  A Former User @yours.mukul
                  last edited by Nov 1, 2017, 10:44 AM

                  @yours.mukul
                  I think it is not good to put the noti into start() of node_helper.js.
                  Because after finishing module loaded and DOM created, updateDOM() would be working properly, but your code tried too early.
                  As you’ve said, you had your code in node_helper.js to get REAL latitude & longitude, isn’t it? My code was just an example. Send your REAL lat & lng to main module when your node_helper code gathers real target values.

                  1 Reply Last reply Reply Quote 0
                  • 1 / 1
                  1 / 1
                  • First post
                    1/8
                    Last post
                  Enjoying MagicMirror? Please consider a donation!
                  MagicMirror created by Michael Teeuw.
                  Forum managed by Sam, technical setup by Karsten.
                  This forum is using NodeBB as its core | Contributors
                  Contact | Privacy Policy