gpt4 book ai didi

javascript - 通过 LAT LONG 的 OpenWeatherMap API 地理定位无法正常工作或无法打印

转载 作者:行者123 更新时间:2023-12-03 06:47:59 26 4
gpt4 key购买 nike

我已经获取了 API URL 并删除了变量,并在浏览器中插入了实际的 LAT 和 LONG 值,并且它可以工作。但当它在带有 LAT 和 LONG 变量的代码中使用时,它不起作用。

我提到了现有问题 herehere

我使用类似(如果不相同)的语法来成功提取和打印 LAT 和 LONG 值,然后存储变量。我正在将变量插入 API URL,但它没有打印。我不知道为什么。

谢谢!

这是我的 HTML

<html>
<head>
<meta charset="UTF-8">
<title>Today's Weather | Malvin</title>
</head>

<body>

<p id="geoLoc"></p>

<p id="currTemp"></p>

</body>
</html>

这是我的 JS/JQuery

//geolocation via freecodecamp exercise
$(document).ready(function(){
if (navigator.geolocation){
navigator.geolocation.getCurrentPosition(function(position){
$("#geoLoc").html("latitude: " + position.coords.latitude + "<br>longitude: " + position.coords.longitude);
});
}

//jquery current weather via openweathermapp
var lat = position.coords.latitude;
var long = position.coords.longitude;
var weatherURL = "http://api.openweathermap.org/data/2.5/weather?lat="+lat+"&lon="+long+"&APPID=********f4144190680fc96bd357451d";
$.getJSON(weatherURL, function(data)
{
$('#currTemp').html("current temp: " + data.main.temp);
});
});

最佳答案

由于您是通过.getJSON进行异步调用,因此您需要使用“done”形式的回调函数,或者您可以定义自己的自定义回调函数。例如:您可以使用“done”作为异步调用的回调,如下所示。

$.getJSON(weatherURL).done(function(data){
$('#currTemp').html("current temp: " + data.main.temp);
});

done 定义的函数将在 AJAX 调用成功时执行。

关于javascript - 通过 LAT LONG 的 OpenWeatherMap API 地理定位无法正常工作或无法打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37637776/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com