【问题标题】:Cityname to geographical coordinates PHP script?Cityname到地理坐标PHP脚本?
【发布时间】:2011-03-14 12:17:49
【问题描述】:

我需要开发一个 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 可以做到这一点吗?如何将数据从 cityXXXX 导入 MySQL?,对 php 脚本有什么建议吗?...谢谢!

【问题讨论】:

    标签: php mysql geolocation


    【解决方案1】:

    我使用 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];
    

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

    【讨论】:

      【解决方案2】:

      请参阅http://www.maxmind.com/app/geoip_resources 以及底部的“MySQL”部分。他们会帮忙的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-07-04
        • 2017-06-30
        • 1970-01-01
        • 1970-01-01
        • 2020-08-24
        • 2013-09-09
        • 2021-04-21
        相关资源
        最近更新 更多