2012-04-19 56 views
0

我知道JSON輸出格式爲given by GoogleGoogle Maps Javascript API v3返回不同的JSON?

但是,我有時會得到一個slightly different format。這是從results變量的gecoder.geocode({'address': address}, callback)

"location": { 
      "Za": 37.3492097, 
      "$a": -122.03260190000003 
     }, 

回調函數...

"viewport": { 
      "aa": { 
      "b": 37.329901, 
      "f": 37.37543489999999 
      }, 
      "ba": { 
      "b": -122.06526500000001, 
      "f": -121.99577999999997 
      } 
     } 

通知下位置,範圍和視口的鑰匙 - 他們是從什麼標準不同。這對我來說是個問題,因爲我需要通過 AJAX調用和 將此JSON發送到我的服務器並解析它,並且我的解析器不能將「$ a」作爲有效的密鑰名稱。

這是正常的還是我錯過了什麼?編輯: 我正在一個雲託管平臺(salesforce.com)上。我們正在使用JavaScript API v3。我能夠從本地HTML文件複製此問題。 我現在認爲這可能是因爲我支持公司代理。任何人都面對過這個?

+0

爲什麼你的解析器不能使用它?這是一個有效的json密鑰名稱。也許修復你的解析器? – tkone 2012-04-19 11:06:14

+0

正確制定的​​查詢的結果是「略有不同的格式」數據嗎?那個查詢是什麼?如果您遵循了文檔,結果應該一致。 – 2012-04-19 11:17:25

+0

我正在使用的解析器內置於我所使用的平臺中,期望鍵值是類中的變量名稱,「$」不是接受的字符 – 2012-04-19 13:28:26

回答

0

正如在對錯誤報告和API文檔的評論中指出的,回調中的response對象不是純JSON。爲了得到一個JSON對象,我需要像下面這樣創建一個:

//assume status is ok 

var bestResult = getBestResultFromJSON(results); 

var bounds = {'northeast' : {'lat' : 0, 'lng' : 0}, 'southwest' : {'lat' : 0, 'lng' : 0}}; 
var location = {'lat' : 0, 'lng' : 0}; 
var viewport = {'northeast' : {'lat' : 0, 'lng' : 0}, 'southwest' : {'lat' : 0, 'lng' : 0}}; 
// bounds not always available (https://developers.google.com/maps/documentation/javascript/reference#GeocoderGeometry) 
if(bestResult.geometry.hasOwnProperty(bounds)) { 
    bounds.southwest.lat = bestResult.geometry.bounds.getSouthWest().lat(); 
    bounds.southwest.lng = bestResult.geometry.bounds.getSouthWest().lng(); 
    bounds.northeast.lat = bestResult.geometry.bounds.getNorthEast().lat(); 
    bounds.northeast.lng = bestResult.geometry.bounds.getNorthEast().lng(); 
} 
else { 
    bounds = null; 
}      

viewport.southwest.lat = bestResult.geometry.viewport.getSouthWest().lat(); 
viewport.southwest.lng = bestResult.geometry.viewport.getSouthWest().lng(); 
viewport.northeast.lat = bestResult.geometry.viewport.getNorthEast().lat(); 
viewport.northeast.lng = bestResult.geometry.viewport.getNorthEast().lng(); 

location.lat = bestResult.geometry.location.lng(); 
location.lng = bestResult.geometry.location.lng(); 

var jsonResults = {'results' : [{'address_components' : bestResult.address_components, 
       'formatted_address': bestResult.formatted_address, 
       'geometry' : {'bounds' : bounds, 'location' : location, 'location_type' : bestResult.location_type, 'viewport' : viewport}, 
       'types' : bestResult.types}], 'status' : status}; 
0

如果您僅使用記錄的方法並獲得未記錄的響應,則表示存在錯誤。在Issues List中提出問題 - 如果存在一個能夠持續產生錯誤響應的地址,則需要包含演示地址。如果沒有一致錯誤的地址,請儘可能詳細地提供。例如,是否在請求重複過於靠近的情況下發生?

確保您還使用不同的瀏覽器客戶端以及甚至不同的機器進行測試。您當前的瀏覽器或計算機中可能存在一個奇怪的情況,導致該API錯誤操作。

我建議用問題編號評論這個答案以備將來參考。

+0

我已經提交[bug 4089](http://code.google.com/p/gmaps-api-issues/issues/detail?id=4089) 看起來,即使從谷歌的地理編碼示例頁面,答案是不正確的。請參閱:https://google-developers.appspot.com/maps/documentation/javascript/examples/geocoding-simple – 2012-04-20 07:09:34

+0

該報告中的*數量多於此問題。使用salesforce.com,https和身份驗證可能都是相關的(這也意味着您應該擁有Google直接支持的Maps for Business許可證)。試着依次排除這三個組合中的每一個,並看看它是否有所作爲。例如,我不使用https或salesforce.com,也從來沒有像這樣的問題。 – 2012-04-20 07:25:31

+0

我們沒有使用Maps for Business _yet_。 我甚至創建了一個簡單的HTML文件,並得到相同的。我現在在想,也許是因爲我在代理的背後?一旦我嘗試從不同的網絡嘗試它,就會迴圈 – 2012-04-20 07:41:42