你从哪里来你是谁你到哪里去

Because this site has a truly global audience, you the reader could be anywhere in the world. Were I to ask you where on Earth you are, you might volunteer the name of a city, a state, or perhaps just the country you’re in. The answer might be ambiguous – is that Paris, France or Paris, Texas?

由于该站点具有真正的全球受众,因此您的读者可以在世界任何地方。 我是要问你你在地球上的什么地方,你可能会自愿提供一个城市,一个州的名称,或者只是你所在国家的名字。答案可能是模棱两可的–是法国巴黎还是德克萨斯州巴黎?

Modern geolocation applications use latitude and longitude to identify the location of people and of places to within a few meters, but it’s highly unlikely you’ll answered the question with those. Geolocation – that is, obtaining your location using GPS, Wi-Fi triangulation, or perhaps using your IP address – is all very well and good, but sometimes you just need to ask someone where they are.

现代地理定位应用程序使用纬度和经度来识别人和地方的位置(几米之内),但是用这些来回答这个问题的可能性很小。 地理位置很好-即使用GPS,Wi-Fi三角测量或也许使用IP地址来获取您的位置-很好,但是有时您只需要问问别人他们在哪里即可。

The challenge then is to do two things: work out what place you could be talking about, disambiguate if necessary, and then identify exactly where on Earth that is.

然后面临的挑战是要做两件事:弄清楚您可能在谈论什么地方,如有必要,请消除歧义,然后准确确定地球上的位置。

That’s what I’ll show you how to do in this article; by using a freely available web service, we’ll write a simple program to ask users where they are (and ask them to clarify if necessary) before identifying their responses in concrete terms.

这就是我将在本文中向您展示的方法。 通过使用免费提供的Web服务,我们将编写一个简单的程序来询问用户他们所在的位置(并在必要时要求他们澄清),然后再具体确定他们的回答。

地球上的位置标识符(WOEID) (Where On Earth Identifiers (WOEIDs))

I used the phrase “where on Earth” very deliberately as there is a database where places are identified not by name, but by what are called Where on Earth Identifiers (WOEIDs). The WOEID database has been put together and made freely available by Yahoo!, and is also used by Flickr, and for geographic-based trends by Twitter.

我故意使用“地球上的位置”一词,因为有一个数据库,其中不是通过名称来标识位置,而是通过所谓的“地球上的位置标识符” (WOEID)来标识 。 Yahoo!已将WOEID数据库放在一起并免费提供,Flickr也将其使用, Twitter还将其用于基于地理的趋势。

With every entry is a latitude and longitude for that place, as well as the names people have given them. To complicate this somewhat, there’s not always a one-to-one relationship. What I call Brussels you may call Bruselas, while residents would be more likely to call it Bruxelles. Then there are special cases, such as Peking/Beijing.

每输入一个条目,便表示该位置的经度和纬度,以及人们给他们的名字。 为了使这一点复杂化,并不总是存在一对一的关系。 我所说的布鲁塞尔,您可以称其为Bruselas,而居民更可能将其称为Bruxelles。 然后是特殊情况,例如北京/北京。

Placemaker入门 (Getting Started with Placemaker)

Yahoo!’s Placemaker web service is a “geo-extraction” service which uses the WOEID database and performs the sort of interpretation we need; parsing free-form text, identifying possible places, and returning information about them, such as the WOEID and that all-important latitude and longitude.

Yahoo!的Placemaker Web服务是一种“地理提取”服务,它使用WOEID数据库并执行我们所需的某种解释。 解析自由格式的文本,标识可能的位置,并返回有关它们的信息,例如WOEID以及最重要的纬度和经度。

Before you can use the Placemaker service, you’ll need an application ID, and in order to do that you’ll need a Yahoo! Id. If you use any of their services, you’ll already have one. Go to the Yahoo! website to generate an application ID where you’ll be asked to fill out the following form:

