javascript - String value becomes undefined when trying to concat another string -
i'm working on practicing openweathermap api. have coordinate object keys lat & lon equal string. when pass coord obj function , try concat strings api call string become undefined. thought made scope of these variables global doesn't seem case. can tell me incorrect code
var apikey = '9575f3355ae129dc91424b5712a7695e'; var coords = {}; var accessowm=''; function mylocation(){ navigator.geolocation.getcurrentposition(function(position) { coords.lat = (math.round(position.coords.latitude*100)/100).tostring(); coords.lon = (math.round(position.coords.longitude*100)/100).tostring(); }); } function changeaccess(coordobj, key){ console.log(coordobj); accessowm ='http://api.openweathermap.org/data/2.5/forecast?lat='+coordobj['lat']+'&lon='+coordobj['lon']+'&appid='+key; } mylocation(); console.log(coords); changeaccess(coords, apikey); console.log(accessowm);
that's because getcurrentposition method asynchronous. mean getcurrentposition's callback not invoked @ moment of calling changeaccess function. have place changeaccess call getcurrentposition's callback:
function mylocation() { navigator.geolocation.getcurrentposition(function(position) { coords.lat = (math.round(position.coords.latitude*100)/100).tostring(); coords.lon = (math.round(position.coords.longitude*100)/100).tostring(); }); changeaccess(coords, apikey); }
Comments
Post a Comment