Leaflet.js Map in C#

9 Apr
Leaflet in C# on the Desktop

Leaflet in C# on the Desktop

I have been writing some plugins for ArcGIS Desktop Explorer and thought that for my purposes, Leaflet could do this so much faster and so much lighter.  I could write a webpage to load data from all my sources – Sharepoint, SQL Server, CSV – but I don’t know how to make all the connections and all my SharePoint data is acquired through C# already so why not stick with it? I just needed to figure out how to load Leaflet in a C# Form. I used the WebBrowser Tool.

To figure it out, I went to StackOverflow and Atanas Korchev had the answer. I default open to my base Leaflet map with one point. I added the BounceMarker  plugin because it’s cool.  Lastly, I added a print button.

Here is the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MSHTML;
namespace LeafletCsharp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            HtmlElement head = webBrowser1.Document.GetElementsByTagName(“head”)[0];
            HtmlElement scriptEl = webBrowser1.Document.CreateElement(“script”);
            IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
            element.text = “var mymarker; function addPoints() { mymarker = new L.marker([36.104743, -106.629925]); map.addLayer(mymarker); mymarker.bindPopup(‘HELLO <br>Added By C#.’); }”;
            head.AppendChild(scriptEl);
            webBrowser1.Document.InvokeScript(“addPoints”);

           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            HtmlElement head = webBrowser1.Document.GetElementsByTagName(“head”)[0];
            HtmlElement scriptEl = webBrowser1.Document.CreateElement(“script”);
            IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
            element.text = “function removePoints() { map.removeLayer(mymarker); }”;
            head.AppendChild(scriptEl);
            webBrowser1.Document.InvokeScript(“removePoints”);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            webBrowser1.ShowPrintDialog();
        }
    }
}

 

Advertisements

2 Responses to “Leaflet.js Map in C#”

  1. clasense4 April 10, 2013 at 9:28 am #

    Reblogged this on clasense4 blog.

  2. joskid August 19, 2014 at 2:16 am #

    Reblogged this on josephdung.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: