📜 ⬆️ ⬇️

Geography package or library that knows geography well and speaks different languages

In many of the projects in which I participated, there was a need to use geographic data in one form or another. Even the simplest sites usually have a list of countries or cities on any of their pages - stores want to know where to deliver the goods; social networks want to know where the user is from; and so on.

What to go far - even here on Habré there are drop-down lists of countries, states and cities:

image

')
I (like you, for sure) had to store such data (say, a list of countries) many times in arrays, databases, and configuration files. Then it’s tedious to add options in other languages ​​when the project has an alternative language version.

I see several problems at once and I will be only glad if someone disputes this list:



Geographic data is an ideal candidate for a single package. I want to enter one command in my favorite package manager (be it composer, npm or CocoaPods) and immediately get the opportunity to work with geographic names. I want to have something like a trendy http://momentjs.com/ , but about geography. Put one package - closed this page, so to speak.

Surprisingly, such packages do not exist yet, so I started working on my own. For starters, the PHP version. This article is a description of my approach, and the main objectives of the publication can be considered as collecting opinions from other developers; assessment of the need for the relevance of the package.

Proposed Implementation


This seems to me the best API for PHP at the moment:

//     –   //             $planet = new Planet(); // ,     ,       $planet->getCountries()->toArray(); // ,        –  ,      $planet->getCountries()->useShortNames()->toArray(); //      $countries = $planet->getCountries(); $thailand = $countries->find(['code' => 'TH']); $thailand->getStates()->toArray(); //      $thailand->getStates()->setLanguage('ru')->toArray(); //       ( , " "  "") $thailand->getStates()->setLanguage('ru')->inflict('in')->toArray(); //      ?    geonames?   ? $capital = $thailand->getCapital(); $capital->getGeonamesId(); $capital->getLatitude(); $capital->getLongitude(); 


This is enough to add lists from the first image to your site very quickly.

For complete happiness, I would like to:
 //        $russia->find(['zip' => '626430']); //      $planet->find(['zip' => 'EC3R 6DN']); //     $planet->find([ 'latitude' => 51.5078788, 'longitude' => -0.0899208 ]); 


The opinion of people on Habré is important to me, but I’m not going to prevaricate - I started writing a package for PHP without waiting for the reaction of the public. The pilot version is on GitHub , and somewhere in a month there will appear 3-4 first languages ​​and all the basic functionality. If someone wants to participate in the development (especially interested in packages for other languages ​​- JavaScript, Ruby, et cetera), I will be very happy to receive a personal message from you.

Communication with application models


I anticipate questions like “but how, then, to memorize the user's city?”, And the answer here is quite simple - in your database (or where you write data there) use standard identifiers such as ISO 3166-1 codes for countries, GeoNames codes for cities and states. There will be no problems to compare the code with its content and translation, and you will not become tied to this particular package.

Question


The main question, which is the purpose of this article: would you use such a package in your applications? Or would you prefer to continue storing geographic data yourself?

Source: https://habr.com/ru/post/283136/


All Articles