-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ol-search-control): add search control (#309)
closes #306
- Loading branch information
1 parent
3b3464d
commit 4f529f7
Showing
7 changed files
with
203 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# ol-search-control | ||
|
||
> A Search control for OpenLayers. | ||
<script setup> | ||
import SearchControlDemo from "@demos/SearchControlDemo.vue" | ||
</script> | ||
<ClientOnly> | ||
<SearchControlDemo /> | ||
</ClientOnly> | ||
|
||
## Usage | ||
|
||
::: code-group | ||
|
||
<<< ../../../../src/demos/SearchControlDemo.vue | ||
|
||
::: | ||
|
||
## Properties | ||
|
||
### Props from OpenLayers | ||
|
||
Properties are passed-trough from `ol-ext` directly. | ||
Their types and default values can be checked-out [in the official OpenLayers docs](https://viglino.github.io/ol-ext/examples/search/map.control.search.html). | ||
Only some properties deviate caused by reserved keywords from Vue / HTML. | ||
This deviating props are described in the section below. | ||
|
||
### Deviating Properties | ||
|
||
None. | ||
|
||
## Events | ||
|
||
### select | ||
|
||
Emits when selecting an item from the list of search results. | ||
|
||
- **Type**: `SearchEvent` | ||
|
||
```vue | ||
<template> | ||
<ol-map ref="map" style="height: 400px"> | ||
<!-- ... --> | ||
<ol-search-control :autocomplete="autocomplete" @select="select"/> | ||
</ol-map> | ||
</template> | ||
<script setup lang="ts"> | ||
// ... | ||
function select(e: SearchEvent) { | ||
const map: Map = e.target.getMap(); | ||
map.getView().animate({ | ||
center: e.search.pos, | ||
zoom: e.search.zoom, | ||
easing: easeOut, | ||
}); | ||
} | ||
</script> | ||
``` |
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,24 @@ | ||
<template> | ||
<div v-if="false"></div> | ||
</template> | ||
<script setup lang="ts"> | ||
import Search, { type Options } from "ol-ext/control/Search"; | ||
import { useAttrs } from "vue"; | ||
import useControl from "@/composables/useControl"; | ||
import usePropsAsObjectProperties from "@/composables/usePropsAsObjectProperties"; | ||
const emit = defineEmits(["select"]); | ||
const props = withDefaults(defineProps<Options>(), { | ||
className: "ol-search", | ||
}); | ||
const attrs = useAttrs(); | ||
const { properties } = usePropsAsObjectProperties(props); | ||
const { control } = useControl(Search, properties, attrs); | ||
control.value.on("select", (event) => emit("select", event)); | ||
defineExpose({ control }); | ||
</script> |
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,106 @@ | ||
<template> | ||
<ol-map ref="map" style="height: 400px" :controls="[]"> | ||
<ol-view | ||
ref="view" | ||
:center="center" | ||
:zoom="zoom" | ||
:projection="projection" | ||
/> | ||
<ol-tile-layer title="OSM"> | ||
<ol-source-osm /> | ||
</ol-tile-layer> | ||
|
||
<ol-search-control | ||
:autocomplete="autocomplete" | ||
:getTitle="getTitle" | ||
:collapsed="true" | ||
:maxHistory="10" | ||
@select="select" | ||
/> | ||
</ol-map> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
import type { Coordinate } from "ol/coordinate"; | ||
import { type SearchEvent } from "ol-ext/control/Search"; | ||
import { easeOut } from "ol/easing"; | ||
import { ref } from "vue"; | ||
import type { Map } from "ol"; | ||
type City = { | ||
name: string; | ||
pos: Coordinate; | ||
zoom: number; | ||
}; | ||
const center = ref([40, 40]); | ||
const projection = ref("EPSG:4326"); | ||
const zoom = ref(8); | ||
const positions: City[] = [ | ||
{ | ||
name: "Paris", | ||
pos: [2.351828, 48.856578], | ||
zoom: 11, | ||
}, | ||
{ | ||
name: "London", | ||
pos: [-0.1275, 51.507222], | ||
zoom: 11, | ||
}, | ||
{ | ||
name: "Geneve", | ||
pos: [6.149985, 46.200013], | ||
zoom: 13, | ||
}, | ||
{ | ||
name: "Bruxelles", | ||
pos: [4.35, 50.83], | ||
zoom: 12, | ||
}, | ||
{ | ||
name: "Berlin", | ||
pos: [13.383333, 52.516667], | ||
zoom: 12, | ||
}, | ||
{ | ||
name: "Madrid", | ||
pos: [-3.683333, 40.433333], | ||
zoom: 12, | ||
}, | ||
{ | ||
name: "Roma", | ||
pos: [12.48657, 41.888732], | ||
zoom: 12, | ||
}, | ||
]; | ||
function getTitle(f: City) { | ||
return f.name; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
function autocomplete(s: string, callback: (result: City[]) => void) { | ||
const result = []; | ||
for (let i = 0; i < positions.length; i++) { | ||
if (new RegExp(s.replace("*", "") || ".*", "i").test(positions[i].name)) { | ||
result.push(positions[i]); | ||
} | ||
} | ||
/* Return result directly... */ | ||
return result; | ||
/* ...or use the callback function | ||
callback(result); | ||
return false; | ||
*/ | ||
} | ||
function select(e: SearchEvent) { | ||
const map: Map = e.target.getMap(); | ||
map.getView().animate({ | ||
center: e.search.pos, | ||
zoom: e.search.zoom, | ||
easing: easeOut, | ||
}); | ||
} | ||
</script> |
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