2017-04-05 65 views
0

我使用.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" 
} 
+0

爲什麼'foreach'如果你只設置一個單數的'data'變量?爲什麼在這個字符串前加上'return Json(「Successful」+ data,'這可能會產生無效的結果? –

+0

因爲我想要這個數據在ajax成功方法中返回 – shweta

+0

爲什麼不只是'return Json(googleResults,JsonRequestBehavior.AllowGet)然後你可以得到這個JSON字符串並處理它的客戶端 –

回答

0
I solved this problem by sending json data to client side like this 
var data = ""; 
      dynamic googleResults = new Uri(url + pincode).GetDynamicJsonObject(); 
      var status = googleResults.status; 
      foreach (var result in googleResults.results) 
      { 
           data += result; 
      } 
      return Json(data, "application/json",Encoding.UTF8, JsonRequestBehavior.AllowGet); 


and then process this data in javascript ,code given below 

data = JSON.parse(result); 

       for(var i=0;i<data.address_components.length;i++) 
       { 

        if(data.address_components[i].types[0]=="administrative_area_level_2") 
        { 
         alert("City:" + data.address_components[i].long_name); 
         document.getElementById("pin_city").value = data.address_components[i].long_name; 
        } 
        if (data.address_components[i].types[0] == "administrative_area_level_1") { 
         alert("State:" + data.address_components[i].long_name); 
         document.getElementById("pin_state").value = data.address_components[i].long_name; 
        } 
        if (data.address_components[i].types[0] == "country") { 
         alert("Country:" + data.address_components[i].long_name); 
        } 

       } 
+0

謝謝@Mark Schultheiss的建議 – shweta

0
data=["address_components"]["type"]["administrative_level_2"][your value] 
data2=["address_components"]["type"]["administrative_level_1"][your value] 

你也可以像這樣使用

data=["result"]["address_components"]["type"]["administrative_level_2"][your value]; 
data2=["result"]["address_components"]["type"]["administrative_level_1"][your value]; 

結果是第一個json的數組

相關問題