2010-02-28 71 views
1

我使用谷歌地圖API地址解析服務來獲得位置的國家名稱,郵政編碼等谷歌地圖的地理編碼服務

JSON響應:

{ 
    "name": "1600 Amphitheatre Parkway, Mountain View, CA, USA", 
    "Status": { 
    "code": 200, 
    "request": "geocode" 
    }, 
    "Placemark": [ 
    { 
     "address": "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA", 
     "AddressDetails": { 
     "Country": { 
      "CountryNameCode": "US", 
      "AdministrativeArea": { 
      "AdministrativeAreaName": "CA", 
      "SubAdministrativeArea": { 
       "SubAdministrativeAreaName": "Santa Clara", 
       "Locality": { 
       "LocalityName": "Mountain View", 
       "Thoroughfare": { 
        "ThoroughfareName": "1600 Amphitheatre Pkwy" 
       }, 
       "PostalCode": { 
        "PostalCodeNumber": "94043" 
       } 
       } 
      } 
      } 
     }, 
     "Accuracy": 8 
     }, 
     "Point": { 
     "coordinates": [-122.083739, 37.423021, 0] 
     } 
    } 
    ] 
} 

這裏的問題是 - 有時(儘管檢查未定義)我得到一個國家未定義的錯誤。

var country = document.getElementById("id_country"); 
if(place.AddressDetails.Country.CountryNameCode != undefined){ 
    country.value = place.AddressDetails.Country.CountryNameCode; 
} 

另外如何訪問PostalCodeNumber?

place.AddressDetails.AdministrativeArea.SubAdministrativeArea.PostalCode.PostalCodeNumber 

回答

1

大多數時候,當我嘗試這樣的代碼:

place.AddressDetails.AdministrativeArea. //etc. 

我沒有得到任何東西,我不知道該如何解釋,但我的隊友寫了一個腳本來手動解析它它對我們來說很好(代碼雖然很多)。您可以嘗試並編輯它以獲取剩餘字段(您的郵政編碼):

//[MARKEL]: Returns object type: obj.ad1, obj.ad2, obj.ad3, obj.state, obj.country 
function ResolveGeoCode(point, returnCall) { 
    //[MARKEL]: initialize geocoder 
    geocoder = new GClientGeocoder(); 

    geocoder.getLocations(point, function getAddress(response) { 
     //[MARKEL]: Create a object to call proxy location Set location variable to be global 
     //because it will be assigned in call-back function 
     var Location; 

     if (!response || response.Status.code != 200) { 
      //MARKEL: [TODO] => Set code here to alert that the address id invalid 
     } 
     else { 
      place = response.Placemark[0]; 
      place = response.Placemark[0]; 
      point = new GLatLng(place.Point.coordinates[1], 
         place.Point.coordinates[0]); 

      var len = place.address.toString().split(",").length; 
      var array = place.address.toString().split(","); 
      //alert(place.address); 
      //alert(len); 
      if (len >= 3) { 
       if (array[0].length > 2) { 
        Location = { 
         Street: array[0], 
         State: array[1], 
         Country: array[2] 
        }; 
       } 
       else { 
        Location = { 
         Street: array[1], 
         State: "", 
         Country: array[2] 
        }; 
       } 
      } 
      else if (len == 2) { 
       Location = { 
        Street: "", 
        State: array[0], 
        Country: array[1] 
       }; 

      } 

      else if (len == 1) { 
       Location = { 
        Sreet: "", 
        State: "", 
        Country: array[0] 
       }; 
      } 
      else { 
       //[MARKEL]: [TODO] => Place code here to indicate that the address is not valid. 
      } 

     } 
     returnCall(Location); 
     return Location; 
    }); 

}