在使用Placemaker服务之前,您需要一个应用程序ID,然后,您需要Yahoo! ID。 如果您使用他们的任何服务,那么您已经拥有了。 转到Yahoo! 网站生成应用程序ID,并要求您填写以下表格:

Most of the form should be self-explanatory – under Authentication method, tick “Generic, No user authentication required”, as all you need to use the service is the application ID provided on the next page. Make a note of this application ID as you’ll need it in a moment (although you can always view your existing application IDs from the developers area).

大部分表格应该是不言自明的–在“ 身份验证方法 ”下,勾选“通用,无需用户身份验证”,因为使用该服务所需的全部是下一页提供的应用程序ID。 请暂时记下此应用程序ID(尽管您始终可以从开发人员区域查看现有的应用程序ID)。

提出要求 (Making the Request)

Placemaker is a REST-like web service with a single endpoint; to use the service you issue a POST request to the URL http://wherein.yahooapis.com/v1/document. There are a number of available response types: as well as the standard XML and JSON (and JSONP), you also have the option of obtaining the results in RSS format. Although I normally prefer JSON (a purely personal preference), in this example I’ll use XML.

Placemaker是具有单个端点的类似REST的Web服务。 要使用该服务,您需要向URL http://wherein.yahooapis.com/v1/document发出POST请求。 有许多可用的响应类型:除了标准的XML和JSON(以及JSONP),您还可以选择以RSS格式获取结果。 尽管我通常更喜欢JSON(纯粹是个人喜好),但在此示例中,我将使用XML。

Let’s look at the five request parameters you’re going to use.

让我们看一下您将要使用的五个请求参数。

The first, appid, is the application ID you obtained earlier. If you leave this out the request will be rejected. The second, documentContent, will be the string you’re searching for – the parameter named such because the service allows the parsing of entire documents, however I’m really just going to pass the name of a place provided by the user.

第一个appid是您之前获得的应用程序ID。 如果您忽略此要求,则该请求将被拒绝。 第二个documentContent将是您要搜索的字符串–名为诸如此类的参数,因为该服务允许解析整个文档,但是我实际上只是传递用户提供的位置的名称。

The documentType and outputType both refer to the response you’re expecting, though they could be a little confusing; the documentType (of the response) is plain text, while the corresponding outputType is XML. (The documentType would also be plain text if outputType were set to JSON.)

documentTypeoutputType都引用了您期望的响应,尽管可能会有些混乱。 (响应的) documentType是纯文本,而相应的outputType是XML。 (如果将outputType设置为JSON,则documentType也将是纯文本。)

Setting autoDisambiguate false overrides the default behavior of the service, which is to only return the most likely place. It’s this ambiguity we’re looking to resolve ourselves by asking the user rather than letting the service decide for us.

autoDisambiguate设置autoDisambiguate false会覆盖服务的默认行为,即仅返回最可能的位置。 我们希望通过询问用户而不是让服务为我们做出决定来解决这种模糊性。

Finally inputLanguage tells the service which language the document (that is to say, the user input) is in; this helps the service to know how to interpret place names. It’s not a required parameter (and predictably it defaults to US English), but it’s worth mentioning because it’s important to remember that a single WOEID can correspond to a number of names in a multitude of languages.

最后, inputLanguage告诉服务文档(即用户输入)所使用的语言; 这有助于服务部门了解如何解释地名。 它不是必需的参数(并且可以预测为默认为美国英语),但是值得一提,因为记住一个WOEID可以对应多种语言中的多个名称非常重要。

代码 (The Code)

Now let’s dig into the code then as we start by obtaining the text from the user via POST and make the request.

现在,让我们深入研究代码,然后开始通过POST从用户那里获取文本并发出请求。

<?php
$appId = YOUR_APP_ID_HERE;
$name = $_POST["name"];
$handle = curl_init("http://wherein.yahooapis.com/v1/document");
curl_setopt($handle, CURLOPT_POST, 1);
curl_setopt($handle, CURLOPT_POSTFIELDS, sprintf("documentContent=%s&documentType=%s&outputType=%s&autoDisambiguate=%s&appid=%s&inputLanguage=%s",
urlencode($name),
"text/plain",
"xml",
"false",
$appId,
"en-US"));
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($handle);
curl_close($handle);

