2012-08-31 19 views
1

https://developers.google.com/maps/documentation/distancematrix/如何從谷歌地圖API處理JSON

如果我輸入:

http://maps.googleapis.com/maps/api/distancematrix/json?origins=Hickory+NC&destinations=Niagra+Falls+NY&sensor=false&units=imperial 

然後我得到:

{ 
    "destination_addresses" : [ "Niagara Falls, NY, USA" ], 
    "origin_addresses" : [ "Hickory, NC, USA" ], 
    "rows" : [ 
     { 
     "elements" : [ 
      { 
       "distance" : { 
        "text" : "667 mi", 
        "value" : 1072719 
       }, 
       "duration" : { 
        "text" : "11 hours 21 mins", 
        "value" : 40835 
       }, 
       "status" : "OK" 
      } 
     ] 
     } 
    ], 
    "status" : "OK" 
} 

但如果我發出以下命令:

<cfhttp method="get" result="myContent" url="http://maps.googleapis.com/maps/api/distancematrix/json"> 
    <cfhttpparam type="url" name="origins" value="Hickory+NC"> 
    <cfhttpparam type="url" name="destinations" value="Niagra+Falls+NY"> 
    <cfhttpparam type="url" name="sensor" value="false"> 
    <cfhttpparam type="url" name="units" value="imperial"> 
</cfhttp> 
<cfdump var="#myContent#"> 

然後我找回一個結構StatusCode = 200OK,但Text = NO。

+0

我想嘗試這個自己,但我爲我的手機上。嘗試* not * URL編碼param標記中的值。只用一個空格,而不是+,看看你是否得到不同的結果。 –

+0

啊。同樣的結果。 –

+0

確保你正在查看myContent.FileContent。我運行了你的代碼並得到了一個有效的結果。沒有修改。 –

回答

2

添加getAsBinary="never"您CFHTTP

<cfhttp method="get" result="myContent" url="http://maps.googleapis.com/maps/api/distancematrix/json" getAsBinary="never"> 
    <cfhttpparam type="url" name="origins" value="Hickory+NC"> 
    <cfhttpparam type="url" name="destinations" value="Niagra+Falls+NY"> 
    <cfhttpparam type="url" name="sensor" value="false"> 
    <cfhttpparam type="url" name="units" value="imperial"> 
</cfhttp> 
<cfdump var="#myContent#"> 

返回我這個在myContent.FileContent

{ 
    "destination_addresses": ["Niagara Falls, NY, USA"], 
    "origin_addresses": ["Hickory, NC, USA"], 
    "rows": [{ 
     "elements": [{ 
      "distance": { 
       "text": "667 mi", 
       "value": 1072719 
      }, 
      "duration": { 
       "text": "11 hours 21 mins", 
       "value": 40835 
      }, 
      "status": "OK" 
     }] 
    }], 
    "status": "OK" 
} 
+0

謝謝鄧肯! –