Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/420.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用Yahoo'输入位置;使用simplewather.js的天气API?_Javascript_Jquery_Weather Api_Yahoo Weather Api - Fatal编程技术网

Javascript 使用Yahoo'输入位置;使用simplewather.js的天气API?

Javascript 使用Yahoo'输入位置;使用simplewather.js的天气API?,javascript,jquery,weather-api,yahoo-weather-api,Javascript,Jquery,Weather Api,Yahoo Weather Api,我正在建立一个显示天气的网页。我希望它能够使用地理定位,以及选择手动输入一个位置拉天气信息。我的地理定位工作正常,但我不确定如何使用city或zipcode添加额外的输入 这是我的相关代码: if ("geolocation" in navigator) { navigator.geolocation.getCurrentPosition(function(position) { loadWeather(position.coords.latitude + ',' + p

我正在建立一个显示天气的网页。我希望它能够使用地理定位,以及选择手动输入一个位置拉天气信息。我的地理定位工作正常,但我不确定如何使用city或zipcode添加额外的输入

这是我的相关代码:

if ("geolocation" in navigator) {
    navigator.geolocation.getCurrentPosition(function(position) {
        loadWeather(position.coords.latitude + ',' + position.coords.longitude);
    });
} else {
    loadWeather("", "1062617");
}

function loadWeather(location, woeid) {
    $.simpleWeather({
        location: location,
        woeid: woeid,
        unit: 'f',
        success: function(weather) {
          $(".weather-text").html(weatherText[weather.code]);
          $(".weather-icon").html('<i class="icon-' + weather.code + '"></i>');
      $(".weather-stats").html('<ul><li><strong>'+weather.city+', ' +weather.region+ '</strong></li>');
      $(".weather-stats").append('<li>'+ weather.temp + '&deg;F / '+ weather.alt.temp +'&deg;C</li></ul>');
    },
    error: function(error) {
        $("#weather").html('<p>' + error + '</p>');
    }
  });
}
if(导航器中的“地理位置”){
navigator.geolocation.getCurrentPosition(函数(位置){
loadWeather(position.coords.latitude+','+position.coords.longitude);
});
}否则{
负荷天气(“1062617”);
}
功能loadWeather(位置,woeid){
$.SimpleWither({
地点:地点,,
沃伊德:沃伊德,
单位:f,
成功:功能(天气){
$(“.weather text”).html(weatherText[weather.code]);
$(“.weather icon”).html(“”);
$(“.weather stats”).html(“
  • ”+weather.city+”、“+weather.region+”
  • ); $(“.weather stats”)。追加(“
  • ”+weather.temp+”°;F/“+weather.alt.temp+”°;C
”); }, 错误:函数(错误){ $(“#weather”).html(“”+错误+”

”); } }); }

我对javascript和jquery非常陌生,因此我知道如何使用HTML制作输入框,但不确定如何使用用户输入数据检索相关的天气数据。

通过
getCurrentPosition
可以访问
位置
界面,该界面将为您提供坐标。因此,
if
语句会根据设备的地理位置生成纬度和经度。为了从用户的输入(纬度和经度)中获得相同的信息,您需要将输入(可以是城市、州或完整地址)转换为坐标。我建议使用GoogleMapsAPI来转换用户输入

转换后,您可以将lat和long传递到
loadWeather()
。以下是用户输入(地址)转换的示例:


var geocoder=new google.maps.geocoder();
var address=jQuery('#address').val();
geocoder.geocode({'address':address},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
var latitude=results[0]。geometry.location.lat();
var longitude=results[0]。geometry.location.lng();
jQuery(“#坐标”).val(纬度+”,“+经度);
负荷天气(纬度+','+经度);
} 
}); 

您能发布地理定位数据的样本吗?例如
navigator
geolocation
属性,这是愚蠢的,但是我该怎么做呢?
console.log(navigator.geolocation)应将该数据记录到控制台。这样做的目的是查看数据的确切类型,以便您知道在(例如)输入字段(或字段组合)上向用户请求什么。当我使用console.log时似乎不起作用,但我尝试了document.write,得到了这个“[对象地理位置]”嗯,我尝试了谷歌地图API,但当我手动输入地址时,似乎什么都没有发生。。。不太确定发生了什么API返回坐标了吗?至少我会确保它正在这样做。我这里的提琴起作用了:注意
div
后面的值是如何表示坐标的。这些是为
loadWeather(arg1、arg2)
函数提供的值。为了测试,我的输入是“波特兰,俄勒冈州”,奇怪的是,没有任何东西显示为console.log,我也尝试了document.write(),但没有任何东西是解析的。我已经在你的小提琴上试过了,所以我不知道我这方面会出什么问题。您缺少了一些东西:Jquery库、
$
定义,而且您仍然缺少Yahoo Weather API(至少在小提琴上是这样),所以请添加它。此时您将得到一个错误,
$。SimpleLather
没有定义,因为它没有定义。无论是发布雅虎API的详细信息,还是简单地使用其中的内容,都应该有效。我已经更新了我的答案@HCL。fiddle现在可以工作了(获得了API端点)。让我知道这是否解决了您的问题,并标记答案,如果正确,干杯!