-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from MIERUNE/query-features
Declarative components for querySourceFeatures and queryRenderedFeatures
- Loading branch information
Showing
18 changed files
with
342 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<script lang="ts"> | ||
import { | ||
MapLibre, | ||
GeoJSONSource, | ||
CircleLayer, | ||
Marker, | ||
GlobeControl, | ||
QuerySourceFeatures, | ||
QueryRenderedFeatures | ||
} from 'svelte-maplibre-gl'; | ||
import maplibregl from 'maplibre-gl'; | ||
import * as Tabs from '$lib/components/ui/tabs/index.js'; | ||
let map: maplibregl.Map | undefined = $state(); | ||
let features: maplibregl.MapGeoJSONFeature[] = $state.raw([]); | ||
let mode: 'source' | 'rendered' = $state('source'); | ||
</script> | ||
|
||
<div class="flex h-[55vh] min-h-[300px] gap-x-3"> | ||
<MapLibre | ||
bind:map | ||
class="h-[55vh] min-h-[300px] grow" | ||
style="https://basemaps.cartocdn.com/gl/voyager-gl-style/style.json" | ||
zoom={3} | ||
center={{ lng: 120, lat: 20 }} | ||
> | ||
<GlobeControl /> | ||
<GeoJSONSource | ||
id="earthquakes" | ||
data={'https://maplibre.org/maplibre-gl-js/docs/assets/significant-earthquakes-2015.geojson'} | ||
promoteId="ids" | ||
> | ||
{#if mode == 'source'} | ||
<!-- map.querySourceFeatures() --> | ||
<QuerySourceFeatures bind:features> | ||
{#snippet children(feature: maplibregl.MapGeoJSONFeature)} | ||
{#if feature.geometry.type === 'Point'} | ||
<Marker lnglat={feature.geometry.coordinates as [number, number]} /> | ||
{/if} | ||
{/snippet} | ||
</QuerySourceFeatures> | ||
{/if} | ||
<CircleLayer paint={{ 'circle-color': 'red', 'circle-radius': 4 }}> | ||
{#if mode == 'rendered'} | ||
<!-- map.queryRenderedFeatures() --> | ||
<QueryRenderedFeatures bind:features> | ||
{#snippet children(feature: maplibregl.MapGeoJSONFeature)} | ||
{#if feature.geometry.type === 'Point'} | ||
<Marker lnglat={feature.geometry.coordinates as [number, number]} /> | ||
{/if} | ||
{/snippet} | ||
</QueryRenderedFeatures> | ||
{/if} | ||
</CircleLayer> | ||
</GeoJSONSource> | ||
</MapLibre> | ||
|
||
<!-- List of earthquakes --> | ||
<div class="relative basis-[14em]"> | ||
<Tabs.Root bind:value={mode} class="flex h-full flex-col"> | ||
<Tabs.List class="grid w-full grid-cols-2"> | ||
<Tabs.Trigger value="source">Source</Tabs.Trigger> | ||
<Tabs.Trigger value="rendered">Rendered</Tabs.Trigger> | ||
</Tabs.List> | ||
<Tabs.Content value="source" class="min-h-0 shrink overflow-scroll"> | ||
<ul class="m-0 ml-4 overflow-scroll px-3 text-xs"> | ||
{#each features as feature} | ||
<li>{feature.properties.title}</li> | ||
{/each} | ||
</ul> | ||
</Tabs.Content> | ||
<Tabs.Content value="rendered" class="min-h-0 shrink overflow-scroll"> | ||
<ul class="m-0 ml-4 overflow-scroll px-3 text-xs"> | ||
{#each features as feature} | ||
<li>{feature.properties.title}</li> | ||
{/each} | ||
</ul> | ||
</Tabs.Content> | ||
</Tabs.Root> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
title: Query Features | ||
description: Query source and rendered features to place markers and display a list. | ||
--- | ||
|
||
<script lang="ts"> | ||
import Demo from "./Query.svelte"; | ||
import demoRaw from "./Query.svelte?raw"; | ||
import CodeBlock from "../../CodeBlock.svelte"; | ||
let { shiki } = $props(); | ||
</script> | ||
|
||
<Demo /> | ||
|
||
<CodeBlock content={demoRaw} {shiki} /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Tabs as TabsPrimitive } from "bits-ui"; | ||
import Content from "./tabs-content.svelte"; | ||
import List from "./tabs-list.svelte"; | ||
import Trigger from "./tabs-trigger.svelte"; | ||
|
||
const Root = TabsPrimitive.Root; | ||
|
||
export { | ||
Root, | ||
Content, | ||
List, | ||
Trigger, | ||
// | ||
Root as Tabs, | ||
Content as TabsContent, | ||
List as TabsList, | ||
Trigger as TabsTrigger, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<script lang="ts"> | ||
import { Tabs as TabsPrimitive } from "bits-ui"; | ||
import { cn } from "$lib/utils.js"; | ||
let { | ||
ref = $bindable(null), | ||
class: className, | ||
...restProps | ||
}: TabsPrimitive.ContentProps = $props(); | ||
</script> | ||
|
||
<TabsPrimitive.Content | ||
bind:ref | ||
class={cn( | ||
"ring-offset-background focus-visible:ring-ring mt-2 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-offset-2", | ||
className | ||
)} | ||
{...restProps} | ||
/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<script lang="ts"> | ||
import { Tabs as TabsPrimitive } from "bits-ui"; | ||
import { cn } from "$lib/utils.js"; | ||
let { | ||
ref = $bindable(null), | ||
class: className, | ||
...restProps | ||
}: TabsPrimitive.ListProps = $props(); | ||
</script> | ||
|
||
<TabsPrimitive.List | ||
bind:ref | ||
class={cn( | ||
"bg-muted text-muted-foreground inline-flex h-10 items-center justify-center rounded-md p-1", | ||
className | ||
)} | ||
{...restProps} | ||
/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<script lang="ts"> | ||
import { Tabs as TabsPrimitive } from "bits-ui"; | ||
import { cn } from "$lib/utils.js"; | ||
let { | ||
ref = $bindable(null), | ||
class: className, | ||
...restProps | ||
}: TabsPrimitive.TriggerProps = $props(); | ||
</script> | ||
|
||
<TabsPrimitive.Trigger | ||
bind:ref | ||
class={cn( | ||
"ring-offset-background focus-visible:ring-ring data-[state=active]:bg-background data-[state=active]:text-foreground inline-flex items-center justify-center whitespace-nowrap rounded-sm px-3 py-1.5 text-sm font-medium transition-all focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 data-[state=active]:shadow-sm", | ||
className | ||
)} | ||
{...restProps} | ||
/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<script lang="ts"> | ||
import { onDestroy, type Snippet } from 'svelte'; | ||
import maplibregl from 'maplibre-gl'; | ||
import { getLayerContext, getMapContext } from '../contexts.svelte.js'; | ||
interface Props extends maplibregl.QueryRenderedFeaturesOptions { | ||
map?: maplibregl.Map; | ||
geometry?: maplibregl.PointLike | [maplibregl.PointLike, maplibregl.PointLike]; | ||
features?: maplibregl.MapGeoJSONFeature[]; | ||
children?: Snippet<[maplibregl.MapGeoJSONFeature]>; | ||
} | ||
let { map: givenMap, geometry, features = $bindable([]), children, layers, ...options }: Props = $props(); | ||
features = []; | ||
let trigger = $state(0); | ||
let map = $derived(givenMap || getMapContext().map); | ||
$effect(() => { | ||
map?.on('render', update); | ||
return () => { | ||
map?.off('render', update); | ||
}; | ||
}); | ||
function update() { | ||
trigger++; | ||
} | ||
$effect(() => { | ||
trigger; | ||
if (!map) { | ||
features = []; | ||
return; | ||
} | ||
let _options = { | ||
layers: layers || [getLayerContext()?.id || ''], | ||
...options | ||
}; | ||
let _geometry = geometry; | ||
let queriedFeature = _geometry | ||
? map.queryRenderedFeatures(_geometry, _options) | ||
: map.queryRenderedFeatures(_options); | ||
// sort | ||
queriedFeature.sort((a, b) => { | ||
return String(a.id).localeCompare(String(b.id)); | ||
}); | ||
features = queriedFeature; | ||
}); | ||
onDestroy(() => { | ||
features = []; | ||
}); | ||
</script> | ||
|
||
{#if children}{#each features as feature}{@render children(feature)}{/each}{/if} |
Oops, something went wrong.