Skip to content

Commit

Permalink
Add scale (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristineguadelupe authored Jan 16, 2024
1 parent 10c5e19 commit 31db285
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 31 deletions.
13 changes: 13 additions & 0 deletions assets/maplibre/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export function init(ctx, data) {
terrain = [],
geocode = [],
fullscreen = [],
scale = [],
hover = [],
center = [],
info = [],
Expand Down Expand Up @@ -54,6 +55,9 @@ export function init(ctx, data) {
fullscreen.forEach(() => {
addFullScreen();
});
scale.forEach((options) => {
addScale(options);
});
hover.forEach((layer) => {
addHover(layer);
});
Expand Down Expand Up @@ -104,6 +108,10 @@ export function init(ctx, data) {
addFullScreen();
});

ctx.handleEvent("add_scale", (options) => {
addScale();
});

ctx.handleEvent("clusters_expansion", (clusters) => {
inspectClusters(clusters);
});
Expand Down Expand Up @@ -168,6 +176,11 @@ export function init(ctx, data) {
map.addControl(fullscreen);
}

function addScale(options) {
const scale = new maplibregl.ScaleControl(options);
map.addControl(scale);
}

function loadImage({ name, url, options }) {
map.loadImage(url, (error, image) => {
if (error) throw error;
Expand Down
62 changes: 31 additions & 31 deletions lib/assets/maplibre/build/main.js

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions lib/kino/maplibre.ex
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,27 @@ defmodule Kino.MapLibre do
update_events(map, :fullscreen, %{})
end

@doc """
Adds a scale control for displaying the ratio of a distance on the map to the corresponding
distance on the ground.
## Options
* `:max_width` - The maximum length of the scale control in pixels. Default: 100
* `:unit` - Unit of the distance (`:imperial`, `:metric` or ':nautical'). Default: `:metric`
## Examples
Kino.MapLibre.add_scale_control(map)
Kino.MapLibre.add_scale_control(map, unit: :nautical)
"""
@spec add_scale_control(maplibre(), keyword()) :: :ok | %__MODULE__{}
def add_scale_control(map, opts \\ []) do
scale = %{options: normalize_opts(opts)}
update_events(map, :scale, scale)
end

@doc """
A helper function to allow inspect a cluster on click. Receives the ID of the clusters layer
## Examples
Expand Down Expand Up @@ -378,6 +399,12 @@ defmodule Kino.MapLibre do
{:noreply, ctx}
end

def handle_cast({:scale, scale}, ctx) do
broadcast_event(ctx, "add_scale", scale)
ctx = update_assigned_events(ctx, :scale, scale)
{:noreply, ctx}
end

def handle_cast({:hover, layer}, ctx) do
broadcast_event(ctx, "add_hover", layer)
ctx = update_assigned_events(ctx, :hover, layer)
Expand Down
28 changes: 28 additions & 0 deletions test/kino/maplibre_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,34 @@ defmodule Kino.MapLibreTest do
end
end

describe "add_scale_control/2" do
test "adds a scale control to a static map" do
ml = Ml.new() |> Kino.MapLibre.add_scale_control()
assert ml.events.scale == [%{options: %{}}]
end

test "adds a scale control to a dynamic map" do
ml = Ml.new() |> Kino.MapLibre.new()
Kino.MapLibre.add_scale_control(ml, unit: :nautical)
data = connect(ml)

assert data.events.scale == [%{options: %{"unit" => :nautical}}]

assert_broadcast_event(ml, "add_scale", %{options: %{"unit" => :nautical}})
end

test "adds a scale control to a converted map" do
ml = Ml.new() |> Kino.MapLibre.add_scale_control() |> Kino.MapLibre.new()

Kino.MapLibre.add_scale_control(ml, max_width: 200)
data = connect(ml)

assert data.events.scale == [%{options: %{"maxWidth" => 200}}, %{options: %{}}]

assert_broadcast_event(ml, "add_scale", %{options: %{"maxWidth" => 200}})
end
end

describe "clusters_expansion/2" do
test "adds a cluster expansion to a static map" do
ml = Ml.new() |> Kino.MapLibre.clusters_expansion("clusters")
Expand Down

0 comments on commit 31db285

Please sign in to comment.