// 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.
Comments