Несколько погодных локаций - PullRequest
0 голосов
/ 07 октября 2018

У меня есть простой код для вызова местоположения погоды, но мне нужно, по крайней мере, два местоположения.Как я могу получить несколько результатов на основе координат?

Я нашел этот код, который довольно прост, где вы пишете координаты, чтобы получить местоположение, и все.Но мне нужно, чтобы несколько сайтов отображалось на сайте.Я знаю, что это простой вопрос, но было бы очень хорошо, если бы кто-нибудь мог помочь мне в этом.

Вот мой код:

<script type="text/javascript">
$(document).ready(function() {
  var Celsius;
  var Fahrenheit;
  $.ajaxSetup({ cache:true}); 
  setRandomColor();
  getLocation(getWeather);
  $("#temp-slider").click(sliderChange);
});
function getRandomColor() {
  var letters = '0123456789ABCDEF';
  var color = '#' + letters[Math.round(Math.random() * 5)];
  for (var i = 0; i < 5; i++) {
    color += letters[Math.floor(Math.random() * 15)];
  }
  return color;
}
function setRandomColor() {
  var newColor = getRandomColor();
  $(".weather-current").css("color", newColor);
}
function getLocation(callback) {
  $.getJSON("https://geoip-db.com/json/", function(json) {
    callback(json.latitude, json.longitude, setWeather);
});
}
function getWeather(lat, lon, callback) {
  $.getJSON("https://fcc-weather-api.glitch.me/api/current?lat=25.6750700&lon=-100.3184700", function(json) {
    callback(json);
});
}
function setWeather(weather) {
  var temp = Math.round(weather.main.temp * 10) / 10;
  Celsius = temp + ' °C';
  $(".weather-current p").html(Celsius);
}
function sliderChange() {
  if ($(".slider").css('background-color') !== 'rgb(204, 204, 204)') {
    $(".weather-current p").html(Fahrenheit);
  } else {
    $(".weather-current p").html(Celsius);
  }
}

Ответы [ 2 ]

0 голосов
/ 13 октября 2018

Конечно, это html <div class="weather-current"><p> </p></div>, и весь сценарий этот.Что мне нужно, это добавить еще одно место из этой части.Спасибо !!

<script type="text/javascript">
    $(document).ready(function() {
      var Celsius;
      var Fahrenheit;
      $.ajaxSetup({ cache:true}); 
      setRandomColor();
      getLocation(getWeather);
      $("#temp-slider").click(sliderChange);
    });
    function getRandomColor() {
      var letters = '0123456789ABCDEF';
      var color = '#' + letters[Math.round(Math.random() * 5)];
      for (var i = 0; i < 5; i++) {
        color += letters[Math.floor(Math.random() * 15)];
      }
      return color;
    }
    function setRandomColor() {
      var newColor = getRandomColor();
      $(".weather-current").css("color", newColor);
    }
    function getLocation(callback) {
      $.getJSON("https://geoip-db.com/json/", function(json) {
        callback(json.latitude, json.longitude, setWeather);
    });
    }
    function getWeather(lat, lon, callback) {
      $.getJSON("https://fcc-weather-api.glitch.me/api/current?lat=25.6750700&lon=-100.3184700", function(json) {
        callback(json);
    });
    }
    function setWeather(weather) {
      var temp = Math.round(weather.main.temp * 10) / 10;
      Celsius = temp + ' °C';
      $(".weather-current p").html(Celsius);
    }
    function sliderChange() {
      if ($(".slider").css('background-color') !== 'rgb(204, 204, 204)') {
        $(".weather-current p").html(Fahrenheit);
      } else {
        $(".weather-current p").html(Celsius);
      }
    }
 </script>
0 голосов
/ 07 октября 2018

Используйте параметры в определении функции для строки запроса getWeather url.

function getWeather(lat, lon, callback) {
  $.getJSON(
    `https://fcc-weather-api.glitch.me/api/current?lat=${lat}&lon=${lon}`,
    callback
  );
}
...