Google在c#中映射自动填充文本框

时间:2016-11-30 06:16:01

标签: c# google-maps google-api

我有一个餐厅交付WINFORM c#应用程序,在用户需要输入地址,以避免人为错误我们正试图将谷歌地图的自动完成功能整合到文本框中。我用google搜索,我看到的只是用于WINFORM的asp。我尝试了一个代码,但它的命名空间丢失了,我不确定它是哪一个

使用的命名空间:

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;

下面是代码:

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

错误:

  

错误1命名空间“GoogleApi”中不存在类型或命名空间名称“Instance”(您是否缺少程序集引用?)

我认为这可能有效。所以请让我知道正确的命名空间或指导我如何达到上述要求

最后我得到了它的工作,但它崩溃了应用程序,继承人代码:

 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;
    }

每次我在几次尝试后都会跑,它会崩溃。 感谢

0 个答案:

没有答案