在我的C#项目中,我试图获取建筑物的经纬度。我将此线程用作基础Distance between addresses,并尝试对其进行调整以获取位置的经度和纬度。目前,从API中获取JSON响应很好,正在从JSON响应中获取我想要的值。
这是我的代码
string requesturl = url;
string content = fileGetContents(requesturl);
JObject o = JObject.Parse(content);
user.Latitude = (double)o.SelectToken("results[0].geometry[0].location[0].lat");
user.Longitude = (double)o.SelectToken("results[0].geometry[0].location[0].lng");
protected string fileGetContents(string fileName)
{
string sContents = string.Empty;
string me = string.Empty;
try
{
if (fileName.ToLower().IndexOf("https:") > -1)
{
System.Net.WebClient wc = new System.Net.WebClient();
byte[] response = wc.DownloadData(fileName);
sContents = System.Text.Encoding.ASCII.GetString(response);
}
else
{
System.IO.StreamReader sr = new System.IO.StreamReader(fileName);
sContents = sr.ReadToEnd();
sr.Close();
}
}
catch { sContents = "unable to connect to server "; }
return sContents;
}
答案 0 :(得分:2)
问题似乎出在您试图像访问列表一样访问几何和位置。试试这个:
user.Latitude = (double)o.SelectToken("results[0].geometry.location.lat");
user.Longitude = (double)o.SelectToken("results[0].geometry.location.lng");