|
| 1 | +<template> |
| 2 | + <ol-map ref="map" style="height: 400px" :controls="[]"> |
| 3 | + <ol-view |
| 4 | + ref="view" |
| 5 | + :center="center" |
| 6 | + :zoom="zoom" |
| 7 | + :projection="projection" |
| 8 | + /> |
| 9 | + <ol-tile-layer title="OSM"> |
| 10 | + <ol-source-osm /> |
| 11 | + </ol-tile-layer> |
| 12 | + |
| 13 | + <ol-search-control |
| 14 | + :autocomplete="autocomplete" |
| 15 | + :getTitle="getTitle" |
| 16 | + :collapsed="true" |
| 17 | + :maxHistory="10" |
| 18 | + @select="select" |
| 19 | + /> |
| 20 | + </ol-map> |
| 21 | +</template> |
| 22 | + |
| 23 | +<script setup lang="ts"> |
| 24 | +import type { Coordinate } from "ol/coordinate"; |
| 25 | +import { type SearchEvent } from "ol-ext/control/Search"; |
| 26 | +import { easeOut } from "ol/easing"; |
| 27 | +import { ref } from "vue"; |
| 28 | +import type { Map } from "ol"; |
| 29 | +
|
| 30 | +type City = { |
| 31 | + name: string; |
| 32 | + pos: Coordinate; |
| 33 | + zoom: number; |
| 34 | +}; |
| 35 | +
|
| 36 | +const center = ref([40, 40]); |
| 37 | +const projection = ref("EPSG:4326"); |
| 38 | +const zoom = ref(8); |
| 39 | +
|
| 40 | +const positions: City[] = [ |
| 41 | + { |
| 42 | + name: "Paris", |
| 43 | + pos: [2.351828, 48.856578], |
| 44 | + zoom: 11, |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "London", |
| 48 | + pos: [-0.1275, 51.507222], |
| 49 | + zoom: 11, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "Geneve", |
| 53 | + pos: [6.149985, 46.200013], |
| 54 | + zoom: 13, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "Bruxelles", |
| 58 | + pos: [4.35, 50.83], |
| 59 | + zoom: 12, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "Berlin", |
| 63 | + pos: [13.383333, 52.516667], |
| 64 | + zoom: 12, |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "Madrid", |
| 68 | + pos: [-3.683333, 40.433333], |
| 69 | + zoom: 12, |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "Roma", |
| 73 | + pos: [12.48657, 41.888732], |
| 74 | + zoom: 12, |
| 75 | + }, |
| 76 | +]; |
| 77 | +
|
| 78 | +function getTitle(f: City) { |
| 79 | + return f.name; |
| 80 | +} |
| 81 | +
|
| 82 | +// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 83 | +function autocomplete(s: string, callback: (result: City[]) => void) { |
| 84 | + const result = []; |
| 85 | + for (let i = 0; i < positions.length; i++) { |
| 86 | + if (new RegExp(s.replace("*", "") || ".*", "i").test(positions[i].name)) { |
| 87 | + result.push(positions[i]); |
| 88 | + } |
| 89 | + } |
| 90 | + /* Return result directly... */ |
| 91 | + return result; |
| 92 | + /* ...or use the callback function |
| 93 | + callback(result); |
| 94 | + return false; |
| 95 | + */ |
| 96 | +} |
| 97 | +
|
| 98 | +function select(e: SearchEvent) { |
| 99 | + const map: Map = e.target.getMap(); |
| 100 | + map.getView().animate({ |
| 101 | + center: e.search.pos, |
| 102 | + zoom: e.search.zoom, |
| 103 | + easing: easeOut, |
| 104 | + }); |
| 105 | +} |
| 106 | +</script> |
0 commit comments