I assume you’ve already implemented a form to capture the name and I’m also leaving out things like escaping input for the sake of brevity. The code simply uses cURL to set the endpoint to http://wherein.yahooapis.com/v1/document, the POST variables to those discussed earlier, and return the web service’s response as a string. Don’t forget to URL encode the name before passing it via POST though.

我假设您已经实现了一个用于捕获名称的表单,并且为了简洁起见,我还将省略诸如转义输入之类的内容。 该代码仅使用cURL将端点设置为http://wherein.yahooapis.com/v1/document ,将POST变量设置为前面讨论的变量,然后将Web服务的响应作为字符串返回。 不过,在通过POST传递名称之前,请不要忘记对名称进行URL编码。

Before we start manipulating the response, let’s take a look at what we’re expecting (you might want to echo it out to the screen at this point). Here’s a simplified example of an XML response:

在开始处理响应之前,让我们看一下期望的结果(此时您可能希望将其回显到屏幕上)。 这是XML响应的简化示例:

<?xml version="1.0" encoding="UTF-8"?>
<contentlocation />
<processingTime>0.009052</processingTime>
<version> build 110725</version>
<documentLength>6</documentLength>
<document>
<administrativeScope>..</administrativeScope>
<geographicScope>..</geographicScope>
<localscopes>..</localscopes>
<extents>..</extents>
<placeDetail>..</placeDetail>
<placeDetail>..</placeDetail>
<placeDetail>..</placeDetail>
<placeDetail>..</placeDetail>
...
</document>
</xml>

When you tell the service not to disambiguate the results of the search, the response contains two aspects: the scope of the place that best describes the document, and the places the document could refer to. In other words, the service provides a best guess and gives you further information about that place within the administrativeScope, geographicScope, localscopes, and extents elements.

当您告诉服务不要对搜索结果进行歧义时,响应包含两个方面:最能描述文档的位置范围和文档可以引用的位置。 换句话说,该服务提供了一个最好的猜测,并为您提供有关范围内的地方更多信息administrativeScopegeographicScopelocalscopesextents元素。

If you queried “Paris” for example, then it would give you geographic information about the region of Île-de-France, and the country France – but these scopes do not relate to Paris, Texas, or indeed anywhere else that shares that name. It’s those places we’re interested in because we want the user to clarify. These alternatives can be found in the placeDetails elements, as shown below:

例如,如果您查询“巴黎”,它将为您提供法兰西岛地区和法国国家的地理信息-但这些范围与巴黎,德克萨斯州或其他任何具有该名称的地区无关。 这些是我们感兴趣的地方,因为我们希望用户澄清一下。 这些替代方法可以在placeDetails元素中找到,如下所示:

<placeDetails>
<placeId>3</placeId>
<place>
<woeId>615702</woeId>
<type>Town</type>
<name><![CDATA[Paris, Ile-de-France, FR]]></name>
<centroid>
<latitude>48.8569</latitude>
<longitude>2.34121</longitude>
</centroid>
</place>
<placeReferenceIds>1</placeReferenceIds>
<matchType>0</matchType>
<weight>1</weight>
<confidence>6</confidence>
</placeDetails>

Let’s look at the contents of the placeDetails element. The placeId represents a given place within the context of the response, and can be used to disambiguate between multiple matches with the same WOEID, although that – along with the placeReferenceIds – is a little beyond the scope of this article.

让我们看一下placeDetails元素的内容。 placeId表示响应上下文中的给定位置,可用于在具有相同WOEID的多个匹配之间进行歧义消除,尽管该内容(与placeReferenceIds一起)超出了本文的范围。

