2010-07-11 74 views
3

我需要開發一個php腳本,請求一個城市名稱並返回一個簡單的回聲與城市的地理座標。城市名稱到地理座標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

所以,它是一個好主意,用這個數據的基礎上?有一個在線美聯社我要做到這一點?,我怎樣才能將數據從citiesXXXX導入到MySQL?,任何關於php腳本的建議?...謝謝!

回答

8

我使用谷歌地圖來獲取有關位置的信息: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的或DOM文檔的功能來分析數據。如果你只是想呼應的座標爲標籤簡單的使用支持該代碼:

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

我希望這是你在尋找什麼。

相關問題