GithubHelp home page GithubHelp logo

Comments (4)

harpreetkhalsagtbit avatar harpreetkhalsagtbit commented on May 29, 2024

Hi @matadorhernan ,
Would you be using State Name or State Id to get Country?
I can create a function for that. Or you can send a P.R.

from country-state-city.

matadorhernan avatar matadorhernan commented on May 29, 2024

Hey I actually found a way to get those with a bunch of geonames.org API calls , I used behavior subjects to reduce the ammount of calls but the result is neat. I also maped the response objects to fit an interface I have but you can match whatever you need.

I am using Angular 10, and rxjs

import { Injectable } from "@angular/core";
import { Option } from "../Interfaces/components/dropdown";
import { HttpClient } from "@angular/common/http";
import { map } from "rxjs/operators";
import { Observable, BehaviorSubject } from "rxjs";
import { GEONAMES_PATH, GEONAMES_USERNAME } from "./config"; // have http://api.geonames.org and <your username>

@Injectable({
  providedIn: "root",
})
export class CountryService {
  public country$: BehaviorSubject<Option[]> = new BehaviorSubject([]);
  public state$: BehaviorSubject<Option[]> = new BehaviorSubject([]);
  public county$: BehaviorSubject<Option[]> = new BehaviorSubject([]);
  public city$: BehaviorSubject<Option[]> = new BehaviorSubject([]);

  constructor(private readonly http: HttpClient) {
    this.getAll().subscribe((country) => {
      this.country$.next(country);
    });
  }

  public getState(geocode: string) {
    this.getChildren(geocode).subscribe((state) => {
      this.state$.next(state);
    });
  }

  public getCounty(geocode: string) {
    this.getChildren(geocode).subscribe((county) => {
      this.county$.next(county);
    });
  }

  public getCity(geocode: string) {
    this.getChildren(geocode).subscribe((city) => {
      this.city$.next(city);
    });
  }

  private getAll(): Observable<any> {
    return this.http
      .get(
        `${GEONAMES_PATH}/countryInfoJSON?formatted=true&username=${GEONAMES_USERNAME}`
      )
      .pipe(
        map((geonames: any) =>
          geonames.geonames.map((country) => {
            return {
              icon: `/assets/flags/${country.countryCode}.svg`,
              title: country.countryName,
              value: country.geonameId,
            } as Option;
          })
        )
      );
  }

  private getChildren(geocode: string) {
    return this.http
      .get(
        `${GEONAMES_PATH}/childrenJSON?formatted=true&geonameId=${geocode}&username=${GEONAMES_USERNAME}`
      )
      .pipe(
        map((geonames: any) => {
          return geonames && geonames.geonames
            ? geonames.geonames.map((child) => {
                return {
                  icon: "",
                  title: child.name,
                  value: child.geonameId,
                } as Option;
              })
            : null;
        })
      );
  }
}

cuadrosoftPay

from country-state-city.

matadorhernan avatar matadorhernan commented on May 29, 2024

Hi @matadorhernan ,
Would you be using State Name or State Id to get Country?
I can create a function for that. Or you can send a P.R.

I actually meant a county so in the end I get a structure like this country >> state or provinces >> county or subprovinces >> cities villas and other settlements. So since my structure is much different I think I you should keep your lib the same and if you decide to use geonames as data source then you can follow my example, or use the docs at geonames.org

from country-state-city.

harpreetkhalsagtbit avatar harpreetkhalsagtbit commented on May 29, 2024

Thanks for the effort you put in but unfortunately I want to keep it the way it is and I don't want to use any api calls.

from country-state-city.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.