Ejemplo n.º 1
0
        // TEST KEY BUTTON
        private void button2_Click(object sender, EventArgs e)
        {
            btnTestApi.BackColor = button1.BackColor; // clear color to default
            timelineentry te = new timelineentry();

            te.Place = "1600 Pennsylvania Avenue, Washington D.C";
            te.Name  = "President Lincoln";
            if (GetGeoCode2(ref te))
            {
                btnTestApi.BackColor = Color.Lime;
                toolTip1.SetToolTip(btnTestApi, "Google Maps API Key is Valid");
            }
            else
            {
                btnTestApi.BackColor = Color.Red;
                toolTip1.SetToolTip(btnTestApi, "Invalid API Key");
            }
        }
Ejemplo n.º 2
0
        // GETGEOCODE2
        // CALL WITH: a timelineemtry class object
        // RETURNS: fills in geocode relevant fields using callobject.Place
        private bool GetGeoCode2(ref timelineentry te)
        {
            byte[]       buffer         = new byte[2048];
            const string ADDRESS_FIELD  = "formatted_address";
            const string LOCATION_FIELD = "location";
            string       httprequest    = String.Empty;

            httprequest = FormatGeoCodeRequest(te.Place);
            using (WebClient client = new WebClient())
            {
                client.Headers["User-Agent"] =
                    "Mozilla/4.0 (Compatible; Windows NT 5.1; MSIE 6.0) " +
                    "(compatible; MSIE 6.0; Windows NT 5.1; " +
                    ".NET CLR 1.1.4322; .NET CLR 2.0.50727)";
                bool   result  = true;
                string result2 = String.Empty;

                // Download data.
                try
                {
                    buffer = client.DownloadData(httprequest);
                }
                catch (Exception ex)
                {
                    sfeht.GeneralExceptionHandler("Http Request failed", "(10) - Test API Key", false, ex);
                    result = false;
                }
                finally
                {
                    client.Dispose();
                }
                if (result)
                {
                    foreach (byte b in buffer)
                    {
                        result2 += (Char)b;
                    }

                    string[] parsedresult = result2.Split('"');
                    te.Status = parsedresult[parsedresult.Length - 2]; // get status code
                    int x = 0;
                    // EXTRACT FORMATTED ADDRESS
                    for (x = 0; x < parsedresult.Length; x++)
                    {
                        if (parsedresult[x] == ADDRESS_FIELD)
                        {
                            te.FormattedAddress = parsedresult[x + 2];
                            te.Latitude         = parsedresult[x + 5];
                            te.Longitude        = parsedresult[x + 6];
                            break;
                        }
                    }
                    // EXTRACT LATITUDE AND LONGITUDE
                    for (x = 0; x < parsedresult.Length; x++)
                    {
                        if (parsedresult[x] == LOCATION_FIELD)
                        {
                            te.Latitude  = parsedresult[x + 3].Substring(2, 10);
                            te.Longitude = parsedresult[x + 5].Substring(2, 10);
                            break;
                        }
                    }

                    return(true);
                }
                return(false);
            }
        }