google maps API for C#

后端 未结 3 1740
终归单人心
终归单人心 2020-11-28 14:39

im really new in to using APIs so after looking on google maps Api page im not sure if there are APIs designed to be used for C#. I dont need a google maps to be show on my

相关标签:
3条回答
  • 2020-11-28 14:51

    Adding to Alex answer above...

    In their example they first try to write DurationInTraffic, which only works if you supply a departureTime. Also you want the value not the object to display the seconds or the text. You could just use Duration w/o the traffic option too.

    If you do want to set departure time, this project will set to epoc/linux/seconds since 1970, for you automatically, but i don't see a "now" option like in google, so be sure to set to UTC first.

    if you don't supply a departureTime, DurationInTraffic will be null so this would error.

    request.DepartureTime = DateTime.Now.ToUniversalTime();
    

    ...

    var x = response.Routes.First().Legs.First();
                    Console.WriteLine("Duration w/ traffic: " + x.DurationInTraffic.Value + " vs w/o " + x.Duration.Value);
                    Console.ReadLine();
    
    0 讨论(0)
  • 2020-11-28 15:10

    1. Create Requests to the Google Maps API by yourself

    You can just send HTTP-Requests to the google maps API and then work with the results. You could use WebRequest to forge requests to the google api. You need a Maps API Key for that.


    You would have to build up the GET parameters eg. origin=...&destination=...&key=YOUR_API_KEY + parse the JSON response to do something with it.

    Google Maps Api Documentation (under Web Service APIs) lists the request parameters and example responses.

    string url = @"https://maps.googleapis.com/maps/api/directions/json?origin=75+9th+Ave+New+York,+NY&destination=MetLife+Stadium+1+MetLife+Stadium+Dr+East+Rutherford,+NJ+07073&key=YOUR_API_KEY";
    
    WebRequest request = WebRequest.Create(url);
    
    WebResponse response = request.GetResponse();
    
    Stream data = response.GetResponseStream();
    
    StreamReader reader = new StreamReader(data);
    
    // json-formatted string from maps api
    string responseFromServer = reader.ReadToEnd();
    
    response.Close();
    

    A JSON response will look something like this (from Directions API )

    {
       "geocoded_waypoints" : [
          {
             "geocoder_status" : "OK",
             "place_id" : "ChIJRVY_etDX3IARGYLVpoq7f68",
             "types" : [
                "bus_station",
                "transit_station",
                "point_of_interest",
                "establishment"
             ]
          },
          {
             "geocoder_status" : "OK",
             "partial_match" : true,
             "place_id" : "ChIJp2Mn4E2-woARQS2FILlxUzk",
             "types" : [ "route" ]
          }
       ],
       "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[] ...
    
    
             "overview_polyline" : {
                "points" : "knjmEnjunUbKCfEA?_@]@kMBeE@qIIoF@wH@eFFk@WOUI_@?u@j@k@`@EXLTZHh@Y`AgApAaCrCUd@cDpDuAtAoApA{YlZiBdBaIhGkFrDeCtBuFxFmIdJmOjPaChDeBlDiAdD}ApGcDxU}@hEmAxD}[tt@yNb\\yBdEqFnJqB~DeFxMgK~VsMr[uKzVoCxEsEtG}BzCkHhKWh@]t@{AxEcClLkCjLi@`CwBfHaEzJuBdEyEhIaBnCiF|K_Oz\\
                {MdZwAbDaKbUiB|CgCnDkDbEiE|FqBlDsLdXqQra@kX|m@aF|KcHtLm@pAaE~JcTxh@w\\`v@gQv`@}F`MqK`PeGzIyGfJiG~GeLhLgIpIcE~FsDrHcFfLqDzH{CxEwAbBgC|B}F|DiQzKsbBdeA{k@~\\oc@bWoKjGaEzCoEzEwDxFsUh^wJfOySx[uBnCgCbCoFlDmDvAiCr@eRzDuNxC_EvAiFpCaC|AqGpEwHzFoQnQoTrTqBlCyDnGmCfEmDpDyGzGsIzHuZzYwBpBsC`CqBlAsBbAqCxAoBrAqDdDcNfMgHbHiPtReBtCkD|GqAhBwBzBsG~FoAhAaCbDeBvD_BlEyM``@uBvKiA~DmAlCkA|B}@lBcChHoJnXcB`GoAnIS~CIjFDd]A|QMlD{@jH[vAk@`CoGxRgPzf@aBbHoB~HeMx^eDtJ}BnG{DhJU`@mBzCoCjDaAx@mAnAgCnBmAp@uAj@{Cr@wBPkB@kBSsEW{GV}BEeCWyAWwHs@qH?
                cIHkDXuDn@mCt@mE`BsH|CyAp@}AdAaAtAy@lBg@pCa@jE]fEcBhRq@pJKlCk@hLFrB@lD_@xCeA`DoBxDaHvM_FzImDzFeCpDeC|CkExDiJrHcBtAkDpDwObVuCpFeCdHoIl\\uBjIuClJsEvMyDbMqAhEoDlJ{C|J}FlZuBfLyDlXwB~QkArG_AnDiAxC{G|OgEdLaE`LkBbEwG~KgHnLoEjGgDxCaC`BuJdFkFtCgCnBuClD_HdMqEzHcBpB_C|BuEzCmPlIuE|B_EtDeBhCgAdCw@rCi@|DSfECrCAdCS~Di@jDYhA_AlC{AxCcL`U{GvM_DjFkBzBsB`BqDhBaEfAsTvEmEr@iCr@qDrAiFnCcEzCaE~D_@JmFdGQDwBvCeErEoD|BcFjC}DbEuD~D`@Zr@h@?d@Wr@}@vAgCbEaHfMqA`Cy@dAg@bAO`@gCi@w@W"
             },
             "summary" : "I-5 N and US-101 N",
             "warnings" : [],
             "waypoint_order" : []
          }
       ],
       "status" : "OK"
    }
    

    2. ... or use a dedicated library

    • gmaps-api-net (github) offers an api interface for Google Maps. You can get it via nuget. I haven't testet it, but it looks very easy to use.

    This library is outdated, see https://stackoverflow.com/a/61531795/2881450

    0 讨论(0)
  • 2020-11-28 15:15

    As mentioned in the other respose(s), you can make HTTP requests and use the JSON response.

    If you want a .NET library that makes your life easier by abstracting the query/response you can use one of these .NET wrapper libraries for the Google Maps API :

    1. GoogleApi
    2. google-maps

    gmaps-api-net is outdated (at this time of answering) - the last update for the Directions API was made in 2016.

    Usage example for GoogleApi:

    using GoogleApi.Entities.Common;
    using GoogleApi.Entities.Maps.Directions.Request;
    using GoogleApi.Entities.Maps.Directions.Response;
    
    public void GetRoute()
    {
        DirectionsRequest request = new DirectionsRequest();    
    
        request.Key = "AIzaSyAJgBs8LYok3rt15rZUg4aUxYIAYyFzNcw";
    
        request.Origin = new Location("Brasov");
        request.Destination = new Location("Merghindeal");
    
        var response = GoogleApi.GoogleMaps.Directions.Query(request);
    
        Console.WriteLine(response.Routes.First().Legs.First().DurationInTraffic);
        Console.WriteLine(response.Routes.First().Legs.First().Distance);
        Console.WriteLine(response.Routes.First().Legs.First().Steps);
    }
    
    0 讨论(0)
提交回复
热议问题