I'm trying to get coordinates from various locations in my country to put markers+pop ups.
in some locations in my country mapbox with geoJSON forwarding gives invalid response, hence no coordinates for me.
I circumvented this by using google translate(using an npm api from @vitalets) , which usually works. so i parse the re-translated location name back to mapbox geoJSON.
but my question is how do i pass the re-translated name if it can only use it once in the forEach? should i save the name and make another geoJSON request to get the coordinates again(check out the "<<<<<<<" in the code)?
results.forEach(myGeocoder); // results is taken from the MySQL query, bringing all the info from the list
function myGeocoder(item,index){
console.log("location #28:"+location[28]);
const geoData =geocoder
.forwardGeocode({
query : item.residence,
autocomplete: false,
limit : 1
})
.send()
.then((response) => {
if (
!response ||
!response.body ||
!response.body.features ||
!response.body.features.length
) {
console.error('Invalid response:');// put a function for name changing here<<<<<<<<
console.error(response);
return;
}
const feature = response.body.features[0].geometry.coordinates;
console.log("feature="+feature);
longlat = feature;
// updates all coordinates for all teachers
connection.query('UPDATE list SET coordinates="'+longlat.toString()+'" WHERE list_id='+item.list_id+";", function(err,results){
if (err) {console.log("or not found<<<<<<<<<<<<<<<<<<<");
throw err;}
})
console.log("longlat="+longlat[0]+" ,longlat="+longlat[1])
}) ;
}
my idea to get this to work, was to rehash the names , remember those that came null, and call the geoJSON again with another loop using the new translated names, and only for those.