Skip to content

Commit

Permalink
geolocation button
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew44-mappable committed Jun 3, 2024
1 parent f09fabc commit 680eb7b
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions src/controls/MMapRouteControl/MMapWaypointInput/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ export class MMapWaypointInput extends mappable.MMapComplexEntity<MMapWaypointIn
private _suggestComponent?: MMapSuggest;

private _rootElement: HTMLElement;
private _isBottomPosition: boolean;
private _inputEl: HTMLInputElement;
private _indicator: HTMLElement;
private _locationButton: HTMLElement;

private _mapListener: MMapListener;

private _isBottomPosition: boolean;
private _isHoverMode = false;

private get _isInputFocused(): boolean {
Expand Down Expand Up @@ -85,10 +86,10 @@ export class MMapWaypointInput extends mappable.MMapComplexEntity<MMapWaypointIn
this._inputEl.addEventListener('keydown', this._onKeydownInput);
form.appendChild(this._inputEl);

const locationButton = document.createElement('button');
locationButton.classList.add('mappable--route-control_waypoint-input__button');
locationButton.insertAdjacentHTML('afterbegin', locationSVG);
form.appendChild(locationButton);
this._locationButton = document.createElement('button');
this._locationButton.classList.add('mappable--route-control_waypoint-input__button');
this._locationButton.insertAdjacentHTML('afterbegin', locationSVG);
form.appendChild(this._locationButton);

const suggestContainer = document.createElement('mappable');
suggestContainer.classList.add('mappable--route-control_waypoint-input_suggest');
Expand Down Expand Up @@ -171,10 +172,15 @@ export class MMapWaypointInput extends mappable.MMapComplexEntity<MMapWaypointIn
private _onBlurInput = (event: FocusEvent) => {
if (this._isHoverMode) {
this._inputEl.focus();
return;
}
if (event.relatedTarget !== this._suggestComponent.activeSuggest) {
this.removeChild(this._suggestComponent);
}
if (event.relatedTarget === this._locationButton) {
this._getGeolocation();
return;
}
this._indicator.innerHTML = '';
this._indicator.insertAdjacentHTML('afterbegin', emptyIndicatorSVG);
};
Expand Down Expand Up @@ -206,6 +212,21 @@ export class MMapWaypointInput extends mappable.MMapComplexEntity<MMapWaypointIn
}
};

private async _getGeolocation() {
const position = await mappable.geolocation.getPosition();
const text = 'My location';
const feature: SearchResponseFeature = {
properties: {name: text, description: text},
geometry: {type: 'Point', coordinates: position.coords}
};

this._inputEl.value = text;

this._indicator.innerHTML = '';
this._indicator.insertAdjacentHTML('afterbegin', settedIndicator[this._props.type]);
this._props.onSelectWaypoint({feature});
}

private async _search(params: SearchParams, reverseGeocodingCoordinate?: LngLat) {
const searchResult = (await this._props.search?.({params, map: this.root})) ?? (await mappable.search(params));
const feature = searchResult[0];
Expand Down

0 comments on commit 680eb7b

Please sign in to comment.