1

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:

enter image description here

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!

Kresek
  • 55
  • 1
  • 7

1 Answers1

0

from what I understand you're trying to store the firebase data in your dlugosc variable. Since you're declaring it inside your callback handler for the firebase query.

Maybe you could try declaring your dlugosc variable outside the function, so you can use it inside your initMap() function like this:

var dlugosc;
var longitude = firebase.database().ref().child('Polozenie_urzadzenia');
longitude.once("value", snap => {
   dlugosc = snap.child("Dlugosc").val();
   console.log(dlugosc);
});

function initMap() {
    var position = { lat: dlugosc.lat, lng: dlugosc.lng }; <- 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});
}

Sorry, I'm not sure how is that possible by by mistake I've edited not my post o.O

Kresek
  • 55
  • 1
  • 7
Taro
  • 126
  • 9
  • Thank you for the quick response! Regarding your tip - I've already tried it and the console says that "dlugosc" variable is undefined :( – Kresek May 14 '20 at 18:07
  • Do you have a sample of the model you're trying to load? – Taro May 14 '20 at 18:16
  • Sure I have, catch it - https://i.imgur.com/G4KQWZg.png Forgive me polish names, I did not think I will share it somewhere :) Dlugosc - Longitude Szerokosc - Latitude Dokladnosc - Accuracy – Kresek May 14 '20 at 18:38