Javascript 如何在字符串变量中存储navigator.geolocation?
您好,我不明白为什么存储navigator.geolocation.getCurrentPosition会导致一个变量并使用它与其他字符串连接不起作用这两个Javascript 如何在字符串变量中存储navigator.geolocation?,javascript,geolocation,navigator,Javascript,Geolocation,Navigator,您好,我不明白为什么存储navigator.geolocation.getCurrentPosition会导致一个变量并使用它与其他字符串连接不起作用这两个getCurrentPosition()和fetch()都是异步的 您的流程需要看起来更像: const successfulLookup = position => { getLocationName(position).then(getWeatherData) } navigator.geolocation.getCurre
getCurrentPosition()
和fetch()
都是异步的
您的流程需要看起来更像:
const successfulLookup = position => {
getLocationName(position).then(getWeatherData)
}
navigator.geolocation.getCurrentPosition(successfulLookup)
const getLocationName = position => {
const {lat, long } = position
const forFetch = `https://api.opencagedata.com/geocode/v1/json?q=${lat}+${long}&key=87c637778f19465f89895cad4bf83cfa`
// return the fetch promise
return fetch(forFetch)
.then(response => response.json())
.then(res => res.results[0].components.village)
//.then(console.log) must reurn data after this log !!
}
const getWeatherData = locationName => {
const forWeather = `http://api.openweathermap.org/data/2.5/weather?q=${locationName}&appid=cb79b904798a1f67e15e9d71fb81bc11`
return fetch(forWeather)
.then(res => res.json())
.then(data=>{
// consume the wather data
console.log(data)
})
}
:(1)它不返回位置名,(2)它要求您提供一个成功回调,因为它是一个异步操作。请阅读更多关于处理异步操作的内容:您基本上想要的是实际等待查找,然后在成功回调中执行
$.getJSON()
请求。它一直告诉我:“未捕获的TypeError:无法读取successfulLookup(weather.js:2)未定义的属性'then'”const successfulLookup=position=>{getLocationName(position).then(getWeatherData)}哎呀……我在getLocationName()中留下了一条“返回获取承诺”的注释
并省略了return
语句。请参阅更新我本想使用您之前帮助过我的URL,但后来我在下面的函数中使用了它,它抛出了一个错误…请您再次提供帮助!!!我完成了!!!我完成了!!!天哪!!!非常感谢!!!谢谢!!!我已经坚持了2年了连续几天,你在我剩下的头发上节省了很多!!!!你太好了!!!!!太好了。当你跳过那样的障碍时感觉很满足
$.getJSON(getWeatherData, function(data){
console.log(data)
let weatherDescription = data.weather[0].description
let temp = data.main.temp
let location = data.name
const kelvin = temp
const celsius = kelvin - 273
let newton = celsius * ( 33 / 100 )
newton = Math.floor(newton)
$(".iconDescription").append(weatherDescription)
$(".temperature").append(newton + " ˚C")
$(".location").append(location)
switch (weatherDescription){
case "clear sky":
$(".Clouds").css("display", "none")
$(".overcastClouds").css("display", "none")
$(".moderateRain").css("display", "none")
break;
case "Clouds":
$(".clearskyIcon").css("display", "none")
$(".overcastClouds").css("display", "none")
$(".moderateRain").css("display", "none")
break;
case "broken clouds":
$(".clearskyIcon").css("display", "none")
$(".Clouds").css("display", "none")
$(".overcastClouds").css("display", "none")
$(".moderateRain").css("display", "none")
break;
case "overcast clouds":
$(".clearskyIcon").css("display", "none")
$(".Clouds").css("display", "none")
$(".moderateRain").css("display", "none")
break;
case "moderate rain":
$(".clearskyIcon").css("display", "none")
$(".Clouds").css("display", "none")
$(".overcastClouds").css("display", "none")
break;
case "few clouds":
$(".clearskyIcon").css("display", "none")
$(".overcastClouds").css("display", "none")
$(".moderateRain").css("display", "none")
break;
default:
$(".clearskyIcon").css("display", "none")
$(".Clouds").css("display", "none")
$(".overcastClouds").css("display", "none")
$(".moderateRain").css("display", "none")
}
})