gpt4 book ai didi

php - 城市名称到地理坐标的PHP脚本?

转载 作者:可可西里 更新时间:2023-11-01 07:50:33 26 4
gpt4 key购买 nike

我需要开发一个 php 脚本,它要求输入城市名称并返回带有城市地理坐标的简单回显。

输入数据:城市名或城市名,国家

我知道有一个名为 GeoNames 的免费数据库您是否可以下载包含此信息的数据库,但我真的不知道如何将此数据库导出到我的 MySQL 服务器,有 3 个文件可能是我认为需要的,但我不知道我需要选择什么:

cities1000.zip                             11-Jul-2010 01:13  3.4M  
cities15000.zip 11-Jul-2010 01:13 1.1M
cities5000.zip 11-Jul-2010 01:13 1.8M

see were this files are

那么,使用这个数据库是个好主意吗?有一个在线 API 可以做到这一点吗?我如何将数据从 citiesXXXX 导入 MySQL?对 php 脚本有什么建议吗?...谢谢!

最佳答案

我使用 Google map 获取有关位置的任何信息:http://code.google.com/apis/maps/index.html

它可以返回 XML 或 JSON 格式的数据,以便您轻松解析和保存数据。

这是华盛顿特区的示例链接:http://maps.google.com/maps/geo?output=xml&oe=utf8&sensor=false&hl=en&q=washington%20dc

它将返回此 XML 数据:

    <?xml version="1.0" encoding="UTF-8" ?>
<kml xmlns="http://earth.google.com/kml/2.0">
<Response>
<name>washington dc</name>
<Status>
<code>200</code>
<request>geocode</request>
</Status>
<Placemark id="p1">
<address>Washington, DC, USA</address>
<AddressDetails Accuracy="4" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0">
<Country>
<CountryNameCode>US</CountryNameCode>
<CountryName>USA</CountryName>
<AdministrativeArea>
<AdministrativeAreaName>DC</AdministrativeAreaName>
<SubAdministrativeArea>
<SubAdministrativeAreaName>District of Columbia</SubAdministrativeAreaName>
<Locality>
<LocalityName>Washington</LocalityName>
</Locality>
</SubAdministrativeArea>
</AdministrativeArea>
</Country>
</AddressDetails>
<ExtendedData>
<LatLonBox north="38.9645516" south="38.8256040" east="-76.9083064" west="-77.1644252" />
</ExtendedData>
<Point>
<coordinates>-77.0363658,38.8951118,0</coordinates>
</Point>
</Placemark>
</Response>
</kml>

然后您可以使用类似 SimpleXML 或 DOMDocument 的函数来解析数据。如果您只想回显标签中支持的坐标,请使用以下代码:

$xml = simplexml_load_string(file_get_contents('http://maps.google.com/maps/geo?output=json&oe=utf8&sensor=false&hl=de&q='.urlencode($address)));
echo (string)$xml->Placemark->Point->coordinates;

如果您想要单个坐标,请使用标签中的数据,例如:

$coordinates = explode(',', $xml->Placemark->Point->coordinates);
echo $coordinates[0];
echo $coordinates[1];

我希望这就是您正在寻找的。

关于php - 城市名称到地理坐标的PHP脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3223659/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com