Ich benutze .net MVC 3 und Google Maps v3. Ich möchte Geocoding in einer Aktion durchführen. Das ist eine gültige Adresse an Google und die Stadt, Staat und Land zurück zu bekommen. Wie würden Sie dies in einer Aktion mit C# tun?Wie man Stadt, Staat, Land von Google Karte Geocoding JSON Daten lesen
[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);
}
dies json Daten, die ich von Google Geocoding, von dem ich aus address_components Stadt, Staat und Land lesen, will ich immer auch Arten administrative_area_level_2, administrative_area_level_1, Land
{
"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"
}
Warum die 'foreach' wenn Sie nur eine singuläre' data' Variable setzen? Warum diese Zeichenfolge 'return Json (" Erfolgreich "+ Daten,' die wahrscheinlich ungültige Ergebnisse erzeugt? –
, weil ich möchte, dass diese Daten in AJAX-Erfolgsmethode zurückgegeben werden – shweta
Warum nicht nur 'zurück Json (googleResults, JsonRequestBehavior.AllowGet);' Sie könnten dann diese JSON-Zeichenfolge abrufen und sie clientseitig verarbeiten –