Display Google Maps for an address in C#


[Google Maps]

This example shows how to make Google Maps look up an address and display a map in the system’s default browser.

A Google Maps URL can use the following format.

    http://maps.google.com/maps?q=QUERY&t=TYPE&z=ZOOM

Where:

  • QUERY is the query. If it begins with “loc:” then its latitude+longitude. Otherwise it’s something like an address or place name.
  • TYPE is map type:
    • m = Map
    • k = Satellite
    • h = Hybrid
    • p = Terrain
    • e = Google Earth
  • ZOOM is the zoom level, usually 1 – 20.

The program uses the following code to generate a Google Maps URL.

// Return a Google map URL.
private string GoogleMapUrl(string query, string map_type, int zoom)
{
    // Start with the base map URL.
    string url = "http://maps.google.com/maps?";

    // Add the query.
    url += "q=" + HttpUtility.UrlEncode(query, Encoding.UTF8);

    // Add the type.
    map_type = GoogleMapTypeCode(map_type);
    if (map_type != null) url += "&t=" + map_type;

    // Add the zoom level.
    if (zoom > 0) url += "&z=" + zoom.ToString();

    return url;
}

// Return a Google map type code.
private string GoogleMapTypeCode(string map_type)
{
    // Insert the proper type.
    switch (map_type)
    {
        case "Map":
            return "m";
        case "Satellite":
            return "k";
        case "Hybrid":
            return "h";
        case "Terrain":
            return "p";
        case "Google Earth":
            return "e";
        default:
            return null;
    }
}

The GoogleMapUrl method builds the URL. It starts with the base URL and adds the query.

The method then calls the GoogleMapTypeCode method to convert a string (such as “Terrain”) into a map type code (such as “k”). If the type code isn’t null, and adds the map type code to the URL. If the map type code is null, that means the initial map type parameter was null, blank, or some unrecognized value. In that case, the URL omits the type code and Google Maps uses the default map type.

Next if the zoom factor is greater than 0, the code adds it to the URL. If the value is zero or less, the URL omits the zoom level and you get the default level.

Finally the method returns the resulting URL.

The GoogleMapTypeCode method simply looks up a map type string and returns the appropriate type code.

When you enter an address, select a map type, and click Google, the program uses the following code to display a map in the system’s default browser.

// Display a Google map.
private void btnGoogle_Click(object sender, EventArgs e)
{
    // Get the Google Maps URL with defult zoom.
    string url = GoogleMapUrl(txtAddress.Text, cboGoogle.Text, 0);

    // Display the URL in the default browser.
    Process.Start(url);
}

All this code does is use the GoogleMapUrl method to build a URL. It then calls Process.Start to invoke the URL. That automatically makes the system’s default browser display the URL and Google Maps does the rest.


Download Example   Follow me on Twitter   RSS feed   Donate




This entry was posted in graphics, internet, system and tagged , , , , , , , , , , , , , . Bookmark the permalink.

3 Responses to Display Google Maps for an address in C#

  1. Pingback: Display Google Maps for an address inside a form in C# - C# HelperC# Helper

  2. Keith Dudley says:

    Using C#, how to draw a polygon on google maps?

Leave a Reply

Your email address will not be published. Required fields are marked *