Google Maps API for C#

Google Maps API for C#

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`Ag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sB`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

How to implement Google Place API in C#.Net

If we look at the documentation for the Google Places API, we can see the format of the JSON that a request to the API returns. You can traverse the json object and get your required values as in below snippet.

JObject objectContainer = response.Value<JObject>("candidates");

foreach (KeyValuePair<string, JToken> tag in objectContainer)
{
if(tag.key=="formatted_address")
var address = tag.value;
if(tag.key=="name")
var name = tag.value;
}

With a simple HTTP request to the Google Places API, we can then use above code to get required fields.

using (var client = new HttpClient())
{
var response = await client.GetStringAsync("https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input=YOUR_POSTCODE&inputtype=textquery&&fields=photos,formatted_address,name,opening_hours,rating&key=YOUR_API_KEY");
var result = JsonConvert.DeserializeObject<PlacesApiQueryResponse>(response);
}

.NET Library for Google Maps API support

This API is still in development but may provide you with what you are looking for:

http://code.google.com/p/gmaps-api-net/

.Net wrapper for Google Maps API V3

Not sure how useful this answer is but here I go.

I wouldnt choose any wrapper out there without doing some research about the same. From the url that you shared looks like there is only 1 contributor to the project, also the dwnld count on the latest api is 117 which I dont think is a great number to warrant its use, there have only been 3 issues reported so far and from the looks of it they are inquiries or feature requests (not sure if it means that there are no bugs in the project or it hasn't been used).

Anyways having said that there are commercial wrappers out there which support google maps. I can think of ThinkGeo for one. see if that is within your budget.

Now to your dll question. Just adding a reference in your web project should do the trick and then probably creating a seprate controller that serves your map. Is that what you wanted?



Related Topics



Leave a reply



Submit