This is because of the callback pattern. Helpers have already returned undefined by the time callbacks return data. You need to use synchronous javascript inside helpers, and if during asynchronous operation use reactive Meteor Session hashes to transfer data through:
Template.header.helpers({ locationCheck: function() { return Session.get("locationCheck"); }, isLoading:function() { return Session.equals("locationCheck",null); } });
Then in your header, when the template is created , you can run the check:
Template.header.created = function() { navigator.geolocation.getCurrentPosition(success_callback,error_callback); function success_callback(p){ // Building Latitude = 51.522206 // Building Longitude = -0.078305 var lat = parseFloat(p.coords.latitude); var lon = parseFloat(p.coords.longitude); console.log('Latitude: '+lat); console.log('Longitiude: '+lon); if( lat >= 51.521606 && lat <= 51.522606 && lon >= -0.078805 && lon <= -0.077705 ) { console.log('you are in the area'); Session.set("locationCheck",1); } else { console.log('you are not in the area'); Session.set("locationCheck",0); } } function error_callback(p){ return 0; } }
Once Session.set("locationCheck",1) (or 0 ) is set, the template will be redrawn with the new data.
You can use the isLoading helper when capturing a location:
<template name="header"> {{#if isLoading}} Loading {{else}} {{#if locationCheck}} {{>template1}} {{else}} {{>template0}} {{/if}} {{/if}} </template> <template name="template0"> <p>Denied</p> </template> <template name="template1"> Approved </template>
source share