We help build better software

Friday, May 27, 2016

Embed Google Maps in .NET Desktop Application

The lead developer of the DotNetBrowser Anna Dolbina has shared another useful tutorial on C# Corner. Here is the full text for your convenience: 

If you need to embed and display Google Maps in your .NET WPF or WinForms application, then DotNetBrowser library can help you to implement it without much effort.

DotNetBrowser is a .NET library that allows embedding a Chromium-based web browser control into a WPF or WinForms application to display web pages built with HTML5, CSS3, JavaScript, AJAX etc. The Browser component that displays web pages has two rendering modes, lightweight and heavyweight. Both modes have advantages and limitations, and it’s up to you which one to choose.

In order to embed and display Google Maps in your .NET application you just need to create an instance of the BrowserView, embed it into a Window or any other container and load the https://maps.google.com web page using the LoadURL() method of its Browser property. The following sample demonstrates the most straightforward way of doing this,
  1. using DotNetBrowser;  
  2. using DotNetBrowser.WinForms;  
  3. using System;  
  4. using System.Windows.Forms;  
  5.   
  6. namespace GoogleMapsSample  
  7. {  
  8.     public partial class Form1 : Form  
  9.     {  
  10.         private BrowserView browserView;  
  11.   
  12.         public Form1()  
  13.         {  
  14.             InitializeComponent();  
  15.             browserView = new WinFormsBrowserView();  
  16.             this.Controls.Add((Control)browserView.GetComponent());  
  17.             browserView.Browser.LoadURL("http://maps.google.com");  
  18.   
  19.         }  
  20.     }  
  21. }  


In this example I created a BrowserView instance, embed it into a Form and load https://maps.google.com into the Browser to display Google Maps.

Once you compile and run this sample in Visual Studio you should see the following window:

map

For more guides on using DotNetBrowser in your .NET application see DotNetBrowser Support Site.

What if you need to communicate with the loaded map from your VB.NET or C# code? For example, you may want to provide an ability to zoom in or out from your .NET application. In this case you need to communicate with Google Maps API via JavaScript. DotNetBrowser API will help to execute any JavaScript code on the loaded web page.

To work with the loaded Map you need to use google.maps.Map JavaScript object. Since Google updates https://maps.google.com very often, you may not know how to access this object on the loaded web page. To avoid complications I recommend you to embed Google Maps using your own HTML file. You just need to create the map.html file with the following content:
  1. <!DOCTYPE html>  
  2. <html>  
  3.  <head>  
  4.    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />  
  5.    <style type="text/css">  
  6.      html { height: 100% }  
  7.      body { height: 100%; margin: 0; padding: 0 }  
  8.      #map-canvas { height: 100% }  
  9.    </style>  
  10.    <script type="text/javascript"  
  11.      src="https://maps.googleapis.com/maps/api/js?key=API_KEY&sensor=false">  
  12.    </script>  
  13.    <script type="text/javascript">  
  14.      var map;  
  15.      function initialize() {  
  16.        var mapOptions = {  
  17.          center: new google.maps.LatLng(48.209331, 16.381302),  
  18.          zoom: 4  
  19.        };  
  20.        map = new google.maps.Map(document.getElementById("map-canvas"),  
  21.            mapOptions);  
  22.      }  
  23.      google.maps.event.addDomListener(window, 'load', initialize);  
  24.    </script>  
  25.  </head>  
  26.  <body>  
  27.    <div id="map-canvas"/>  
  28.  </body></html>  
To use this sample HTML, just replace API_KEY with a valid Google API key.

After that, load this map.html file to the Browser:

browserView.Browser.LoadURL("C:\\map.html");

The output should look as follows:

output

Now you can access google.maps.Map JavaScript object and invoke its zoom in/out methods. To change zoom use the map.setZoom (zoom:number) method. 

Let’s try adding the zoom buttons to the sample,
  1. private void button1_Click(object sender, EventArgs e)  
  2.         {  
  3.             if (zoomValue < MAX_ZOOM)  
  4.             {  
  5.                 browserView.Browser.ExecuteJavaScript("map.setZoom(" + ++zoomValue + ")");  
  6.             }  
  7.         }  
  8.   
  9.         private void button2_Click(object sender, EventArgs e)  
  10.         {  
  11.             if (zoomValue > MIN_ZOOM)  
  12.             {  
  13.                 browserView.Browser.ExecuteJavaScript("map.setZoom(" + --zoomValue + ")");  
  14.             }  
  15.         }  
Now you can zoom the map in and out directly from your application, using Google Maps controls:

maps

Complete example source code
  1. using DotNetBrowser;  
  2. using DotNetBrowser.WinForms;  
  3. using System;  
  4. using System.Windows.Forms;  
  5.   
  6. namespace GoogleMapsSample  
  7. {  
  8.     public partial class Form1 : Form  
  9.     {  
  10.         private BrowserView browserView;  
  11.         private int zoomValue = 4;  
  12.   
  13.         public static readonly int MIN_ZOOM = 0;  
  14.         public static readonly int MAX_ZOOM = 21;  
  15.   
  16.         public Form1()  
  17.         {  
  18.             InitializeComponent();  
  19.             browserView = new WinFormsBrowserView();  
  20.             this.Controls.Add((Control)browserView.GetComponent());  
  21.   
  22.             browserView.Browser.LoadURL("C://map.html");  
  23.   
  24.         }  
  25.   
  26.         private void button1_Click(object sender, EventArgs e)  
  27.         {  
  28.             if (zoomValue < MAX_ZOOM)  
  29.             {  
  30.                 browserView.Browser.ExecuteJavaScript("map.setZoom(" + ++zoomValue + ")");  
  31.             }  
  32.         }  
  33.   
  34.         private void button2_Click(object sender, EventArgs e)  
  35.         {  
  36.             if (zoomValue > MIN_ZOOM)  
  37.             {  
  38.                 browserView.Browser.ExecuteJavaScript("map.setZoom(" + --zoomValue + ")");  
  39.             }  
  40.         }  
  41.     }  
  42. }  
If you require more Google Maps functions in your WPF or WinForms application, DotNetBrowser API can help you to invoke other methods of the google.maps.Map JavaScript object and to access all the required functionality.

Useful Links: