Skip to content

Commit

Permalink
Merge pull request #307 from gtt-project/dkastl/issue304
Browse files Browse the repository at this point in the history
Use notification box for reverse geocoding
  • Loading branch information
dkastl authored Jun 3, 2024
2 parents 4b4de0c + 39b2f59 commit 96b22b9
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 3 deletions.
1 change: 1 addition & 0 deletions config/locales/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ de:
clear_map: "Clear map"
geolocation_activated: Geolokalisierung aktiviert
geolocation_deactivated: Geolokalisierung deaktiviert
copied_location_to_clipboard: Standort in die Zwischenablage kopiert
modal:
load: Laden
cancel: Abbrechen
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ en:
linestring: "Line editor"
polygon: "Area editor"
clear_map: "Clear map"
copied_location_to_clipboard: "Copied location to clipboard"
modal:
load: "Load"
cancel: "Cancel"
Expand Down
1 change: 1 addition & 0 deletions config/locales/ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ ja:
linestring: ライン編集
polygon: エリア編集
clear_map: "地図をクリア"
copied_location_to_clipboard: 位置情報をクリップボードにコピーしました
modal:
load: 読み込み
cancel: キャンセル
Expand Down
43 changes: 42 additions & 1 deletion src/components/gtt-client/geocoding/SearchFactory.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,49 @@
// src/components/gtt-client/geocoding/SearchFactory.ts
import { Feature } from 'ol';
import { applyCustomButton } from './CustomButtonMixin';
import SearchGTT from './SearchGTT';
import SearchGoogle from './SearchGoogle';
import SearchNominatim from 'ol-ext/control/SearchNominatim';
import SearchPhoton from 'ol-ext/control/SearchPhoton';

export function createSearchControl(options: any): any {
/**
* Function signature for the handleSelect function.
* @param feature - The selected feature.
* @param reverse - Whether the feature was selected in reverse mode.
* @param options - Additional options.
* @returns void
*/
type HandleSelectFunction = (
feature: Feature,
reverse: boolean,
options?: any
) => void;

/**
* Custom callback for the handleSelect function.
* @param searchControl
* @param handleSelectCallback
* @returns void
*/
function extendHandleSelect(searchControl: any, handleSelectCallback: (response: object) => void): void {
const originalHandleSelect: HandleSelectFunction = searchControl._handleSelect.bind(searchControl);
searchControl._handleSelect = (feature: Feature, reverse: boolean, options?: any): void => {
originalHandleSelect(feature, reverse, options);
handleSelectCallback({
'title': searchControl.getTitle(feature),
'reverse': reverse ? true : false,
// Add any other additional keys here
});
};
}

/**
* Creates a search control instance based on the provider.
* @param options
* @param handleSelectCallback - Custom callback function to handle the selected feature.
* @returns
*/
export function createSearchControl(options: any, handleSelectCallback: (feature: Feature) => void): any {
let searchControl: any;

// Create search control instance based on the provider
Expand Down Expand Up @@ -54,5 +92,8 @@ export function createSearchControl(options: any): any {
// Apply custom button implementation
applyCustomButton(searchControl, options);

// Extend the handleSelect function with the custom callback
extendHandleSelect(searchControl, handleSelectCallback);

return searchControl;
}
25 changes: 23 additions & 2 deletions src/components/gtt-client/init/controls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,25 @@ function setSearchControl(instance: any): void {

// Add the search control if enabled in plugin settings
if (JSON.parse(geocoder.enabled)) {
const searchControl = createSearchControl({
// Custom callback function to handle the selected feature
const handleSelectCallback = (response: any) => {
if (response.reverse) {
// Add copy to clipboard functionality, if available
if (navigator.clipboard) {
// strip htmls from response title
const text = response.title.replace(/<[^>]*>?/gm, '');
navigator.clipboard.writeText(text);
instance.map.notification.show(instance.i18n.control.copied_location_to_clipboard);
}
else {
instance.map.notification.show(response.title, 10000);
console.warn('Clipboard API not supported');
}
}
};

// Options for creating the search control
const options = {
html: '<i class="mdi mdi-map-search-outline"></i>',
html_reverse: '<i class="mdi mdi-map-marker-question-outline"></i>',
title: instance.i18n.control.search_location,
Expand All @@ -46,7 +64,10 @@ function setSearchControl(instance: any): void {
placeholder: instance.i18n.control.search_placeholder,
...geocoder.options
},
});
};

// Create the search control with the custom callback
const searchControl = createSearchControl(options, handleSelectCallback);
instance.map.addControl(searchControl);

// Add a listener for the select event
Expand Down

0 comments on commit 96b22b9

Please sign in to comment.