2010-07-11 4 views
3

나는 도시 이름을 물어 도시의 지리적 좌표와 함께 간단한 반향을 반환하는 PHP 스크립트를 개발해야합니다.지명 PHP 스크립트에 대한 도시 이름?

입력 데이터 : CityName이 OR CityName이, 나라

난 당신이 정보를 포함하는 데이터베이스를 다운로드 할 수 있었다 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

그래서 그 좋은 생각이 데이터베이스를 사용하는가? 온라인이 AP 어떻게해야합니까? citiesXXXX에서 MySQL로 데이터를 가져올 수 있습니까?, PHP 스크립트에 대한 제안 ... ... 감사합니다! 쉽게 구문 분석하고 데이터를 저장할 수 있도록 http://code.google.com/apis/maps/index.html

그것은 XML 또는 JSON의 데이터를 반환 할 수 있습니다

답변

8

나는 위치에 대한 정보를 얻기 위해 Google지도를 사용합니다. 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 같은 기능을 사용할 수 있습니다 여기에

워싱턴 DC에 대한 예제 링크입니다. 당신이 원하는 경우

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

나는 그 희망 그냥 좌표를 에코하려면 태그 간단한 사용이 코드를 지원하는로 당신이 찾고있는 것.

관련 문제