简体   繁体   中英

Retrievin GPS/DMS Coordinates from Google

I have some code in an ASP .Net program which makes a request to Google map API and retrieves the co-ordinates as LAT/LNG

      string url = https://maps.google.com/maps/api/geocode/xml?address=ABC DEF,UK&sensor=false&key=AI123ZYX
        WebRequest request = WebRequest.Create(url);

        using (WebResponse response = (HttpWebResponse)request.GetResponse())
        {
            using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
            {
                // removed for brevity
                // Returns LAT/LNG values
            }
        }

This works as expected but i cant workout how to convert or retrieve the DMS (Degree, Minutes, Second eg 1° 23' 45" W 67° 89' 10" N) value.

I've checked Googles documentation but everything points at a phone or web which you enter values or the address and it would display it.

Im trying to do the same but programatically so i could store the LAT/LNG along with the DMS (or maybe known as GPS) co-ordinates. How do i retrieve these co-ordinates?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM