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

发布于 2024-09-08 16:18:25 字数 686 浏览 2 评论 0原文

我需要开发一个 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  

看看这些文件是

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

i need to develop a php script who ask for a city name and return a simple echo with the geographical coordinates of the city.

Input data: Cityname OR Cityname, Country

I know there is a free database called GeoNames were you can download a database who contains this information, but i really dont know how to export this database to my MySQL server, there 3 files who can be the think i need but i dont know what i need to pick:

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

So, its a good idea to use this data base?, there is an online API to do this?, how can i import the data from citiesXXXX to MySQL?, any suggestion for the php script?... Thanks!

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

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

发布评论

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

评论(2

红颜悴 2024-09-15 16:18:25

我使用 Google 地图获取有关某个位置的任何信息: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];

我希望这就是您正在搜索的内容。

I use Google Maps to get any information about a location: http://code.google.com/apis/maps/index.html

It can return the data in XML or JSON so you can easily parse and save the data.

Here is an example link for Washington DC: http://maps.google.com/maps/geo?output=xml&oe=utf8&sensor=false&hl=en&q=washington%20dc

It will return this XML data:

    <?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>

You can then use a function like SimpleXML or DOMDocument to parse the data. If you just want to echo the coordinates as supported in the tag simple use this code:

$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;

If you want the single coordinates use the data in the tag e.g.:

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

I hope that is what you are searching for.

冰雪梦之恋 2024-09-15 16:18:25

请参阅 http://www.maxmind.com/app/geoip_resources 并在底部“ MySQL”部分。他们会提供帮助。

See http://www.maxmind.com/app/geoip_resources and there in the bottom "MySQL" sections. They will help.

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