2012-05-02 2 views
0

나는 구글 지오 코딩 API를 사용하고 있습니다를 사용하여 XML 결과에서 모든 필드의 압축을 풉니 다. 아래의 코드는 어레이 $arrGeocodeSales 통해 반복하고, 각 행에 대해 그 다음 표에 표시되는 $lat$long를 검색한다.구체적으로 XML 출력 형식에 의존, 구글 지오 코딩 API

이 코드는 정상적으로 작동하지만 반환 할 결과가 $xml 인 결과에서 더 많은 필드를 추출 할 수 있도록 확장하고 싶습니다. 예를 들어 $ xml 결과에서 추출하려는 추가 필드 중 일부는 formatted_addresspostal_code입니다. 아래의 코드를 수정하여 추가 필드를 추출하려면 어떻게해야합니까?

당신은 여기 $의 XML 결과의 예를 볼 수 있습니다 여기에

http://maps.googleapis.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true

수정해야하는 코드입니다. 나는 반환 된 SimpleXMLElement을 사용하는 방법을 이해하지 못한다고 생각합니다. 도와 주셔서 감사합니다.

<table border="1" style="margin-bottom:0px;"> 
<tr> 
    <th>ID</th> 
    <th>Roll</th> 
    <th>Address</th> 
    <th>Lat</th> 
    <th>Long</th> 
    <th>MapSaleNumber</th> 
    </tr> 

<?php foreach ($arrGeocodeSales as $key => $value): 
    $address = $value['Address']."+".$value['Municipality']."+Ontario+Canada"; 
    $googleAddress = "https://maps.google.com/maps/geo?q=".urlencode($address)."&output=xml"; 
    // Retrieve the URL contents 
    $googlePage = file_get_contents($googleAddress); 
    // Parse the returned XML file 
    $xml = new SimpleXMLElement($googlePage); 
    // Parse the coordinate string 
    list($lng, $lat, $alt) = explode(",", $xml->Response->Placemark->Point->coordinates); 
?> 
<tr> 
    <td ><?php echo str_pad($row++, 2, '0', STR_PAD_LEFT); ?></td> 
    <td ><?php echo $value['SingleRoll']; ?></td> 
    <td ><?php echo $address; ?> </td> 
    <td ><?php echo $lat ?></td> 
    <td ><?php echo $lng ?></td> 
    <td ><?php echo $value['MapSaleNumber']; ?></td> 
    </tr> 
<?php endforeach; ?> 

답변

1

당신은 방법 documented here를 사용하여 SimpleXMLElement 결과를 조작 할 수 있습니다.

type 요소 값을 찾으려면 address_component 요소를 반복해야합니다. 해당 API를 사용하는 다른 코드의 예를 찾아서 빨리 알아낼 수 있어야합니다.