[Solved] google maps API for C#

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 app all i need to know if i can use the google maps API on C#.
This is the one i will like to use

Ive look for it on many places but all i could find was alternatives to using Gmaps but thats not what i want.

¿It is possible to use it?

Solution #1:

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" : "[email protected]]@[email protected]@[email protected]@[email protected][email protected]@[email protected]`@[email protected]`[email protected]{YlZiBdBaIhGkFrDeCtBuFxFmIdJmOjPaChDeBlDiAdD}ApGcDxU}@hEmAxD}[[email protected]yBdEqFnJqB~DeFxMgK~VsMr[uKzVoCxEsEtG}[email protected]][email protected]{[email protected]`CwBfHaEzJuBdEyEhIaBnCiF|K_Oz
            {MdZwAbDaKbUiB|CgCnDkDbEiE|[email protected]|[email protected]|[email protected][email protected]`[email protected]`@}F`MqK`PeGzIyGfJiG~GeLhLgIpIcE~FsDrHcFfLqDzH{CxEwAbBgC|B}F|DiQzKsbBdeA{[email protected]~[email protected]^wJfOySx[[email protected]_EvAiFpCaC|AqGpEwHzFoQnQoTrTqBlCyDnGmCfEmDpDyGzGsIzHuZzYwBpBsC`CqBlAsBbAqCxAoBrAqDdDcNfMgHbHiPtReBtCkD|GqAhBwBzBsG~FoAhAaCbDeBvD_BlEyM``@uBvKiA~DmAlCkA|B}@lBcChHoJnXcB`GoAnIS~CIjFDd]A|QMlD{@jH[[email protected]`[email protected]~HeMx^eDtJ}BnG{DhJU`@[email protected]@[email protected]{[email protected]@kBSsEW{GV}[email protected]?
            [email protected]@mE`BsH|[email protected]}[email protected]@[email protected]][email protected]@[email protected][email protected]`DoBxDaHvM_FzImDzFeCpDeC|CkExDiJrHcBtAkDpDwObVuCpFeCdHoIluBjIuClJsEvMyDbMqAhEoDlJ{C|J}FlZuBfLyDlXwB~QkArG_AnDiAxC{G|OgEdLaE`LkBbEwG~KgHnLoEjGgDxCaC`BuJdFkFtCgCnBuClD_HdMqEzHcBpB_C|BuEzCmPlIuE|[email protected]@|[email protected]_AlC{AxCcL`U{GvM_DjFkBzBsB`[email protected]@[email protected]|BcFjC}DbEuD~D`@[email protected]@[email protected]@}@vAgCbEaHfMqA`[email protected]@bAO`@[email protected]@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

Respondent: jHilscher

Solution #2:

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);
}
Respondent: Alex Pandrea

Solution #3:

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();
Respondent: Hobbykitjr

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.