Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use notification box for reverse geocoding #307

Merged
merged 1 commit into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/locales/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,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 @@ -121,6 +121,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 @@ -117,6 +117,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 @@ -34,7 +34,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 @@ -44,7 +62,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
Loading