Display Google Maps for the computer’s location in C#


[Google Maps]

This example combines the techniques of the following two posts to display the computer’s current location in Google Maps:

See those posts for additional details and caveats.

When the program starts, it executes the following code.

// The coordinate watcher.
private GeoCoordinateWatcher Watcher = null;

// Create and start the watcher.
private void Form1_Load(object sender, EventArgs e)
{
    // Create the watcher.
    Watcher = new GeoCoordinateWatcher();

    // Catch the StatusChanged event.
    Watcher.StatusChanged += Watcher_StatusChanged;

    // Start the watcher.
    Watcher.Start();
}

This code defines and creates a GeoCoordinateWatcher. It registers an event handler for the watcher’s StatusChanged event and then starts it so it will try to get the computer’s location.

The following code shows the watcher’s StatusChanged event handler.

// The watcher's status has change. See if it is ready.
private void Watcher_StatusChanged(object sender,
    GeoPositionStatusChangedEventArgs e)
{
    if (e.Status == GeoPositionStatus.Ready)
    {
        // Display the latitude and longitude.
        if (Watcher.Position.Location.IsUnknown)
        {
            lblStatus.Text = "Cannot find location data";
        }
        else
        {
            GeoCoordinate location =
                Watcher.Position.Location;
            DisplayMap(
                location.Latitude,
                location.Longitude);
        }
    }
}

If the watcher is ready, the code checks to see if it has a position. If it does, the code passes the computer’s latitude and longitude into the following DisplayMap method to display the computer’s location in Google Maps.

// Display a map for this location.
private void DisplayMap(double latitude, double longitude)
{
    // Emulate Internet Explorer 11.
    SetWebBrowserVersion(11001);

    // Get the Google maps hybrid URL with defult zoom.
    string address = "loc:" +
        latitude.ToString() + "+" +
        longitude.ToString();
    string url = GoogleMapUrl(address, "Hybrid", 0);

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

    // Hide the label and display the map.
    lblStatus.Hide();
    wbrMap.Show();
}

This method calls the SetWebBrowserVersion method to make the program’s WebBrowser control emulate Internet Explorer 11 so it can handle Google Maps. See this post for details.

The code then combines the latitude and longitude into a string of the form “loc:latitude+longitude” and calls the GoogleMapUrl method to compose a URL that passes the latitude and longitude to Google Maps. See this post for information about that method.

The method then calls the WebBrowser control’s Navigate method to make it visit the Google Maps URL. It finishes by hiding the “Working…” label and displaying the WebBrowser.


Download Example   Follow me on Twitter   RSS feed   Donate




About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.

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

One Response to Display Google Maps for the computer’s location in C#

  1. Maxy says:

    It very important and nice work.

Leave a Reply

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