City name for geographic coordinates PHP script?

I need to develop php scripts that query the name of the city and return a simple echo with the geographic coordinates of the city.

Input data: city name or city, country

I know that there is a free database called GeoNames , you can download a database that contains this information, but I really don’t know how to export this database to my MySQL server, there are 3 files that I may need, but I I don’t know what I need to choose:

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 if these files were

So, is it a good idea to use this database ?, is there an online API for this ?, how can I import data from cities XXXX to MySQL ?, any suggestions for php script? ... Thanks!

+3
source share
2 answers

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

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

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

It will return the 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>

Then you can use the SimpleXML or DOMDocument function to analyze the data. If you just want the echo coordinates that are supported in the tag, just 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 single coordinates to use the data in the tag, for example:

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

, , .

+8

Source: https://habr.com/ru/post/1754111/


All Articles