The weight and confidence tell us more about the match. The weight refers to the relative weight of the place in the document as a whole. This is largely irrelevant in the context of our use of the service, as we’re only sending a fragment of text referring to a single place (at least that’s what we’re asking the user for). Rather, the is used to rank places when a number are mentioned, determined by such factors as their position in the document and the number of mentions. The confidence value (on a scale of 1-10 with 10 being the most certain) is the confidence that this place is actually referred to in the document – but also how confident it is that the place is referred to rather than another place. This last clause is important, because it takes into account the relative likelihood that if 100 people said “London”, the chances are that the majority would mean the capital of England, rather than one of the various, smaller Londons in the United States. In other words, we can sort the results in descending order, based on the value of the confidence.

weightconfidence告诉我们有关比赛的更多信息。 weight是指整个文档中该位置的相对权重。 在我们使用服务的上下文中,这基本上是无关紧要的,因为我们仅发送指向单个位置的文本片段(至少这是我们要求用户提供的内容)。 而是,当提及数字时,使用来对位置进行排名,具体取决于诸如在文档中的位置和提及次数之类的因素。 confidence值(在1-10的范围内,最确定的是10)是在文档中实际引用此位置的置信度-而是该位置而不是另一个位置的置信度。 最后一条很重要,因为它考虑到相对的可能性,即如果有100个人说“伦敦”,则多数人有可能代表英格兰的首都,而不是美国各种规模较小的伦敦之一。 换句话说,我们可以基于置信度的值对结果进行降序排序。

Finally, and most importantly, there’s the actual place. The place element has a type; this will usually be Town, but may also be State, Suburb or of course Country. The centroid element contains the latitude and longitude of the geometric center, name should be self explanatory, and woeId is the identifier I’ve been talking about. Because this is a widely accepted identifier, there’s nothing to stop us using this to obtain information about the place elsewhere.

最后,最重要的是,这里有实际的位置。 place元素具有类型; 这通常是城镇,但也可以是州,郊区或国家。 重心元素包含几何中心的纬度和经度,名称应woeId说明,而woeId是我一直在谈论的标识符。 因为这是一个广泛接受的标识符,所以没有什么可以阻止我们使用它来获取有关其他地方的信息。

解释响应 (Interpreting the Response)

Now that we’re familiar with the structure of the response, let’s try and use it. Here’s the code which parses the response:

现在我们已经熟悉了响应的结构,让我们尝试使用它。 这是解析响应的代码:

<?php
$places = array();
$xml = simplexml_load_string($response);
foreach ($xml->document->placeDetails as $xmlPlaceDetail) {
$xmlPlace = $xmlPlaceDetail->place;
$xmlCentroid = $xmlPlace->centroid;
$place = new stdClass();
$place->id = (int)$xmlPlaceDetail->placeId;
$place->woeid = (int)$xmlPlace->woeId;
$place->name = (string)$xmlPlace->name;
$place->lat = (float)$xmlCentroid->latitude;
$place->lng = (float)$xmlCentroid->longitude;
$place->confidence = (int)$xmlPlaceDetail->confidence;
$places[$place->id] = $place;
}

The code above uses SimpleXML to parse the response, iterating through the placeDetail elements. Each time it encounters one, it takes the place and centroid elements to extract the information we’re after, all of which is assigned to a new object representing the place in question.

上面的代码使用SimpleXML来解析响应 ,并遍历placeDetail元素。 每次遇到一个place ,它都会使用placecentroid元素来提取我们所需要的信息,所有这些信息都分配给代表该地点的新对象。

We now have a list of possibilities; however the service also gives us that measure of how likely it thinks it is that the user will mean one place over another, so let’s sort that array using a simple callback:

现在,我们列出了所有可能性。 但是该服务还为我们提供了一种衡量方式,即它认为用户将一个地方放在另一个地方的可能性是多少,因此让我们使用一个简单的回调对该数组进行排序:

<?php
function confidenceSort($a, $b) {
if ($a->confidence == $b->confidence) {
return 0;
}
return ($a->confidence > $b->confidence) ? -1 : 1;
}
uasort($places, "confidenceSort");

