Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Display Addresses from a CSV File in Google Maps within a WebBrowser Control (C# Winforms)

0.00/5 (No votes)
21 Jul 2016 1  
Using a WebBrowser control and a DataGridView control, open CSV files that contain address information, and display the selected address in a Google Map from the grid in the browser control

Travel Around the World, One Double-Click at a Time

This tip will show you how to use a WebBrowser control and a DataGridView control to open CSV files that contain address information, and display the selected address (one at a time) from the grid in the browser control.

First, drop four controls on your form: a button, an OpenFileDialog, a DataGridView, and a WebBrowser.

Name them, respectively: btnRun, ofd, dgv, and webBrowser1. Of course, you could really name them just about whatever you want, but then you'd have to adjust the code snippets below accordingly.

After all that heavy lifting, feel free to take a break...

Now that you're back, the code follows, in snippets for the various pieces you'll need to add.

First, create a class to mirror the CSV file (which should contain data in the format Address, City, State, Zip):

public class AddressClass
{
    public string CSV_Address { get; set; }
    public string CSV_City { get; set; }
    public string CSV_State { get; set; }
    public string CSV_Zip { get; set; }
}

Now add this at the top of the form:

private List<addressclass> addressList;

The button click event:

private void btnRun_Click(object sender, EventArgs e)
{
    if (LoadAddresses())
    {
        Popul8DGV();
    }
}

Add the positively puny method just called:

private void Popul8DGV()
{
    dgv.DataSource = addressList;
}

...and the more substantial one:

private bool LoadAddresses()
{
    bool addressesLoaded = false;

    ofd.Title = "Open CSV File";
    ofd.Filter = "CSV files|*.csv";
    ofd.InitialDirectory = @"C:\Direcciones";
    if (ofd.ShowDialog() == DialogResult.OK)
    {
        string filename = ofd.FileName;
        string[] filelines = File.ReadAllLines(filename);

        addressList = new List<addressclass>();
        AddressClass address;

        foreach (string s in filelines)
        {
            address = new AddressClass();
            string[] fileline = s.Split(',');
            address.CSV_Address = fileline[0];
            address.CSV_City = fileline[1];
            address.CSV_State = fileline[2];
            address.CSV_Zip = fileline[3];

            if (string.IsNullOrWhiteSpace(address.CSV_Address)) continue;
            if (address.CSV_Address.Equals("Address1")) continue;
            addressList.Add(address);
            addressesLoaded = true;
        }
    }
    return addressesLoaded;
}

Finally, the DatatGridView's doubleclick event:

private void dgv_CellDoubleClick(object sender, DataGridViewCellEventArgs dgvargs)
{
    const int ADDRESS_COL = 0;
    const int CITY_COL = 1;
    const int STATE_COL = 2;
    const int ZIP_COL = 3;

    int row = dgvargs.RowIndex;

    string addr = dgv.Rows[row].Cells[ADDRESS_COL].Value.ToString();
    string city = dgv.Rows[row].Cells[CITY_COL].Value.ToString();
    string st8 = dgv.Rows[row].Cells[STATE_COL].Value.ToString();
    string zip = dgv.Rows[row].Cells[ZIP_COL].Value.ToString();

    // This is what's generated for the URL when I enter my address in google maps:
    // https://www.google.com/maps/place/412+Dela+Vina+Ave,+Monterey,+CA+93940/@36.5987412,-121.8638099
    // ...but not all that is needed; this also works:
    //https://www.google.com/maps/place/412+Dela+Vina+Ave,+Monterey,+CA+93940/
    string urlStr = string.Format(@"https://www.google.com/maps/place/{0},+{1},+{2}+{3}/", 
                                  addr, city, st8, zip);
    webBrowser1.Url = new Uri(urlStr);
}

That should be "all she wrote".

Sample Data and Recourse for the IE-Bitten

If you just want to try it out quickly, save these handful of sample addresses to a file named whateverYouWant.csv:

Scenic Valley Road, Jenny Lind, CA, 95252
Prindle Road,Mokelumne Hill,CA,95245
Hillside Apartments,San Andreas,CA,95249
412 Dela Vina,Monterey,CA,93940
523 Ramona Avenue,Monterey,CA,93940
40 Lower Ragsdale,Monterey,CA,93940

If you eventually run into a problem where the WebBrowser scolds you for using an old fuddy-duddy browser (the WebBrowser control is a wrapper for Internet Explorer), Google or Bing "How can I get the WebBrowser component to use a browser other than Internet Explorer 11?" and you will find the accepted answer on how to deal with this problem.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here