使用 GeoNames Java API 检索给定维基百科文章标题的坐标

发布于 2025-01-02 21:28:40 字数 1260 浏览 3 评论 0原文

假设我有维基百科文章的标题,例如费城,并且我想要那个地方的坐标。我不想从维基百科获取此内容,因为看起来我必须检索整篇文章。我不想只是松散地查询“像费城”这样的地方的 GeoNames,因为这会产生很多结果,而且我知道我想要特定的维基百科文章 [[费城]] 的坐标。

似乎 GeoNames API 允许您使用类 WikipediaArticle 来执行此操作 及其方法,如 setTitle() 和 getLatitude()。但我一直在摆弄该类和 WebService 并且无法以这种方式检索坐标。我不会用我尝试过的所有组合让你感到厌烦,但我找不到如何做到这一点的示例。比如:

WebService.setUserName("whateveritis");
List<WikipediaArticle> wikiResults = WebService.wikipediaSearchForTitle(
                            "Philadelphia","en");
for (WikipediaArticle r : wikiResults) {
    System.out.println(r.getLatitude());
}

这给出了很多错误,我可以分享,但让我觉得我只是使用错误。或者也许:

WikipediaArticle myPlace = new WikipediaArticle();
myPlace.setTitle("Philadelphia");
Double lat = myPlace.getLatitude();

这会返回 0.0。有熟悉 API 的人可以告诉我哪里出错了吗?我确实认为它应该做这样的事情,因为网络版本会这样做: http://api.geonames.org/wikipediaSearch?title=Philadelphia&maxRows=1&username=demo

Say I have the title of a Wikipedia article, e.g. Philadelphia, and I want the coordinates of that place. I don't want to get this from Wikipedia because it looks like I'd have to retrieve the whole article. And I don't want to just loosely query GeoNames for places "like philadelphia" because that will have lots of results, and I know I want the specific Wikipedia article [[Philadelphia]]'s coordinates.

It seems like the GeoNames API allows you to do this with the class WikipediaArticle and its methods like setTitle() and getLatitude(). But I've been fiddling around with that class and WebService and haven't been able to retrieve the coordinates that way. I won't bore you with all the combinations I've tried, but I can't find an example of how to do it. Something like:

WebService.setUserName("whateveritis");
List<WikipediaArticle> wikiResults = WebService.wikipediaSearchForTitle(
                            "Philadelphia","en");
for (WikipediaArticle r : wikiResults) {
    System.out.println(r.getLatitude());
}

That gives a lot of errors, that I can share, but make me think I'm just using it wrong. Or maybe:

WikipediaArticle myPlace = new WikipediaArticle();
myPlace.setTitle("Philadelphia");
Double lat = myPlace.getLatitude();

This returns 0.0. Is anyone familiar with API and can tell me where I'm going wrong? I do think it's meant to do stuff like this because the web version does it: http://api.geonames.org/wikipediaSearch?title=Philadelphia&maxRows=1&username=demo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞花火° 2025-01-09 21:28:40

第一种方法,使用 WebService.wikipediaSearchForTitle,是执行此操作的方法,但是您必须在类路径中正确设置 jdom——我以为我已经设置了,但没有。但请注意,该函数返回标题中包含“Philadelphia”的任何文章,而不仅仅是 Phildelphia。所以这个功能,即使在工作时,也没有完全满足我的需要。

The first way, using WebService.wikipediaSearchForTitle, is the way to do this, but you have to have jdom set up properly in the classpath--which I thought I had, but didn't. However, note that the function returns any article with "Philadelphia" in the title, not just exactly Phildelphia. So the function, even when working, didn't do quite what I needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文