我使用.net MVC 3和谷歌地圖v3。我想在一個動作中進行地理編碼。這是通過一個有效的地址給谷歌,並獲得了城市,州和國家。你如何在使用C#的操作中做到這一點?如何從谷歌地圖閱讀城市,州,國家地理編碼json數據
[HttpPost]
public ActionResult getLocation(string pincode)
{
string url = "http://maps.googleapis.com/maps/api/geocode/json?
sensor=true&address=";
var data="";
dynamic googleResults = new Uri(url +
pincode).GetDynamicJsonObject();
foreach (var result in googleResults.results)
{
data= "[" + result.geometry.location.lat + "," +
result.geometry.location.lng + "] " + result.formatted_address;
}
return Json("Successful" + data, JsonRequestBehavior.AllowGet);
}
這就是我正在從我希望從address_components類型administrative_area_level_2,administrative_area_level_1閱讀城市,州和國家谷歌的地理編碼獲取JSON數據,國家
{
"results" : [
{
"address_components" : [
{
"long_name" : "411022",
"short_name" : "411022",
"types" : [ "postal_code" ]
},
{
"long_name" : "SRPF",
"short_name" : "SRPF",
"types" : [ "political", "sublocality", "sublocality_level_2" ]
},
{
"long_name" : "Wanowrie",
"short_name" : "Wanowrie",
"types" : [ "political", "sublocality", "sublocality_level_1" ]
},
{
"long_name" : "Pune",
"short_name" : "Pune",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Pune",
"short_name" : "Pune",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Maharashtra",
"short_name" : "MH",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "SRPF, Wanowrie, Pune, Maharashtra 411022, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 18.5065017,
"lng" : 73.9128001
},
"southwest" : {
"lat" : 18.4966367,
"lng" : 73.90290019999999
}
},
"location" : {
"lat" : 18.4986371,
"lng" : 73.9074389
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 18.5065017,
"lng" : 73.9128001
},
"southwest" : {
"lat" : 18.4966367,
"lng" : 73.90290019999999
}
}
},
"place_id" : "ChIJQ1UppdzBwjsRPIND-6yqBNI",
"types" : [ "postal_code" ]
}
],
"status" : "OK"
}
爲什麼'foreach'如果你只設置一個單數的'data'變量?爲什麼在這個字符串前加上'return Json(「Successful」+ data,'這可能會產生無效的結果? –
因爲我想要這個數據在ajax成功方法中返回 – shweta
爲什麼不只是'return Json(googleResults,JsonRequestBehavior.AllowGet)然後你可以得到這個JSON字符串並處理它的客戶端 –