如何从谷歌地图地理编码json数据中读取城市,州,国家 [英] how to read city ,state,country from google map geocoding json data
本文介绍了如何从谷歌地图地理编码json数据中读取城市,州,国家的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用.net MVC 3和Google Maps v3.我想在一个动作中进行地理编码.那是将有效地址传递给Google,并返回城市,州和国家/地区.您将如何在使用C#的操作中做到这一点?
I'm using .net MVC 3 and Google Maps v3. I'd like to do geocoding in an action. That is passing a valid address to Google and getting the city,state and country back. How would you do this in an action using C#?
[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);
}
这是我从google地理编码中获取的json数据,我想从其中读取来自address_components类型管理_area_level_2,administrative_area_level_1,国家/地区的城市,州和国家
this is json data which i am getting from google geocoding from which i wants to read city,state and country from address_components types administrative_area_level_2,administrative_area_level_1,country
{
"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"
}
推荐答案
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);
}
}
这篇关于如何从谷歌地图地理编码json数据中读取城市,州,国家的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文