I am trying to use a variable in another function to create a map marker using angular.
i got the data to be stored in the function and display on the console within one function however when using the data in another function it does not work:
this.getparamformid = this.route.snapshot.paramMap.get('facviewid');
this.daformfacservice
.getOneDAFacForm(this.getparamformid)
.subscribe((daFormFac: DAFormFac) => {
this.daformfacs = daFormFac;
console.log(daFormFac, 'response of form');
this.latitude = daFormFac['latitude'];
this.longitude = daFormFac['longitude'];
console.log(this.latitude, this.longitude, "cords")
});
let map = L.map('map').setView([10.536421, -61.311951], 8);
L.tileLayer(
'https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}',
{
attribution:
'Map data © <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
maxZoom: 18,
id: 'mapbox/streets-v11',
tileSize: 512,
zoomOffset: -1,
accessToken:
'accesstoken',
}
).addTo(map);
var marker = L.marker([this.latitude, this.longitude]).addTo(map);
console.log(this.latitude, this.longitude, "in marker")
}
This function gets the data entry and stored 2 variables latitude and longitude:
this.daformfacservice
.getOneDAFacForm(this.getparamformid)
.subscribe((daFormFac: DAFormFac) => {
this.daformfacs = daFormFac;
console.log(daFormFac, 'response of form');
this.latitude = daFormFac['latitude'];
this.longitude = daFormFac['longitude'];
console.log(this.latitude, this.longitude, "cords")
});
and it results in
however when trying to access the data outside the function the console is blank.
var marker = L.marker([this.latitude, this.longitude]).addTo(map);
console.log(this.latitude, this.longitude, "in marker")
It does not display and is not accessible. what should i do?