2013-05-10 102 views
1

我想對網址進行編碼,將與谷歌地圖API的工作編碼一個C#字符串到谷歌地圖API網址

string rootURL = "http://maps.googleapis.com/maps/api/geocode/json?"; 
         string[][] parms = new string[][] { new string[] { "Address", "3806", "Sterling Road", "Downers Grove", "IL" }, new string[] { "sensor", "false" } }; 
         rootURL += HttpUtility.UrlEncode(parms[]); 

最終的結果應該是這樣的: http://maps.googleapis.com/maps/api/geocode/json?address=3806+Sterling+Road,+Downers+Grove,+IL&sensor=false

不知道是什麼我正在實施錯誤。

回答

2

試用一下這個:

var firstPart = new string[] {"3806", "Sterling Road", "Downers Grove", "IL"}; 
var secondPart = string.Format("{0}={1}", "sensor","false"); 
var joinedFirstPart = string.Join(" ", firstPart); 
//we want to encode only the parameters with white spaces 
var encodedParams = HttpUtility.UrlEncode(joinedFirstPart); 
var rootUrl = string.Format("http://maps.googleapis.com/maps/api/geocode/json?address={0}&{1}", encodedParams, secondPart); 
+0

曾爲太感謝你了! – Jon 2013-05-10 20:54:18