2

I am developing a EPOS System in C#.NET. In My Project, i have to implement a scenario:

  1. User will enter Zip Code/ Postal Code in a Text box.
  2. Application using google places API will return City, Street and Town separately in 3 Tex box respectively. Please any one help me how to implement this scenario in C#.net.

i already created Google Project on https://console.cloud.google.com/apis/credentials/key and generate Google Place API Key for my project.

I Have searched a lot on google but all example are implemented in asp.net, but i need in C# Net. Anyone Help me. Thanks in Advance.

enter image description here

I have implemented Some how but i don't know how to read it

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web.Script.Serialization;
using Newtonsoft.Json;
namespace apidemo
{
    class Program
    {
        String response, result;
         const String apiKey= "My APi kEy";
        static void Main(string[] args)
        {
            getdata("B11 4RA");
            Console.ReadLine();
        }
        static async public void getdata(String code)
        {
            try
            {
                using (var client = new HttpClient())
                {
                   

                    var response = await client.GetStringAsync(String.Format("https://maps.googleapis.com/maps/api/place/autocomplete/json?key="+ apiKey + "&input=" +code));
               
                    Console.WriteLine(response);
                   



                }
            }
            catch (Exception ex) {
                Console.Write(ex.ToString());
            }

          
        }
   

    }
}

Output is this enter image description here

  • 2
    There is not difference between .NET Framework and ASP.NET except UI/Controller related code. Actually, ASP.NET is also a .NET Framework based app. –  Jul 04 '20 at 06:10
  • 1
    @donggas90 i mean c#. net – Engr. Khuram Shahzad Jul 04 '20 at 06:23
  • 1
    Application developing C#.net and asp.net are different, one for Desktop other for We respectively – Engr. Khuram Shahzad Jul 04 '20 at 06:25
  • 1
    Why don't you using Google API libraries for .NET? It should support .NET Standard that is not depend on any runtime platform. .NET Framework, .NET, .NET Core(lagacy of .NET), ASP.NET, ASP.NET Core whatever any of .NET CLR. Therefore, there is no difference. –  Jul 04 '20 at 06:41
  • 1
    Anyway, if you don't using any of library, the HTTP is global standard protocol. Therefore you can call the Google APIs manually by `HttpClient` it is included .NET Standard. It means, all of .NET runtimes support the class. –  Jul 04 '20 at 06:45
  • Can you share any helpful content that how to use Google API libraries for .NET in my application – Engr. Khuram Shahzad Jul 04 '20 at 06:47
  • Search the NuGet like this. https://www.nuget.org/packages/Google.Apis I don't know what packages related with it. –  Jul 04 '20 at 06:49
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/217192/discussion-between-khuram-shahzad-and-donggas90). – Engr. Khuram Shahzad Jul 04 '20 at 07:01
  • Plaese edit your question inlcude your code and describe any issues you are having with your current solution. – Linda Lawton - DaImTo Jul 04 '20 at 14:00
  • @DaImTo i have implement some how but now i am stuck here , How to get desire result from 'responce' – Engr. Khuram Shahzad Jul 04 '20 at 14:59
  • Have you tried Googleing it? https://stackoverflow.com/q/6620165 Your not going to get the result directly from that response the response is in JSON its going to be up to you to format it. No one here is going to code your project for you your going to have to do this. – Linda Lawton - DaImTo Jul 04 '20 at 15:08

1 Answers1

4

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);
}
Attique Ur Rehman
  • 336
  • 1
  • 2
  • 19