leaflet-control-geocoder
Version:
Extendable geocoder with builtin support for OpenStreetMap Nominatim, Bing, Google, Mapbox, MapQuest, What3Words, Photon, Pelias, HERE, Neutrino, Plus codes
81 lines (72 loc) • 2.51 kB
text/typescript
import * as L from 'leaflet';
import { getJSON } from '../util';
import { IGeocoder, GeocoderOptions, geocodingParams, GeocodingResult, reverseParams } from './api';
export interface NeutrinoOptions extends GeocoderOptions {
userId: string;
}
/**
* Implementation of the [Neutrino API](https://www.neutrinoapi.com/api/geocode-address/)
*/
export class Neutrino implements IGeocoder {
options: NeutrinoOptions = {
userId: '',
apiKey: '',
serviceUrl: 'https://neutrinoapi.com/'
};
constructor(options?: Partial<NeutrinoOptions>) {
L.Util.setOptions(this, options);
}
// https://www.neutrinoapi.com/api/geocode-address/
async geocode(query: string): Promise<GeocodingResult[]> {
const params = geocodingParams(this.options, {
apiKey: this.options.apiKey,
userId: this.options.userId,
//get three words and make a dot based string
address: query.split(/\s+/).join('.')
});
const data = await getJSON<any>(this.options.serviceUrl + 'geocode-address', params);
const results: GeocodingResult[] = [];
if (data.locations) {
data.geometry = data.locations[0];
const center = L.latLng(data.geometry['latitude'], data.geometry['longitude']);
const bbox = L.latLngBounds(center, center);
results[0] = {
name: data.geometry.address,
bbox: bbox,
center: center
};
}
return results;
}
suggest(query: string): Promise<GeocodingResult[]> {
return this.geocode(query);
}
// https://www.neutrinoapi.com/api/geocode-reverse/
async reverse(location: L.LatLngLiteral, scale: number): Promise<GeocodingResult[]> {
const params = reverseParams(this.options, {
apiKey: this.options.apiKey,
userId: this.options.userId,
latitude: location.lat,
longitude: location.lng
});
const data = await getJSON<any>(this.options.serviceUrl + 'geocode-reverse', params);
const results: GeocodingResult[] = [];
if (data.status.status == 200 && data.found) {
const center = L.latLng(location.lat, location.lng);
const bbox = L.latLngBounds(center, center);
results[0] = {
name: data.address,
bbox: bbox,
center: center
};
}
return results;
}
}
/**
* [Class factory method](https://leafletjs.com/reference.html#class-class-factories) for {@link Neutrino}
* @param options the options
*/
export function neutrino(options?: Partial<NeutrinoOptions>) {
return new Neutrino(options);
}