void bw_DoWork(object sender, DoWorkEventArgs e)
 {
     string address = (string)e.Argument;
     _bw.ReportProgress(0);
     currentgeodata=mapquest.GetLocation(address);
     e.Result = currentgeodata;
 }
 public void SetInvoice(Invoice invoice,int caller,DateTime date,DriverInfo driver,bool flag)
 {
     this.flag = flag;
     inputinvoice = invoice;
     this.caller = caller;
     this.viewdate = date;
     this.driverinfo = driver;
     currentgeodata = new GeoData();
 }
        /*
         *return GeoData
         *
         */
        public GeoData GetLocation(string address)
        {
            address.Replace('#',' ');
            GeoData location = new GeoData();
            List<float> longitude;
            if(cache.ContainsKey(address))
            {
                return(GeoData)cache[address];
            }
            string[] possiblelocations = new string[1];
            // used to build entire input
            StringBuilder sb = new StringBuilder();

            // used on each read operation
            byte[] buf = new byte[8192];

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.mapquestapi.com/geocoding/v1/address?key="+apikey+"&location="+address+"&maxResults=5");
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream resStream = response.GetResponseStream();
            string tempString = null;
            int count = 0;

            do
            {
                // fill the buffer with data
                count = resStream.Read(buf, 0, buf.Length);

                // make sure we read some data
                if (count != 0)
                {
                    // translate from bytes to ASCII text
                    tempString = Encoding.ASCII.GetString(buf, 0, count);

                    // continue building the string
                    sb.Append(tempString);
                }
            }
            while (count > 0); // any more data to read?
            string sbcomplete=sb.ToString();
            try
            {
                JObject o = JObject.Parse(sbcomplete);
                JObject nextlocation = (JObject)o.First.First.First.First.First.First;
                List<string> street = new List<string>();
                List<string> city = new List<string>();
                List<string> state = new List<string>();
                List<string> zip = new List<string>();
                longitude = new List<float>();
                List<float> latitude = new List<float>();
                while (nextlocation != null)
                {
                    longitude.Add((float)nextlocation.First.First.First.First);
                    latitude.Add((float)nextlocation.First.First.Last.First);
                    city.Add((string)nextlocation.First.Next.Next.Next.Next.First);
                    street.Add((string)nextlocation.First.Next.Next.Next.Next.Next.First);
                    state.Add((string)nextlocation.First.Next.Next.Next.Next.Next.Next.Next.First);
                    zip.Add((string)nextlocation.First.Next.Next.Next.Next.Next.Next.Next.Next.Next.Next.Next.First);
                    nextlocation = (JObject)nextlocation.Next;
                }
                if (latitude[0] >= 39.527 && latitude[0] <= 39.528 && longitude[0] <= -99.141 && longitude[0] >= -99.142)
                {
                    return (null);
                }
                location.latitude = latitude;
                location.longitude = longitude;
                location.city = city;
                location.street = street;
                location.state = state;
                location.zip = zip;
                cache.Add(address, location);
            }
            catch
            {
                return (null);
            }
            return(location);
        }