[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Display Google Maps for an address in C#

[Display Google Maps for an address in C#]

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 the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.