neutrino.ts 2.59 KB
Newer Older
JOE XMG's avatar
update  
JOE XMG committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import * as L from 'leaflet';
import { getJSON } from '../util';
import {
  IGeocoder,
  GeocoderOptions,
  GeocodingCallback,
  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: undefined,
    apiKey: undefined,
    serviceUrl: 'https://neutrinoapi.com/'
  };

  constructor(options?: Partial<NeutrinoOptions>) {
    L.Util.setOptions(this, options);
  }

  // https://www.neutrinoapi.com/api/geocode-address/
  geocode(query: string, cb: GeocodingCallback, context?: any): void {
    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('.')
    });
    getJSON(this.options.serviceUrl + 'geocode-address', params, data => {
      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
        };
      }

      cb.call(context, results);
    });
  }

  suggest(query: string, cb: GeocodingCallback, context?: any): void {
    return this.geocode(query, cb, context);
  }

  // https://www.neutrinoapi.com/api/geocode-reverse/
  reverse(location: L.LatLngLiteral, scale: number, cb: GeocodingCallback, context?: any): void {
    const params = reverseParams(this.options, {
      apiKey: this.options.apiKey,
      userId: this.options.userId,
      latitude: location.lat,
      longitude: location.lng
    });
    getJSON(this.options.serviceUrl + 'geocode-reverse', params, data => {
      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
        };
      }
      cb.call(context, 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);
}