Reputation: 55
In my C# project, I'm trying to get the lat and long location of a building. 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.
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;
}
Upvotes: 0
Views: 1969
Reputation: 106
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");
Upvotes: 2