Sunday, 9 April 2017

How can i use my variable in header.ejs?

I am building simple weather app and i want to use google maps but with my custom latitude and longitude. My code looks like this

app.post('/', (req, res) => {

  var address = req.body.address;

   location.getLocation(address, (error, results) => {
    if(error) {
      console.log(error);
    } else {
      fetchWeather.getWeather(results.latitude, results.longitude, (error, 
 weatherResults) => {
    if(error){
      console.log(error);
    } else {
      var latitude = results.latitude;
      var longitude = results.longitude;
      var temp = weatherResults.temperature;
      var appTemp = weatherResults.apparentTemperature;
      var summary = weatherResults.summary;
      var icon = weatherResults.icon;
      var windSpeed = weatherResults.windSpeed;
      var humidity = weatherResults.humidity;
      var pressure = weatherResults.pressure;
      var visibility = weatherResults.visibility;
      res.render('displayweather', {
        temp: temp,
        appTemp: appTemp,
        summary: summary,
        icon: icon,
        windSpeed: windSpeed,
        humidity: humidity,
        pressure: pressure,
        visibility: visibility,
        latitude: latitude,
        longitude: longitude
          });
        }
      });
    }
  });
});

I'm getting location from function:

  getLocation = (address, callback) => {
  var encodedAddress = encodeURIComponent(address);

  request({
    url: `http://maps.googleapis.com/maps/api/geocode/json?
address=${encodedAddress}`,
    json: true
  }, (error, response, body) => {
if(error) {
  callback('Unable to connect to Google servers.')
} else if(body.status ==='ZERO_RESULTS'){
  callback('Unable to find that adress.');
} else if(body.status === 'OK'){
  callback(undefined, {
      address: body.results[0].formatted_address,
      latitude: body.results[0].geometry.location.lat,
      longitude: body.results[0].geometry.location.lng
        });
      }
  });
};

I included script tag in header.ejs which i include in my rendering page "displayWeather". Script inside header looks like this:

     function initMap() {
     var uluru = {lat: -25.363, lng: 131.044};
    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 4,
      center: uluru
    });
    var marker = new google.maps.Marker({
      position: uluru,
      map: map
    });
  }

Now i am exporting latitude and longitude to the displayWeather.ejs but i want them in header. How can i do that?



via JKshort

No comments:

Post a Comment