GithubHelp home page GithubHelp logo

gampleman / elm-mapbox Goto Github PK

View Code? Open in Web Editor NEW
60.0 7.0 17.0 1.72 MB

MapboxGL bindings for Elm

Home Page: https://package.elm-lang.org/packages/gampleman/elm-mapbox/latest/

License: MIT License

JavaScript 10.79% Elm 89.13% HTML 0.08%
elm mapbox-gl elm-mapbox maps

elm-mapbox's Introduction

elm-mapbox

Great looking and performant maps in Elm using MapboxGl. Discuss in #maps on the Elm Slack.

High Quality Mapping in Elm

There have been some attempts to make native elm mapping packages. However, Mapbox offers a very complex solution that offers some killer features that are difficult to reproduce:

  • client side high quality cartography
  • high performance with large datasets

The way this works, the map accepts a configuration object called a style. The main thing in a style is a list of layers. Layers control what you see on the screen. Their order controls their layering (duh). Each layer references a data source and has a list of properties. Properties are a bit like CSS for maps in the sense that you can use them to specify colors, line thickness, etc. However, unlike CSS, the values that you pass to these use are expressions in a little language, that allows you to style based on other factors like the map's zoom level or actual data in any of the features being styled.

Sources specify how to get the data that powers the layers. Multiple layers can take in a single source.

This library allows you to specify the style declaratively passing it into a specific element in your view function. However, the map element holds some internal state: mostly about the position of the viewport and all the event handling needed to manipulate it. In my experience this is mostly what you want - the default map interactions tend to be appropriate. So this library includes commands that tell the map to modify its internal state (including stuff like animations etc).

How does this Work?

This is a hybrid library that consists both of Elm and JavaScript parts. It uses a combination of ports and custom elements for communication between them.

Getting Started

To get going, install the package and the accompanying npm library:

elm install gampleman/elm-mapbox
npm install --save elm-mapbox

Microsoft Edge needs a polyfill to use custom elements. The polyfill provided by webcomponents.org is known to work https://github.com/webcomponents/custom-elements:

npm install --save @webcomponents/custom-elements

Then include the library into your page. How exactly to do this depends on how you are building your application. We recommend using Parcel, since it is super easy to setup. Then you will want to make your index.js look something like this:

// polyfill for custom elements. Optional, see https://caniuse.com/#feat=custom-elementsv1
import "@webcomponents/custom-elements";

import { registerCustomElement, registerPorts } from "elm-mapbox";

// This brings in mapbox required CSS
import "mapbox-gl/dist/mapbox-gl.css";

// Your Elm application
import { Elm } from "./src/Main.elm";

// A Mapbox API token. Register at https://mapbox.com to get one of these. It's free.
const token =
    "pk.eyJ1Ijovm,vedfg";

// This will add elm-mapbox custom element into the page's registry.
// This **must** happen before your application attempts to render a map.
registerCustomElement({
    token
});

// Initialize your Elm application. There are a few different ways
// to do this, whichever you choose doesn't matter.
var app = Elm.Main.init({ flags: {} });

// Register ports. You only need to do this if you use the port integration.
// I usually keep this commented out until I need it.
registerPorts(app);

Next, optionally, setup a ports module. The best way to do this is to to copy this file into your project. I usually name it Map/Cmd.elm This will allow you to easily use the commands to control parts of your map interactions imperatively - for example you can command your map to fly to a particular location.

Finally, you will need to setup a base style. You can copy some of the example styles, or you can use the (beta) Style code generator in conjunction with Mapbox Studio.

Example

See Example01 for an example application.

Support

This library is supported in all modern browsers. The elmMapbox library has a supported function that can be injected via flags:

import {supported} from "elm-mapbox";

var app = Elm.MyApp.fullscreen({
  mapboxSupported: supported({
    // If  true , the function will return  false if the performance of
    // Mapbox GL JS would be dramatically worse than expected (e.g. a
    // software WebGL renderer would be used).
    failIfMajorPerformanceCaveat: true
  })
});

Customizing the JS side

The registerCustomElement function accepts an options object that takes the following options:

  • token: the Mapbox token. If you don't pass it here, you will need to use the token Elm attribute.
  • onMount a callback that gives you access to the mapbox instance whenever a map gets instantiated. Mostly useful for registering plugins.

Furthermore, the elm-mapbox element exposes its internal mapboxgl.js reference as a map property, which you can use if necessary (although, worth mentioning on slack if you are needing to do this).

The registerPorts function accepts an option object that takes the following options:

  • easingFunctions: an object whose values are easing functions (i.e. they take a number between 0..1 and return a number between 0..1). You can refer to these with the easing option in the Cmd.Option module.

License

(c) Jakub Hampl 2018, 2019

MIT License

elm-mapbox's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar gampleman avatar pjsier avatar purcell avatar rofrol avatar rrbutani avatar vizowl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

elm-mapbox's Issues

Plus with more than two expressions

Hi all - I've run up against a minor wall and I was hoping to get some insight.

According to the Mapbox docs for the + expression, it can sum more than two arguments. https://docs.mapbox.com/mapbox-gl-js/style-spec/expressions/#+

["+", number, number, ...]: number

However, the plus operator in this library only takes in two arguments. I was really hoping to get this working with arbitrary lists.

Looking at the source for this library, it doesn't seem too difficult to write, eg:

plusMany : List (Expression exprType Float) -> Expression exprType Float
plusMany =
    calln "+"

However, calln isn't exposed so I'm unable to write this in my own projects! Any other potential solutions to this that I'm missing?

example styles: major city names not visible

Hi! First of all, thanks for your amazing work, setting up the library was pretty nice, and it's so much fun to be able to play with maps in elm!

I noticed a problem with all of the example styles, as well as custom generated ones: major cities fail to show up. I've attached a screenshot of an example. There is a hole where you'd expect a label for "Sydney" This happens to be the Outdoor style, but it's a problem with every single one I tried. I'm more than happy to dig down a bit more, let me know how I can help. I'm just now learning both Mapbox and your library.

Note that when I convert the Mapbox basic template, the same problem shows up. In mapbox studio itself, the map seems fine, cf second screenshot.

Screenshot 2019-09-19 at 10 42 12

Screenshot 2019-09-19 at 10 39 14

Subscribing to Changes

Hi, great library! I wanted to ask, what is the best way to subscribe to changes at the map? For example if I would like to change the URL accordingly, when scrolling? I mean, getting the new bounding boxes and the zoom factor. At the moment I use subscriptions on mousemove and check, if the center position has changed, but that does not work for scrolling... I guess there is a better way...

Thanks in advance...

Layer.isVisible error at runtime

Setting Layer.visible (E.bool True) on any layer compiles, but gives a runtime error:

Error: layers[1].layout.visibility: expected one of [visible, none], ["case",true,"visible","none"] found

No events firing

Hi! I'm building a SPA with Elm 0.19 where I'm using 2 js plugins: Swiper and Mapbox.
I wanted to simplify the integration through custom-elements so I found this amazing plugin!
I successfully tried the examples but when I try to integrate plugin in one view it renders correctly but no events are fired. Attributes like maxZoom are working correctly.
I have followed R. Feldman SPA example (https://github.com/rtfeldman/elm-spa-example). All page messages are correctly mapped in main view except mapbox-plugin messages.
At runtime there are correctly 2 attached map-element events (click, mouseOver) and if I manually add one it works correctly.

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.