javascript 从城市名称获取WOEID?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12434591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get WOEID from city name?
提问by u283863
I was using Google Weather API to fetch weather info, but apparently Google had stopped its service. And I am trying to switch to Yahoo Weather API now.
我正在使用 Google Weather API 来获取天气信息,但显然 Google 已停止其服务。我现在正在尝试切换到 Yahoo Weather API。
var WOEID = 2502265; //random WOEID
$.ajax({
url: "http://weather.yahooapis.com/forecastjson?w=" + WOEID + "&u=c",
dataType: 'json',
success: function(data) {
console.log(data);
}
});
However, is there a way that I can get the WOEID by JavaScript only? Because back then I can just do
但是,有没有办法只能通过 JavaScript 获取 WOEID?因为那时我只能做
http://www.google.com/ig/api?hl=en&weather=NYC
and that's it.
就是这样。
It says on the Yahoo weather API page,
To find your WOEID, browse or search for your city from the Weather home page. The WOEID is in the URL for the forecast page for that city. You can also get the WOEID by entering your zip code on the home page.
要查找您的 WOEID,请从“天气”主页浏览或搜索您的城市。WOEID 位于该城市的预测页面的 URL 中。您还可以通过在主页上输入您的邮政编码来获取 WOEID。
But I want to get it by JavaScript, not manually go to weather.yahoo.com and find out the WOEID.
但我想通过 JavaScript 获取它,而不是手动去weather.yahoo.com 并找出WOEID。
Don't care about the Cross-Origin Policy because I am using it in an Chrome extension and it does not apply.
不关心跨域策略,因为我在 Chrome 扩展程序中使用它并且它不适用。
回答by aravind.udayashankara
Okay I got to know from your comments what exactly you want
好的,我从你的评论中知道你到底想要什么
You have a place name and you want to get the WOEID of that place name using javascript ajax calls
您有一个地名,并且想使用 javascript ajax 调用获取该地名的 WOEID
The url to get that is not defined any where you have to use GeoPlanet service to resolve a place to a WOEID
未定义任何必须使用 GeoPlanet 服务将地点解析为 WOEID 的 url
http://where.yahooapis.com/v1/places.q('Place name')?appid=[yourappidhere]
OR you have to use Direct YQL some what like this ( use percent encoding in the url for your city name ) appropriately and try doing an ajax call to this
或者你必须适当地使用 Direct YQL 一些类似的东西(在你的城市名称的 url 中使用百分比编码)并尝试对此进行 ajax 调用
http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20geo.places%20where%20text%3D%22Place%20name%22&format=xml
回答by Rafay
you can get it from yahoo too http://developer.yahoo.com/geo/geoplanet/guide/concepts.html
你也可以从雅虎获得它http://developer.yahoo.com/geo/geoplanet/guide/concepts.html
API Reference
API 参考
回答by Patrick
DECEMBER 2018 UPDATE:
2018 年 12 月更新:
Definitely use the Direct YQL technique mentioned above by @aravind.udayashankara. I messed around with the yboss api for awhile only to see it has been discontinued (https://developer.yahoo.com/boss/search/) even though Yahoo still has plenty of documentation on it online.
绝对使用@aravind.udayashankara 上面提到的直接 YQL 技术。我把 yboss api 弄乱了一段时间,结果发现它已经停产了(https://developer.yahoo.com/boss/search/),尽管 Yahoo 仍然有大量的在线文档。
Try the following instead (it runs off the page but there is code within the URL).
请改为尝试以下操作(它在页面之外运行,但 URL 中有代码)。
yourLocation = "location" (zip, city name, etc.)
urlQuery = "https://query.yahooapis.com/v1/public/yql?q=select+*+from+geo.places+where+text%3D%22" + yourLocation + "%22&format=json"
回答by AAli
To get the Woeid by city name
按城市名称获取 Woeid
using (WebClient wc = new WebClient())
{
string results = wc.DownloadString("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20geo.places%20where%20text%3D%22" + CityName + "%22&format=xml");
}