Recently I'm playing with Firebase and a simple web app project based on it. I'm stuck at one simple thing... Mainly, I want to get data from realtime database what I actually did, and then pass it to the variable - and here is my problem. I've seen few discussions here (including this one - JavaScript - Firebase value to global variable) and unfortunately, I STILL CAN'T do that.
I'm just gathering latitude and longitude with my ESP32, then it is sent to firebase and then I'm trying to use it to show map on my webpage. I have a map, but I'm not able to pass coordinates to it :(
Exemplary code I'm using:
var longitude = firebase.database().ref().child('Polozenie_urzadzenia');
longitude.once("value", snap => {
var dlugosc = snap.child("Dlugosc").val();
console.log(dlugosc);
});
function initMap() {
var position = {lat: 52.2375259, lng: 21.1394577}; <- I want to pass longitude and latitude here
var map = new google.maps.Map(
document.getElementById('map'), {zoom: 10, center: position});
var marker = new google.maps.Marker({position: position, map: map});
}
Thanks a lot in advance!
UPDATE!!!!!!!!!!
I've managed to achieve something like this:
Using that code:
function getLongitude(longitude) {
longitude = firebase.database().ref("Polozenie_urzadzenia/Dlugosc");
return longitude.once('value').then(function(snapshot){
var dlugosc = snapshot.val();
return dlugosc;
})
and now, how to pass it to my "lng" here?
function initMap() {
var position = {lat: 52.2375259, lng: 21.1394577};
var map = new google.maps.Map(
document.getElementById('map'), {zoom: 10, center: position});
var marker = new google.maps.Marker({position: position, map: map});
}
Thank you in advance!