Giter Site home page Giter Site logo

mapboxnet's Introduction

MapboxNet

A comprehensive Mapbox map control for WPF and WinForms

Complete vector map client based on Mapbox GL JS built using C#

Features

The map view has almost all the features of Mapbox GL JS, such as:

What is a vector map?

Vector maps are the new generation of maps. Traditional map controls download maps from the internet in form of square image tiles, Vector maps download them in form of vector drawings (like SVG). They are then rendered on the client in realtime according to a style specification.

Vector maps are fast, fluent, scalable, highly customizable, and suitable for offline storage as well.

Examples

Hello World

<Window x:Class="DemosWPF.HelloWorldWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:mapbox="clr-namespace:MapboxNetWPF;assembly=MapboxNetWPF"
        mc:Ignorable="d"
        Title="Hello World" Height="600" Width="800" WindowState="Maximized">
    <Grid>

        <mapbox:Map Name="Map" Center="40.729917, -73.990428" Zoom="12"></mapbox:Map>

    </Grid>
</Window>

Note: Don't forget to write your access token in the code. Without it, some online demos won't work.

Map.AccessToken = accessToken;

Using Offline Raster/Satellite Tiles

MapboxNet comes with a built-in server for serving both vector and raster tiles

// firstly, we create a server using the mbtiles
var server = new MapboxNetCore.MapServer(@"tiles\zurich-raster.mbtiles");
server.GlyphsPath = @"fonts\";
server.Start();

// Creating a custom style that takes our MapServer tiles as a raster source
// Converted from the javascript equivalent using https://jsfiddle.net/aliashrafx/c7pxomjb/39/

var style = new Dictionary<string, object>
{
    ["version"] = 8,
    ["name"] = "Custom style",
    ["sources"] = new Dictionary<string, object>
    {
        ["satellite"] = new Dictionary<string, object>
        {
            ["type"] = "raster",
            ["tileSize"] = 256,
            ["tiles"] = new object[] {
                server.TilesURL, // using tile URL here
            },
        },
    },
    ["layers"] = new object[] {
        new Dictionary<string, object> {
            ["id"] = "satellite",
            ["type"] = "raster",
            ["source"] = "satellite",
            ["minzoom"] = 0,
            ["maxzoom"] = 22,
            ["layout"] = new Dictionary<string, object> {
            },
            ["paint"] = new Dictionary<string, object> {
            },
        },
    },
    ["glyphs"] = server.GlyphsURL,
    ["id"] = "14004506-1129-4d81-9889-800854993041",
};

// You can create a hybrid satellite view as well by using a style that takes both Vector Tiles as Raster Tiles

Map.MapStyle = style;

Note: You can convert your JSON into C# nested code using this script

Using offline vector tiles

You can run MapboxNet completely offline using tile stored in your file system. The demo comes with a aliflux-style.json style that can be customized with MapServer URLs.

// Starting the MapServer on a random port, that serves the vector tiles stored in .mbtiles format

var server = new MapboxNetCore.MapServer(@"tiles\zurich.mbtiles");
server.GlyphsPath = @"fonts\";
server.Start();

// pulling the style json stored as an embedded resource in this project, and decoding it into C# dynamic object

var json = MapboxNetCore.Core.GetEmbeddedResource(this.GetType(), "DemosWPF.aliflux-style.json");
dynamic style = MapboxNetCore.Core.DecodeJsonPlain(json);

// modifying the style a bit so that it uses our tile server as a source

style.sources.openmaptiles.tiles.Add(server.TilesURL);
style.glyphs = server.GlyphsURL;

Map.MapStyle = style;

Note: Be careful in mixing and matching tiles and styles. The version/vendor incompatability may make things disappear.

FAQ

Can I use the map with offline raster/vector tiles?

Yes you can. MapboxNet comes with a built-in tile server for this purpose.

Can I use Google/Bing/Openstreetmaps/etc tiles?

Yes you can. You can create your own style that uses your custom tile URL.

Can I view indoor/outdoor structures in 3D?

Yes you can. This is possible using fill-extrusion layers.

Can I draw line/polygon/image/marker/etc on the map?

Yes you can. This can be done via GeoJSON layers and styles.

How do I specify JSON/GeoJSON in C#?

Please refer to this demo to see how GeoJSON looks like in C#. The code is generated using a JSON to nested C# converter. You can also read JSON as a string and parse it in runtime using MapboxNetCore.Core.DecodeJsonPlain function (demo).

Can I remove the Mapbox logo?

Can be done via RemoveAttribution property, but I won't recommend doing it. If you're using a custom style of your own, the logo won't be visible by default.

What's under the hood?

I'm using CefSharp to emulate Google V8 VM that executes Mapbox GL JS and renders it inside the WPF/WinForms window.

Why is WPF performance a bit slower than WinForms?

CefSharp WPF architecture is built in such a way that each frame is rendered in an external process and copied as a bitmap to the WPF container. There is a thread that aims to improve this issue using DirectX rendering, so stay tuned.

If you can't wait, you may use MapboxNet WinForms component in WPF using WindowsFormsHost control.

Contribution

The project is probably the only pure vector map client in C#/.Net. Therefore, it actively needs some good contribution support. Bug reports, suggestions and pull requests are all welcome. Please submit them to the GitHub issue tracker.

Stay In Touch

For latest releases and announcements, check out my site: aliashraf.net

License

This software is released under the MIT License. Please read LICENSE for information on the software availability and distribution.

Copyright (c) 2019 Ali Ashraf

mapboxnet's People

Contributors

aliflux avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.