Display a Google map for an address inside a form in C#

This example is similar to Display a Google map for an address in the system's default web browser in C# except it uses the following code to display a map on its form.

// Display a Google map.
private void btnGoogle_Click(object sender, EventArgs e)
{
    // The basic map URL without the address information.
    const string url_base =
        "http://maps.google.com/maps?f=q&hl=en&geocode=&" +
        "time=&date=&ttype=&q=@ADDR@&ie=UTF8&t=@TYPE@";

    // URL encode the street address.
    string addr = txtAddress.Text;
    addr = HttpUtility.UrlEncode(txtAddress.Text, System.Text.Encoding.UTF8);

    // Insert the encoded address into the base URL.
    string url = url_base.Replace("@ADDR@", addr);

    // Insert the proper type.
    switch (cboGoogle.Text)
    {
        case "Map":
            url = url.Replace("@TYPE@", "m");
            break;
        case "Satellite":
            url = url.Replace("@TYPE@", "h");
            break;
        case "Terrain":
            url = url.Replace("@TYPE@", "p");
            break;
    }

    // Display the URL in the WebBrowser control.
    wbrMap.Navigate(url);
}

The code first creates a Google Map URL representing the desired address. See the previous example for information about how that works.

The code then calls the Navigate method of the form's WebBrowser control to display the map in the control.

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.