nachshon f
nachshon f

Reputation: 3680

How to use npm package ('latlon-geohash') in angular 7

I'm trying to use the latlon-geohash npm package in my Angular 7 app, but when I run it, I get this error...

ERROR TypeError: latlon_geohash__WEBPACK_IMPORTED_MODULE_8__.encode is not a function

Here is my code:

import { Component, OnInit, Output, EventEmitter } from '@angular/core';
import * as gh from 'latlon-geohash';

@Component({
  selector: 'app-account',
  templateUrl: './account.component.html',
  styleUrls: ['./account.component.scss'],
})
export class AccountComponent implements OnInit {

  constructor() {

  }

  ngOnInit() {
     //Here I'm trying to encode my lat and lng to a GeoHash
    console.log(gh.encode(39.36, -76.69, 4))
  }

}

And it doesn't work. However, when I run console.log(gh), I get this...

Module {default: ƒ, __esModule: true, Symbol(Symbol.toStringTag): "Module"}
default: class Geohash
adjacent: ƒ adjacent(geohash, direction)
arguments: (...)
bounds: ƒ bounds(geohash)
caller: (...)
decode: ƒ decode(geohash)
encode: ƒ encode(lat, lon, precision) //HERE IS MY FUNCTION
length: 0
name: "Geohash"
neighbours: ƒ neighbours(geohash)
prototype: {constructor: ƒ}
__proto__: ƒ ()
[[FunctionLocation]]: latlon-geohash.js:11
[[Scopes]]: Scopes[3]
Symbol(Symbol.toStringTag): "Module"
__esModule: true
__proto__: Object

I see my encode function right there. Why doesn't it work? I can't seem to find an answer, but I feel like there may be something really simple I'm missing. Any ideas?

Upvotes: 3

Views: 1033

Answers (2)

nircraft
nircraft

Reputation: 8478

You should add the latlon-geohash to scripts inside angular.json. Any of the 3rd party scripts/libraries need to be made available inside scripts in angular.json. Provide the full relative path of latlon-geohash.js to the angular project so that it becomes available for use.

Make sure you Rebuild your project too.

EX:

"scripts": [
            "./node_modules/path-to-lib/latlon-geohash.js",

Also add the import to your component: import Geohash from 'latlon-geohash'; as mentioned by Tony.

here is a stackblitz link for you to try

Upvotes: 3

Tony
Tony

Reputation: 20132

Try these 2 way to see if it work

import Geohash from 'latlon-geohash';

or

const Geohash = require('latlon-geohash')

Upvotes: 3

Related Questions