【问题标题】:OpenWeatherMap API - Trouble with append longitude and latitudeOpenWeatherMap API - 附加经度和纬度的问题
【发布时间】:2017-12-05 00:43:13
【问题描述】:

我正在尝试根据我通过.geolocation访问的用户的经度和纬度来定义搜索城市

在我的网址中添加 long 和 lat 时出现此错误

cod    :    "400"    message    :    "-104.9435462 is not a float"

getLocation();

//Find Location of Device
function getLocation(callback) {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(getData);
  }

}

//Get/set Variables
function getData(position) {
  var longitude = position.coords.longitude;
  var latitude = position.coords.latitude;
  var altitude = position.coords.altitude;
  var heading = position.coords.heading;
  var speed = position.coords.speed;
  var date = position.timestamp;

  getWeather(longitude, latitude);
  console.log(longitude);
  console.log(latitude);

}

//Call OpenWeatherAPI
function getWeather(x, y) {
  const apiKey = '';
  var url = 'http://api.openweathermap.org/data/2.5/forecast?lat=' + x + '&lon=' + y + '&APPID=280e605c456f0ba78a519edde1a641d3';

  $.ajax({
    url: url,
    success: function(result) {

    }
  });
}; //END getWeather()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

【问题讨论】:

  • 你解决了吗?
  • 我做了,但我选择了 api.wunderground.com,因为他们的 API 中有自动定位功能

标签: javascript ajax callback openweathermap


【解决方案1】:

迟到的答案,但希望这会节省其他人的时间。错误信息具有误导性。

发生错误是因为经度的范围为-180到180,而纬度的范围为-90到90。

在代码中,getWeather() 调用是这样进行的:

getWeather(longitude, latitude);

但是,注意纬度和经度(x,y)在函数中是有序的(纬度,经度):

  function getWeather(x, y) {
  const apiKey = '';
  var url = 'http://api.openweathermap.org/data/2.5/forecast?lat=' + x + '&lon=' + y + '&APPID= removed id';
// additional code
}; 

更改调用应该可以解决问题:

getWeather(latitude, longitude);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-01
    • 2021-10-10
    • 1970-01-01
    • 1970-01-01
    • 2021-08-08
    相关资源
    最近更新 更多