3

I have a restaurant delivery WINFORM c# application where in user needs to enter the address, as to avoid human error we are trying to integrate google map's autocomplete functionality into textbox. I have googled and all I saw was for asp nothing for WINFORM. I tried one code however its namespace is missing and i am not sure which one it is

Namespace used :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Google.Api.Maps ;
using GoogleApi.Helpers;

Heres the code :

  private void textBox1_TextChanged(object sender, EventArgs e)
    {
        var autoCompleteListCollection = new AutoCompleteStringCollection();
        autoCompleteListCollection.AddRange(new GoogleApi.Instance.GetAddressPredictionsByInput(textBox1.Text).ToArray());
        textBox1.AutoCompleteCustomSource = autoCompleteListCollection;
    }

Error :

Error 1 The type or namespace name 'Instance' does not exist in the namespace 'GoogleApi' (are you missing an assembly reference?)

I think this might work. So please let me know right namespace or guide me how to achieve above requirement

Finally I got it to work, but it crashes the app, heres the code :

 private void textBox1_TextChanged(object sender, EventArgs e)
    {

        string url = "https://maps.googleapis.com/maps/api/place/autocomplete/json?input=" + var + "&types=geocode&key=YOURAPIKEY";
        string content = fileGetContents(url);
        JObject o = JObject.Parse(content);
        List<string> add = new List<string>();
        try
        {
            JObject jObj = (JObject)JsonConvert.DeserializeObject(content);
            int count = jObj.Count;
            for (int i = 0; i < count; i++)
            {

                add.Add((string)o.SelectToken("predictions["+i+"].description"));



            }

            var collection = new AutoCompleteStringCollection();
            collection.AddRange(add.ToArray());
            textBox1.AutoCompleteCustomSource = collection;
            textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
            textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;



        }
        catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        {
        }

    protected string fileGetContents(string fileName)
    {
        string sContents = string.Empty;
        string me = string.Empty;
        try
        {
            if (fileName.ToLower().IndexOf("https:") > -1)
            {
                System.Net.WebClient wc = new System.Net.WebClient();
                byte[] response = wc.DownloadData(fileName);
                sContents = System.Text.Encoding.ASCII.GetString(response);

            }
            else
            {
                System.IO.StreamReader sr = new System.IO.StreamReader(fileName);
                sContents = sr.ReadToEnd();
                sr.Close();
            }
        }
        catch { sContents = "unable to connect to server "; }
        return sContents;
    }

Everytime I run after few attempts it crashes. Thanks

mark
  • 623
  • 3
  • 21
  • 54
  • Did you get this working anyhow ? I am also trying something similar for WPF application, i can't seem to find any solution to have this done in WPF C# – Jolly Jan 07 '22 at 06:33
  • This must be too old though, I think we should not handle this in textchange event instead there should be a button click, this will avoid performance issues – Naga Feb 01 '23 at 11:11

0 Answers0