[英]C# Getting lat and long from Google GeoCoding API
In my C# project, I'm trying to get the lat and long location of a building. 在我的C#项目中,我试图获取建筑物的经纬度。 I used this thread as a base Distance between addresses and tried to adapt it to get the lat and long of a location. 我将此线程用作地址之间的基本距离,并尝试对其进行调整以获取位置的经度和纬度。 At the moment, the getting the JSON response from the API is fine, it's getting the value I want from the JSON response. 目前,可以从API中获取JSON响应,也可以从JSON响应中获取所需的值。
Here is my code at the moment 这是我现在的代码
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;
}
Looks like the problem is that you're trying to access geometry and location as if they were lists. 看起来问题在于您试图像访问列表一样访问几何和位置。 Try this: 尝试这个:
user.Latitude = (double)o.SelectToken("results[0].geometry.location.lat");
user.Longitude = (double)o.SelectToken("results[0].geometry.location.lng");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.