/
githubmirror
/
components
Обзор
Документация
Войти
/
githubmirror
/
components
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/google-maps/map-geocoder/map-geocoder.ts
56 строк
2 KB
Kristiyan Kostadinov
refactor(google-maps): switch to service decorator
25 апр 2026, 23:10
25 апр 2026, 23:10
132eefd
Код
Авторство
О чём код?
/** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.dev/license */ import {Service, NgZone, inject} from '@angular/core'; import {Observable} from 'rxjs'; export interface MapGeocoderResponse { status: google.maps.GeocoderStatus; results: google.maps.GeocoderResult[]; } /** * Angular service that wraps the Google Maps Geocoder from the Google Maps JavaScript API. * See developers.google.com/maps/documentation/javascript/reference/geocoder#Geocoder */ @Service() export class MapGeocoder { private readonly _ngZone = inject(NgZone); private _geocoder: google.maps.Geocoder | undefined; /** * See developers.google.com/maps/documentation/javascript/reference/geocoder#Geocoder.geocode */ geocode(request: google.maps.GeocoderRequest): Observable<MapGeocoderResponse> { return new Observable(observer => { this._getGeocoder().then(geocoder => { geocoder.geocode(request, (results, status) => { this._ngZone.run(() => { observer.next({results: results || [], status: status as google.maps.GeocoderStatus}); observer.complete(); }); }); }); }); } private _getGeocoder(): Promise<google.maps.Geocoder> { if (!this._geocoder) { if (google.maps.Geocoder) { this._geocoder = new google.maps.Geocoder(); } else { return google.maps.importLibrary('geocoding').then(lib => { this._geocoder = new (lib as google.maps.GeocodingLibrary).Geocoder(); return this._geocoder; }); } } return Promise.resolve(this._geocoder); } }