We should be left with an array of places, sorted in descending order of confidence.

我们应该留有一系列位置,以信心的降序排列。

接下来是什么? (What Next?)

I’ve shown you how to ask the user for a place name, retrieved information about possible options and sorted them according to how likely they mean one location over the other, and a simple demo is available which you can find on GitHub. What you do with this now is up to you – you could ask the user for clarification, or try to reduce the number of alternatives using biasing.

我已经向您展示了如何向用户询问一个地名,如何检索有关可能选项的信息,并根据它们在一个位置相对于另一个位置的可能性进行排序,并提供了一个简单的演示 ,您可以在GitHub上找到它。 您现在可以执行此操作,具体取决于您–您可以要求用户进行说明,或尝试使用偏差减少替代方法的数量。

Some queries can produce dozens of results, so it’s probably worth trying to influence the search by biasing it towards a given location based on what you know about the user – you can pass the WOEID of a place using the focusWoeId parameter for just that.

某些查询可以产生数十个结果,因此可能值得尝试通过根据对用户的了解将搜索偏向给定位置来影响搜索–您可以使用focusWoeId参数来传递地点的WOEID。

You may wish to read more about WOEIDs, or dig a little deeper by looking at Yahoo!’s Geoplanet service – not just for towns and cities, but landmarks as well. You may also wish to process whole documents – imagine for a moment the possibilities for geotagging blog content, or enhancing a text-based search to find documents not based on text content, but on the location of the user. Have a look through the documentation, play around, and if you get stuck there’s a Placemaker forum on Yahoo! where you can find further information and ask for help.

您可能希望阅读有关WOEID的更多信息 ,或者通过查看Yahoo!的Geoplanet服务 (不仅仅针对城镇和地标,还包括地标)来进行更深入的研究。 您可能还希望处理整个文档-想象一下对地理博客内容进行地理标记或增强基于文本的搜索以查找不基于文本内容而是基于用户位置的文档的可能性。 浏览文档,四处逛逛,如果遇到麻烦,可以在Yahoo!上找到一个Placemaker论坛。 您可以在其中找到更多信息并寻求帮助。

If you do build anything interesting using this, I’d love to hear about it in the comments!

如果您确实使用此工具构建了一些有趣的东西,我很乐意在评论中听到!

翻译自: https://www.sitepoint.com/where-on-earth-are-you/

你从哪里来你是谁你到哪里去

