简体   繁体   English

从xml(地图api)获取数据(邮政编码)并创建url

[英]Get data (zip code) from xml (maps api) and create url

I have created a android app that gets the gpslocation and creates a url: https://maps.googleapis.com/maps/api/geocode/xml?latlng=00.00,00.00&sensor=true 我创建了一个Android应用,该应用获取gpslocation并创建了一个网址: https ://maps.googleapis.com/maps/api/geocode/xml?latlng=00.00,00.00&sensor=true

Example: https://maps.googleapis.com/maps/api/geocode/xml?latlng=42.120636,-72.568731&sensor=true 示例: https//maps.googleapis.com/maps/api/geocode/xml?latlng = 42.120636,-72.568731&sensor = true

This returns (only a part): 这将返回(仅一部分):

<GeocodeResponse>
    <status>OK</status>
    <result>
        <type>street_address</type>
        <formatted_address>
            17 Dorchester Street, Springfield, Massachusetts 01109, United States
        </formatted_address>
        <address_component>
            <long_name>17</long_name>
            <short_name>17</short_name>
            <type> ...

And i'm interested in this part: 17 Dorchester Street, Springfield, Massachusetts 01109, United States . 我对这部分感兴趣: 17 Dorchester Street, Springfield, Massachusetts 01109, United States

And I would like to create a new url that contains the zip number code "01109" like http://mysite.com?process=01109 and open this site. 并且我想创建一个包含邮政编码“ 01109”的新网址(例如http://mysite.com?process=01109),然后打开此网站。

Can anyone help me! 谁能帮我!

So, the XML you link to actually has the postal code in it. 因此,您链接到的XML实际上包含邮政编码。 It is an address_component with a child type that has the value postal_code . 它是一个子地址为address_component的子type ,其值为postal_code The easiest way to get there, IMHO, is XPath: 到达目的地最简单的方法是XPath:

$d = <<<HER
<GeocodeResponse>
  <!-- yada... --> 
  <result>
    <!-- yada --> 
    <address_component>
        <long_name>01109</long_name>
        <short_name>01109</short_name>
        <type>postal_code</type>
    </address_component>
    <!-- yada --> 
   </result>
   <!-- yoda --> 
</GeocodeResponse>
HER;

$doc = new DomDocument();
$doc->loadXML($d);
$path = new DomXPath($doc);
/*
the query translates->
   // = all nodes
   address_component = which have the type of 'address_component'
   type = the children of the address_component with the type 'type'   
   [text() = "postal_code"] = but only the type's with the value 'postal_code'
   /preceding-sibling = all nodes before this one in the same parent
   ::*[1] = the one most adjacent of the sibling
*/

$p = $path->query(
  '//address_component/type[text() = "postal_code"]/preceding-sibling::*[1]');
$newUrl = 'http://mysite.com?process='.$p->item(0)->nodeValue;
print($newUrl);

To obtain specific data from the googleapis geocode response you not only need to locate an element by it's name but also by it's content. 要从googleapis地理编码响应中获取特定数据,您不仅需要通过元素的名称来定位元素,还需要通过其内容来定位元素。

This can be easily done with Xpath. 使用Xpath可以很容易地做到这一点。 Luckily the SimpleXML extension in PHP has good support for reading such common formatted XML documents. 幸运的是,PHP中的SimpleXML扩展为读取此类常见的格式化XML文档提供了良好的支持。

First for the Xpath: 首先是Xpath:

<GeocodeResponse>
    <result>
            <type>street_address</type>

The <result> element is the element that has a child <type> which node-value is street_address . <result>元素是具有子<type>节点值为street_address的元素。 In Xpath this is expressed as: 在Xpath中,它表示为:

/*/result/type[. = "street_address"]/..

And it works similar for the ZIP-code. 对于邮政编码,它的工作原理类似。 It is the <address_component> child of the previous <result> node that has a child <type> which node-value is postal_code and of that element you want: 它是先前的<result>节点的<address_component>子节点,该子节点具有一个<type>子节点,该子节点的值是postal_code以及所需的该元素:

address_component/type[. = "postal_code"]/..

So far for the xpaths. 到目前为止,对于xpaths。 All you need to get this to run is to load the XML document, execute the paths and read out the values you're interested in: 运行此命令所需要做的就是加载XML文档,执行路径并读出您感兴趣的值:

$xml = simplexml_load_file($xmlFile);

list($result) = $xml->xpath('/*/result/type[. = "street_address"]/..');
list($postal_code) = $result->xpath('address_component/type[. = "postal_code"]/..');

echo $result->formatted_address, "\nZIP: ", $postal_code->long_name, "\n";

With the XML documented linked in your question this creates the following output: 使用您问题中链接的XML文档,将创建以下输出:

17 Dorchester Street, Springfield, MA 01109, USA
ZIP: 01109

I hope this is helpful. 我希望这是有帮助的。

You can access the postal_code short_name using simplexml and xpath. 您可以使用simplexml和xpath访问postal_code short_name。 Here's a working example: 这是一个工作示例:

$location = simplexml_load_file('https://maps.googleapis.com/maps/api/geocode/xml?latlng=42.120636,-72.568731&sensor=true');

$postal_code_search = 
$location->xpath('//result[type="street_address"]/address_component[type="postal_code"]/short_name');
$postal_code = (string) $postal_code_search[0];

Notice that you must cast the values to string explicitly, as simplexml will return array or object to variables versus a string when printed (which can be confusing when testing). 请注意,您必须将值显式转换为字符串,因为simplexml将在打印时将数组或对象返回到变量而不是字符串(测试时可能会引起混淆)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM