parent
21c751e4a0
commit
ec66934ce3
@ -1,8 +1,13 @@
|
|||||||
|
import { provideHttpClient } from '@angular/common/http';
|
||||||
import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';
|
import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';
|
||||||
import { provideRouter } from '@angular/router';
|
import { provideRouter } from '@angular/router';
|
||||||
|
|
||||||
import { routes } from './app.routes';
|
import { routes } from './app.routes';
|
||||||
|
|
||||||
export const appConfig: ApplicationConfig = {
|
export const appConfig: ApplicationConfig = {
|
||||||
providers: [provideZoneChangeDetection({ eventCoalescing: true }), provideRouter(routes)]
|
providers: [
|
||||||
|
provideZoneChangeDetection({ eventCoalescing: true }),
|
||||||
|
provideRouter(routes),
|
||||||
|
provideHttpClient(),
|
||||||
|
],
|
||||||
};
|
};
|
||||||
|
@ -1,9 +1,59 @@
|
|||||||
import { MatGoogleMapsAutocompleteModule } from '@angular-material-extensions/google-maps-autocomplete';
|
import { CommonModule } from '@angular/common';
|
||||||
import { Component } from '@angular/core';
|
import { Component } from '@angular/core';
|
||||||
|
import {
|
||||||
|
FormBuilder,
|
||||||
|
FormControl,
|
||||||
|
FormGroup,
|
||||||
|
ReactiveFormsModule,
|
||||||
|
} from '@angular/forms';
|
||||||
|
import { AutocompleteService } from '../../services/auto-complete.service';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-add-pin-popup',
|
selector: 'app-add-pin-popup',
|
||||||
imports: [MatGoogleMapsAutocompleteModule],
|
standalone: true,
|
||||||
|
imports: [ReactiveFormsModule, CommonModule],
|
||||||
templateUrl: './add-pin-popup.component.html',
|
templateUrl: './add-pin-popup.component.html',
|
||||||
})
|
})
|
||||||
export class AddPinPopupComponent {}
|
export class AddPinPopupComponent {
|
||||||
|
form: FormGroup;
|
||||||
|
suggestions: any[] = [];
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
private fb: FormBuilder,
|
||||||
|
private autocompleteService: AutocompleteService
|
||||||
|
) {
|
||||||
|
// Define the form structure
|
||||||
|
this.form = this.fb.group({
|
||||||
|
title: [''],
|
||||||
|
description: [''],
|
||||||
|
location: new FormControl(''), // Ensure it's explicitly a FormControl
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handles location input
|
||||||
|
onLocationInput(event: any): void {
|
||||||
|
const query = event.target.value.trim();
|
||||||
|
if (query.length > 2) {
|
||||||
|
this.autocompleteService.getAddressSuggestions(query).subscribe({
|
||||||
|
next: (data) => {
|
||||||
|
this.suggestions = data;
|
||||||
|
},
|
||||||
|
error: (err) => {
|
||||||
|
console.error('Error fetching suggestions:', err);
|
||||||
|
this.suggestions = [];
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
this.suggestions = [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handles suggestion selection
|
||||||
|
selectSuggestion(suggestion: any): void {
|
||||||
|
const locationControl = this.form.get('location');
|
||||||
|
if (locationControl instanceof FormControl) {
|
||||||
|
locationControl.setValue(suggestion.display_name);
|
||||||
|
}
|
||||||
|
this.suggestions = [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -0,0 +1,16 @@
|
|||||||
|
import { TestBed } from '@angular/core/testing';
|
||||||
|
|
||||||
|
import { AutoCompleteService } from './auto-complete.service';
|
||||||
|
|
||||||
|
describe('AutoCompleteService', () => {
|
||||||
|
let service: AutoCompleteService;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
TestBed.configureTestingModule({});
|
||||||
|
service = TestBed.inject(AutoCompleteService);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should be created', () => {
|
||||||
|
expect(service).toBeTruthy();
|
||||||
|
});
|
||||||
|
});
|
@ -0,0 +1,23 @@
|
|||||||
|
import { HttpClient } from '@angular/common/http';
|
||||||
|
import { Injectable } from '@angular/core';
|
||||||
|
import { Observable } from 'rxjs';
|
||||||
|
|
||||||
|
@Injectable({
|
||||||
|
providedIn: 'root',
|
||||||
|
})
|
||||||
|
export class AutocompleteService {
|
||||||
|
private apiUrl = 'https://nominatim.openstreetmap.org/search';
|
||||||
|
|
||||||
|
constructor(private http: HttpClient) {}
|
||||||
|
|
||||||
|
getAddressSuggestions(query: string): Observable<any> {
|
||||||
|
return this.http.get(this.apiUrl, {
|
||||||
|
params: {
|
||||||
|
q: query,
|
||||||
|
format: 'json',
|
||||||
|
addressdetails: '1',
|
||||||
|
limit: '5',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue