The complete tutorial on embedding and displaying Google Maps in your .NET WinForms application is available on C# Corner by the following link:


http://www.c-sharpcorner.com/article/embed-google-maps-in-net-desktop-application/


This tutorial also explains how to work with Google Maps JavaScript API in DotNetBrowser.


VB.NET

The map.html used in this sample code is the same as described in the article above.


Imports DotNetBrowser
Imports DotNetBrowser.WinForms

Public Class Form1
    Dim browser As Browser
    Dim browserView As BrowserView

    Public Sub New()
        InitializeComponent()
 
        browser = BrowserFactory.Create(BrowserType.LIGHTWEIGHT)
        browserView = New WinFormsBrowserView(browser)
        Me.Controls.Add(browserView)
        browser.LoadURL(CurDir() & "\maps.html")
 
    End Sub

End Class


Adding marker during runtime (e.g. on button click)

    Private Sub AddMarker_Click(sender As Object, e As EventArgs) Handles AddMarker.Click
        Dim map = browser.ExecuteJavaScriptAndReturnValue("map")
        Dim position = browser.ExecuteJavaScriptAndReturnValue(
                              "new google.maps.LatLng(" & Lat.Text & ", "& Lng.Text &")")

        Dim marker = browser.ExecuteJavaScriptAndReturnValue("new google.maps.Marker()")

        marker.AsObject().GetProperty("setPosition").AsFunction().Invoke(marker,position)
        marker.AsObject().GetProperty("setMap").AsFunction().Invoke(marker,map)
    End Sub



C#


using DotNetBrowser;
using DotNetBrowser.WinForms;
using System;
using System.IO;
using System.Windows.Forms;

namespace WindowsFormsApplication8
{
    public partial class Form1 : Form
    {
        private Browser browser;
        private BrowserView browserView;

        public Form1()
        {
            InitializeComponent();
            browser = BrowserFactory.Create(BrowserType.LIGHTWEIGHT);
            browserView = new WinFormsBrowserView(browser);
            Controls.Add((Control)browserView);
            browserView.Browser.LoadURL(Path.GetFullPath("maps.html"));
        }

        private void AddMarker_Click(object sender, EventArgs e)
        {
            JSValue map = browser.ExecuteJavaScriptAndReturnValue("map");
            JSValue position = browser.ExecuteJavaScriptAndReturnValue(
                        "new google.maps.LatLng('Lat.Text', 'Lng.Text')");

            JSValue marker = browser.ExecuteJavaScriptAndReturnValue(
                        "new google.maps.Marker()");

            marker.AsObject().GetProperty("setPosition").AsFunction()
                                    .Invoke((JSObject)marker, position);

            marker.AsObject().GetProperty("setMap").AsFunction()
                                    .Invoke((JSObject)marker, map);
        }
    }
}



Please take into account that there is a specific permission that should be granted to enable geolocation in DotNetBrowser.

This code sample demonstrates how to implement the IPermissionHandler for Geolocation:


VB.NET


Private Class GeolocationPermissionHandler
    Implements IPermissionHandler

    Public Function OnRequestPermission(request As PermissionRequest) As PermissionStatus _
        Implements IPermissionHandler.OnRequestPermission

        If (request.Type = PermissionType.GEOLOCATION) Then
            return PermissionStatus.GRANTED
        End If

        return PermissionStatus.DENIED

    End Function
End Class


browserView.Browser.PermissionHandler = new GeolocationPermissionHandler()



C#


class GeolocationPermissionHandler : IPermissionHandler
{
    public PermissionStatus OnRequestPermission(PermissionRequest request)
    {
        if(request.Type == PermissionType.GEOLOCATION)
        {
            return PermissionStatus.GRANTED;
        }

        return PermissionStatus.DENIED;
    }
}


browserView.Browser.PermissionHandler = new GeolocationPermissionHandler();