Click here to Skip to main content
14,835,707 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can Google Maps be used in a simple C# Windows Forms Application utilising a WebBrowser control?

As I understand Google Maps API v3 no longer requires a key = I won't need an external dedicated web site & key from Google.

The example given here (http://www.codeproject.com/KB/cs/GpsMapping.aspx) gets pretty close but is displaying the full fat google maps web site instead of a map only version (eg: http://code.google.com/apis/maps/documentation/javascript/examples/map-coordinates.html).

Am I missing an important point regarding simple API v3 integration in C# Forms applications? :confused:
Posted
Comments
Member 10623605 12-Mar-14 6:45am
   
how to integrate google news with windows application?

Many thanks for your help shakil0304003. However, I already came across both articles previously in my search for a solution.

Unfortunately, while the 1st reference talks about a .Net CompactFramework for PDAs and is complex in it's coding, the 2nd reference leaves the same question that I've got unanswered (customised GoogleMaps through WebBrowser control in a C# WinForm application).

Sofar I got to (not so elegant) code versions:
private void button1_Click(object sender, EventArgs e)
{
    // UpdateGoogleMap();
    StringBuilder queryAddress = new StringBuilder();
    // Open locally stored html file in webbroswer control, not very ellegant at all
    queryAddress.Append("C:\\GMtest\\GoogleMapsV3.html");

    //Open Google's example code, this requires the HTML code to be hosted on a web server (not acceptable).
    queryAddress.Append("http://code.google.com/apis/maps/documentation/javascript/examples/map-coordinates.html");

    // Any other possible option? 

    this.webBrowser1.Navigate(queryAddress.ToString());
}
Option two was to directly inject html code (dirty aproach?, everything JScript wise would have to be manually inserted/coded):
<preprivate void="" eventargs="" mode="hold" />{
    object empty = System.Reflection.Missing.Value;
    axWebBrowser1.Navigate("about:blank", ref empty, ref empty, ref empty, ref empty);
    // Manuall inject HTML code instead of using a locally stored file: C:\GMtest\GoogleMapsV3.html 
    setBrowserHtml(" <h1>Hello world!</h1>");
}

Is there any elegant and simple solution to what I want to achieve?
   
v2
You can see this links

Click

Click
   
Below is code, I am using in a project

VB
#Region "GoogleMap"
    Dim GoogleMapLocation As String = ""
    Private Sub LoadGoogleMap(ByVal Location As String)
        If GoogleMapLocation = Location Then Exit Sub
        GoogleMapLocation = Location
        Dim a As String = "" & vbCrLf & _
    " <html>  " & vbCrLf & _
    " <head> " & vbCrLf & _
    " <meta name=""viewport"" content=""initial-scale=1.0, user-scalable=no"" /> " & vbCrLf & _
    "  " & vbCrLf & _
    " <script type=""text/javascript"" src=""http://maps.google.com.mx/maps/api/js?sensor=true&language=en""></script> " & vbCrLf & _
    " <script type=""text/javascript""> " & vbCrLf & _
    "  " & vbCrLf & _
    " var geocoder; " & vbCrLf & _
    " var map; " & vbCrLf & _
    "  " & vbCrLf & _
    "  " & vbCrLf & _
    " function initialize(address) { " & vbCrLf & _
    "  " & vbCrLf & _
        " geocoder = new google.maps.Geocoder();  " & vbCrLf & _
    "  " & vbCrLf & _
        "         var myOptions = {zoom: 16, mapTypeId: google.maps.MapTypeId.ROADMAP}  " & vbCrLf & _
    "  " & vbCrLf & _
        " geocoder.geocode({ 'address': (address ? address : """ & Location & """)}, function (results, status) { " & vbCrLf & _
            " if (status == google.maps.GeocoderStatus.OK) { " & vbCrLf & _
                " map.setCenter(results[0].geometry.location); " & vbCrLf & _
                " var marker = new google.maps.Marker({ " & vbCrLf & _
                    " map: map, " & vbCrLf & _
                    " position: results[0].geometry.location " & vbCrLf & _
                " }); " & vbCrLf & _
            " } else { " & vbCrLf & _
                "  " & vbCrLf & _
            " } " & vbCrLf & _
        " }); " & vbCrLf & _
    "  " & vbCrLf & _
        " map = new google.maps.Map(document.getElementById(""map_canvas""), myOptions); " & vbCrLf & _
    " } " & vbCrLf & _
    "  " & vbCrLf & _
    "  " & vbCrLf & _
    " </script> " & vbCrLf & _
    " </head> " & vbCrLf & _
    " <body onload=""initialize()""> " & vbCrLf & _
    "     <div id="" map_canvas="" style=""></div> " & vbCrLf & _
    "  " & vbCrLf & _
    "   </body> " & vbCrLf & _
    " </html> "
        DisplayHtml(a)
    End Sub

    Private Sub DisplayHtml(ByVal html As String)
        WebBrowser1.Navigate("about:blank")
        If WebBrowser1.Document IsNot Nothing Then
            WebBrowser1.Document.Write(String.Empty)
        End If
        WebBrowser1.DocumentText = html
    End Sub
#End Region
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900