你从哪里来你是谁你到哪里去_你到底在哪里?相关推荐

  1. 从言行合一到知行合一

    本篇记录突然的随想,偶尔停留 2022-01-17 rest不只是一个简单的api约束分享一本rest小手册,如何把东西变得 RESTful! 2021-12-06 规划 未来编程.   未来需要什么 ...

  2. golang通过RSA算法生成token,go从配置文件中注入密钥文件,go从文件中读取密钥文件,go RSA算法下token生成与解析;go java token共用

    RSA算法 token生成与解析 本文演示两种方式,一种是把密钥文件放在配置文件中,一种是把密钥文件本身放入项目或者容器中. 下面两种的区别在于私钥公钥的初始化, init方法,需要哪种取哪种. 通过 ...

  3. springboot实现SSE服务端主动向客户端推送数据,java服务端向客户端推送数据,kotlin模拟客户端向服务端推送数据

    SSE服务端推送 服务器向浏览器推送信息,除了 WebSocket,还有一种方法:Server-Sent Events(以下简称 SSE).本文介绍它的用法. 在很多业务场景中,会涉及到服务端向客户端 ...

  4. /var/lib/docker/overlay2/xxxxx no such file or directory docker文件删除引发的问题

    记一次误删引发的服务雪崩 K8s node节点磁盘报警,报警后我找到服务中占用磁盘最多的地方,在overlay2目录下,对下面的文件进行了删除   删除后,有状态服务先出现了问题,服务无法启动.停止. ...

  5. mysql建立联合索引,mysql建立唯一键,mysql如何解决重复记录联合索引

    在项目中,常常要用到联合唯一   在一些配置表中,一些列的组合成为一条记录.   比如,在游戏中,游戏的分区和用户id会形成一条记录.(比如,一个qq用户可以在艾欧尼亚.德玛西亚创建两个账号) 添加联 ...

  6. 本地打包Docker镜像上传至阿里云远程仓库(一站式脚本)

    打包镜像上传至远程仓库: 1. 本地项目为 mytest-project 2. 仓库为阿里云镜像仓库 registry.cn-beijing.aliyuncs.com/test/mytest-proj ...

  7. nginx配置http、https访问,nginx指定ssl证书,阿里云腾讯云华为云设置nginx https安全访问

    nginx配置http.https访问 要设置https访问需要从对应的云厂商申请证书,并下载Nginx证书到服务器. 我这里从阿里云申请了免费的域名证书,然后将证书放置在服务器的/etc/ssl/. ...

  8. 系统架构升级要不要上微服务?历“久”弥新微服务——你真的需要升级微服务架构吗

    在 <微服务架构设计模式> 一书中,作者总结了关于微服务的一些"重点",原文如下: 中国企业和开发者对微服务架构的热情让我印象深刻.但如同我给所有客户的忠告一样,我想对 ...

  9. Docker容器的备份与恢复,Docker镜像的备份与恢复

    1. 备份容器 首先,为了备份Docker中的容器,我们会想看看我们想要备份的容器列表.要达成该目的,我们需要在我们运行着Docker引擎,并已创建了容器的Linux机器中运行 docker ps 命 ...

  10. Gradle错误提示:Java home supplied via ‘xxx.xxx.xxx‘ is invalid

    Gradle错误提示:Java home supplied via 'org.gradle.java.home' is invalid 描述:在使用idea采用gradle进行依赖的管理功能,当想切换 ...

最新文章

  1. 路由器无法访问目标网络_初设路由器,无法访问管理页面的解决办法_网络设备技术应用...
  2. window.location操作url对象
  3. android控件属性文档,1.Android控件属性收集
  4. 浅谈JAVA中如何利用socket进行网络编程(二)
  5. python多进程与多线程实验
  6. Shadow DOM及自定义标签
  7. 屠榜CV还不是这篇论文的终极目标,它更大的目标其实是……
  8. a*算法matlab代码_MATLAB数学建模算法的代码模板大全
  9. 两种消息模型: 点对点(queue)发布/订阅(topic)
  10. 安卓app开发-02-安卓app快速开发
  11. 深入分析java线程池的理解
  12. python fetchall函数_关于python中的查询数据库内容中用到的fetchone()函数和fetchall()函数(转)还有fetchmany()...
  13. BitTorrent协议规范(BitTorrent Protocol Specification)之Peer Wire协议(Peer Wire Protocol)-第四部分...
  14. Linux查看占用内存的进程
  15. android 后台运行管理,Android 后台运行白名单实现保活
  16. 2021 Summary
  17. VS2017 LINK : fatal error LNK1104: cannot open file ‘atls.lib‘错误解决方案
  18. 100%BIM学员的疑惑:不会CAD可以学Revit吗?
  19. 一、Tensor基础
  20. MySQL补充——索引,流程控制,数据备份,python操作mysql,SQLAlchemy

热门文章

  1. 人力资源如何进行数据分析
  2. Android朋友圈怎么换行,2020年发朋友圈打字怎么换行
  3. 斜面怎么计算机械效率,初中物理斜面的机械效率学习方法
  4. MODIFY STRUCTURE和ALTER TABLE的区别?
  5. PHPstrom + xdebug + postman断点调试代码 php谷歌浏览器代码断点调试
  6. 纯css更改图片颜色的技巧
  7. 国庆旅游——独山、箬寮原始森林
  8. 罗克韦尔PLC编程软件ControlLogix平台
  9. 《Python编程:从入门到实践》中的小练习(1)
  10. 虚拟机安装systemc