I have a database in firebase that stores latitude and longitude values which I retrieve as the variable coords.
function getCoords() {
var place_data= firebase.database().ref("/place/name");
place_data.once('value').then(function(snapshot) {
var longitude = snapshot.child("Event_long").val();
var latitude = snapshot.child("Event_lat").val();
var coords = new google.maps.LatLng(latitude, longitude);
alert(coords); //after running this code, I get (43.6672568, -79.4000838) displayed in an alert message//
});
}
This code is for initializing Google Maps.
function initializeMap() {
var iconBase = {url:'/images/wing.pin.png'};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 15,
center: coords,
mapTypeId: 'satellite'
});
marker = new google.maps.Marker({
icon: iconBase,
map: map,
draggable: true,
animation: google.maps.Animation.DROP,
position: coords
});
marker.addListener('click', toggleBounce);
}
I am struggling to make the function initializeMap read the variable 'coords'. I've attempted making 'coords' a global variable and enclosing the getCoords function within another function, but they don't seem to work. Is there a more efficient way to achieve this?