ultimap
Version:
Universal map api
64 lines (53 loc) • 2.07 kB
text/typescript
import { Api } from './utils/ymaps';
import { IGeocodeResult, IGeocoderStrategy } from '../interface/geocoder';
import { Coords, tCoords } from '../../coords';
export class YandexGeocoderStrategy implements IGeocoderStrategy {
public whatAt(coords: tCoords): Promise<IGeocodeResult> {
return new Promise((
resolve: (result: IGeocodeResult) => void,
reject: (message?: string) => void,
) => {
if (!Api.ymaps) {
reject('Yandex maps script not found');
return;
}
// @TODO implements method
reject('Method not implemented');
});
}
public whereIs(address: string, coords?: tCoords): Promise<IGeocodeResult> {
return new Promise((
resolve: (result: IGeocodeResult) => void,
reject: (message?: string) => void,
) => {
if (!Api.ymaps) {
reject('Yandex maps script not found');
return;
}
const params: {
boundedBy?: [[number, number], [number, number]],
} = {};
if (coords) {
const center = new Coords(coords);
params.boundedBy = center.getBounds().toArray();
}
Api.ymaps.geocode(address, params)
.then((res: any) => {
if (!res || !res.geoObjects || !res.geoObjects.each) {
reject('Empty geocoder response');
return;
}
const elements: IGeocodeResult[] = [];
res.geoObjects.each((obj: any) => {
elements.push({
address: obj.properties.get('name'),
coords: new Coords(obj.geometry.getCoordinates()),
});
});
resolve(elements[0] || null);
}, () => {
reject();
});
});
}
}