Skip to content

Commit

Permalink
support [lng, lat] as well as {lng, lat}
Browse files Browse the repository at this point in the history
  • Loading branch information
ciscorn committed Nov 19, 2024
1 parent 37215b4 commit 6480312
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 39 deletions.
2 changes: 0 additions & 2 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
Close #0

### What I did

<!-- Please describe the motivation behind this PR and the changes it introduces. -->

-

### Notes

<!-- If manual testing is required, please describe the procedure. -->

None
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@

![NPM Version](https://img.shields.io/npm/v/svelte-maplibre-gl)

Svelte wrapper for MapLibre GL JS (supports Svlete 5+ only)
[Svelte](https://svelte.dev/) library for using [MapLibre GL JS](https://maplibre.org/maplibre-gl-js/docs/) as reactive components.

⚠️ This project is currently in the early stages of development. Features and APIs are subject to change.

Documentaion and Examples: https://svelte-maplibre-gl.pages.dev/examples/

License: MIT or Apache 2.0

## Installation

Using `svelte-maplibre-gl` requires `svelte >= 5.0.0`.

```bash
npm install --dev svelte-maplibre-gl
```

## License

Licensed under the [MIT License](./LICENSE-MIT.txt) or the [Apache License 2.0](./LICENSE-APACHE.txt), at your option.
Expand All @@ -26,6 +34,8 @@ Everyone is welcomed to contribute to this project! There are many ways to suppo
- TODO: Add core contributors
- And [all contributors](https://github.com/MIERUNE/svelte-maplibre-gl/graphs/contributors)

Supported by [MIERUNE Inc.](https://www.mierune.co.jp/)

## Acknowledgements

This project `svelte-maplibre-gl` is inspired by the efforts and innovations of the following libraries:
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "svelte-maplibre-gl",
"version": "0.0.3",
"version": "0.0.4",
"license": "(MIT OR Apache-2.0)",
"description": "Svelte wrapper for Maplibre GL JS",
"description": "Svelte library for using MapLibre GL JS as reactive components",
"scripts": {
"dev": "vite dev",
"build": "vite build && npm run package",
Expand Down
2 changes: 1 addition & 1 deletion src/content/examples/animate-images/AnimateImages.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
class="h-[60vh] min-h-[300px]"
style="https://basemaps.cartocdn.com/gl/dark-matter-gl-style/style.json"
zoom={5}
center={{ lng: -76, lat: 43 }}
center={[-76, 43]}
>
<GlobeControl />
<ImageSource
Expand Down
4 changes: 2 additions & 2 deletions src/content/examples/complex/Complex.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
let lineColor = $state('#ff00dd');
let lineWidth = $state(1.5);
let circleRadius = $state(3);
let center = $state({ lng: 137.3543, lat: 37.062 });
let center = $state([137.3543, 37.062]);
let zoom = $state(6.0);
let pitch = $state(0);
let bearing = $state(0);
Expand Down Expand Up @@ -122,7 +122,7 @@
</div>
<div class="flex items-center gap-x-4 text-sm">
<pre
class="my-1 grow">{`lat: ${center.lat.toFixed(3)}, lng: ${center.lng.toFixed(3)}, z: ${zoom.toFixed(1)}, pitch: ${pitch.toFixed(1)}, bearing: ${bearing.toFixed(1)}`}</pre>
class="my-1 grow">{`lat: ${center[1].toFixed(3)}, lng: ${center[0].toFixed(3)}, z: ${zoom.toFixed(1)}, pitch: ${pitch.toFixed(1)}, bearing: ${bearing.toFixed(1)}`}</pre>
<pre class="my-1 grow">marker: {`${markerLnglat.lat.toFixed(3)}, ${markerLnglat.lng.toFixed(3)}`}</pre>
<label>
z:
Expand Down
2 changes: 1 addition & 1 deletion src/lib/maplibre/controls/FullScreenControl.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import maplibregl from 'maplibre-gl';
import { getMapContext } from '../contexts.svelte.js';
import { resetEventListener } from '../utils.js';
import type { Listener, Event } from '../common.js';
import type { Listener, Event } from '../types.js';
interface Props extends maplibregl.FullscreenControlOptions {
position?: maplibregl.ControlPosition;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/maplibre/controls/GeolocateControl.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import maplibregl from 'maplibre-gl';
import { getMapContext } from '../contexts.svelte.js';
import { resetEventListener } from '../utils.js';
import type { Listener, Event } from '../common.js';
import type { Listener, Event } from '../types.js';
type GeolocateEvent = Event<maplibregl.GeolocateControl> & Object;
Expand Down
23 changes: 15 additions & 8 deletions src/lib/maplibre/map/MapLibre.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
// https://maplibre.org/maplibre-gl-js/docs/API/classes/Map/
import { onDestroy, type Snippet } from 'svelte';
import maplibregl from 'maplibre-gl';
import maplibregl, { type LngLatLike } from 'maplibre-gl';
import 'maplibre-gl/dist/maplibre-gl.css';
import { prepareMapContext } from '../contexts.svelte.js';
import type { LngLat } from '../common.js';
import { resetEventListener } from '../utils.js';
import { formatLngLat, resetEventListener } from '../utils.js';
type MapEventProps = {
[K in keyof maplibregl.MapEventType as `on${K}`]?: (ev: maplibregl.MapEventType[K]) => void;
Expand All @@ -16,7 +15,7 @@
map?: maplibregl.Map;
class?: string;
inlineStyle?: string;
center?: LngLat;
center?: LngLatLike;
padding?: maplibregl.PaddingOptions;
fov?: number;
Expand Down Expand Up @@ -207,7 +206,12 @@
map.on('move', (ev) => {
if (map) {
const tr = map.transform;
if (!center || center.lat !== tr.center.lat || center.lng !== tr.center.lng) {
if (center) {
const _center = maplibregl.LngLat.convert(center);
if (_center.lat !== tr.center.lat || _center.lng !== tr.center.lng) {
center = formatLngLat(center, tr.center);
}
} else {
center = tr.center;
}
if (tr.zoom !== zoom) {
Expand Down Expand Up @@ -361,9 +365,12 @@
return Math.abs(a - b) > 1e-14;
}
if (center && (notAlmostEqual(tr.center.lat, center.lat) || notAlmostEqual(tr.center.lng, center.lng))) {
jumpTo.center = center;
changed = true;
if (center) {
const _center = maplibregl.LngLat.convert(center);
if (notAlmostEqual(tr.center.lat, _center.lat) || notAlmostEqual(tr.center.lng, _center.lng)) {
jumpTo.center = center;
changed = true;
}
}
if (zoom !== undefined && notAlmostEqual(tr.zoom, zoom)) {
jumpTo.zoom = zoom;
Expand Down
14 changes: 5 additions & 9 deletions src/lib/maplibre/markers/Marker.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
import { onDestroy, type Snippet } from 'svelte';
import maplibregl from 'maplibre-gl';
import { getMapContext, prepareMarkerContext } from '../contexts.svelte.js';
import type { LngLat } from '../common.js';
import { resetEventListener } from '../utils.js';
import { formatLngLat, resetEventListener } from '../utils.js';
interface Props extends Omit<maplibregl.MarkerOptions, 'className'> {
lnglat: LngLat;
lnglat: maplibregl.LngLatLike;
class?: string;
/** HTML content of the marker */
content?: Snippet;
Expand Down Expand Up @@ -77,11 +76,11 @@
marker = new maplibregl.Marker(options);
markerCtx.marker = marker;
marker.setLngLat($state.snapshot(lnglat)).addTo(mapCtx.map);
marker.setLngLat($state.snapshot(lnglat) as maplibregl.LngLatLike).addTo(mapCtx.map);
marker.on('drag', (e) => {
if (marker) {
lnglat = marker.getLngLat();
lnglat = formatLngLat(lnglat, marker.getLngLat());
}
ondrag?.(e);
});
Expand All @@ -101,10 +100,7 @@
$effect(() => {
if (lnglat && !firstRun) {
const prevLnglat = marker?.getLngLat();
if (prevLnglat && (prevLnglat.lat !== lnglat.lat || prevLnglat.lng !== lnglat.lng)) {
marker?.setLngLat(lnglat);
}
marker?.setLngLat(lnglat);
}
});
Expand Down
10 changes: 3 additions & 7 deletions src/lib/maplibre/markers/Popup.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
import { onDestroy, type Snippet } from 'svelte';
import maplibregl from 'maplibre-gl';
import { getMapContext, getMarkerContext } from '../contexts.svelte.js';
import type { LngLat } from '../common.js';
import { resetEventListener } from '../utils.js';
interface Props extends Omit<maplibregl.PopupOptions, 'className'> {
lnglat?: LngLat;
lnglat?: maplibregl.LngLatLike;
class?: string;
/** HTML content of the popup */
content?: Snippet;
Expand All @@ -22,7 +21,7 @@
let container = $state<HTMLElement | null>(null);
let {
lnglat = $bindable(),
lnglat,
class: className = undefined,
offset,
closeButton,
Expand Down Expand Up @@ -87,10 +86,7 @@
$effect(() => {
if (lnglat && !firstRun) {
const prevLnglat = popup?.getLngLat();
if (prevLnglat && (prevLnglat.lat !== lnglat.lat || prevLnglat.lng !== lnglat.lng)) {
popup?.setLngLat(lnglat);
}
popup?.setLngLat(lnglat);
}
});
Expand Down
5 changes: 0 additions & 5 deletions src/lib/maplibre/common.ts → src/lib/maplibre/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
export type LngLat = {
lng: number;
lat: number;
};

export type Listener<T> = (event: T) => void;

export type Event<T, N = string> = {
Expand Down
10 changes: 10 additions & 0 deletions src/lib/maplibre/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,13 @@ export function resetLayerEventListener(
}
};
}

export function formatLngLat(target: maplibregl.LngLatLike, lnglat: maplibregl.LngLat): maplibregl.LngLatLike {
if (Array.isArray(target)) {
return [lnglat.lng, lnglat.lat];
} else if ('lon' in target) {
return { lon: lnglat.lng, lat: lnglat.lat };
} else {
return { lng: lnglat.lng, lat: lnglat.lat };
}
}

0 comments on commit 6480312

Please sign in to comment.