So it looks like a query to the Google Maps API will return you a JSON object (SRC:https://stackoverflow.com/a/34597393/8502032). C# doesn't handle JSON natively, so you can use Newtonsoft's Json.NET library to help you parse the object into something more workable.
You can access the information from there in a few ways. One is using Json.net's deserialize function. This will take the JSON string and interpolate it into a useable object. You can make a custom object that will grab the sections of JSON that you care about, and then drop the rest.
This comes from the answer I linked, and we'll use that as our dataset that we're trying to access.
"routes" : [
{
"bounds" : {
"northeast" : {
"lat" : 34.1330949,
"lng" : -117.9143879
},
"southwest" : {
"lat" : 33.8068768,
"lng" : -118.3527671
}
},
"copyrights" : "Map data ©2016 Google",
"legs" : [
{
"distance" : {
"text" : "35.9 mi",
"value" : 57824
},
"duration" : {
"text" : "51 mins",
"value" : 3062
},
"end_address" : "Universal Studios Blvd, Los Angeles, CA 90068, USA",
"end_location" : {
"lat" : 34.1330949,
"lng" : -118.3524442
},
"start_address" : "Disneyland (Harbor Blvd.), S Harbor Blvd, Anaheim, CA 92802, USA",
"start_location" : {
"lat" : 33.8098177,
"lng" : -117.9154353
},
... Additional results truncated in this example[] ...
So in order to access what we want we'd make a series of classes that match the information we want.
class Routes
{
Legs legs;
}
class Legs
{
EndLocation endLocation;
}
class EndLocation
{
string lat;
string lon;
}
This is rough code, and I haven't been able to test it but it should hopefully give you a good starting direction.
using Newtonsoft.Json;
void ReadJson()
{
// Let's say for now that our results are held in a file, for simplicity's sake
string jsonString = File.ReadAllText(location.txt);
// Now we'll call the deserializer, since we have the JSON string.
// It'll turn it into our object automatically with the template.
Routes locationInfo = JsonConvert.DeserializeObject<Routes>(jsonString);
serializer.Deserialize(jsonString, locationInfo);
}
After that you should be able to access the locationInfo
object, just as you would any regular C# object.
This should work, but at the very least I'm hoping gives you a good direction on how to solve your problem.