Comments (4)
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.
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;
})
);
}
}
from country-state-city.
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.
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)
- Get a single city HOT 2
- Other Language HOT 1
- Duplicate City name 'Amod' and 'Amod,' in Gujarat, India HOT 1
- Missing some state / cities from countries HOT 3
- Incorrect latitude, longitude for Delta State of Nigeria
- Reduce bundle size for the library HOT 4
- Incorrect and missing cities in Finland
- It Takes 17MB in Build File
- Where is the data from?
- City.getAllCities() not working
- return empty
- United Kingdom showing list of cities in place of states
- How to use with cdn?
- Philippines Laguna does not have Cities
- getCitiesOfCountry
- I'm using Angular version 17 and the plugin works fine on laptop / desktop, but then on iphone it causes a white screen
- Bundle Size HOT 1
- cities of american samoa: missing
- Turkey country name changed to Türkiye
- Hongkong has no city and not regarded as a city
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from country-state-city.