dalerka

dalerka

How to handle entry and storage of location info with Phoenix and Ecto?

In my app, when users need to enter a city, state/province and country I’d like to facilitate a better UX with autocomplete/suggestions.

The location data will be used for user profiles and posts, i.e. probably each will have fields like: city, state and country.

Users should be able to start typing some city name and select the one in the correct country and state/province (with the help of fuzzy autosuggest), and the app should infer and store the city, state, and country in separate (atomic) fields.

On the other hand when searching for posts by eg. country the autocomplete box should only suggest a list of countries (fuzzy).

We only need to support English language for these two use cases.

How would you suggest to implement such requirement in 2020 (maybe with liveview)?

What libraries you’ve worked with and/or can suggest?

Would it be simpler to use some client-side JS library an then somehow validate data on server (to avoid non-existent or misspelled names)?

Does it make sense to use some external API (we have no need to do complex geolocation operations involving lat/long)?

What do you think about having a dedicated “Locations” model (table) in DB and use it for relations (eg. has_many) with other models? I’m not sure about performance (eg. for auto-suggest) considering this lib has over 138k cities.

Any advice is much appreciated!

Most Liked

LostKobrakai

LostKobrakai

Many apps have addresses, but I’m not sure how many of those try to validate those given the nature of actual valid addresses being super complex and a list of cities likely being outdated a month after an update.

I made @kip deal with that question for his tzworld library recently. You can have a look here: Tz_world / timezone_boundary_builder update - #5 by LostKobrakai

It’s a tradeoff for access time against space in memory (against space on disk, which you likely have enough of). He used an in memory cache in places to prefilter the list of possible matches completely in memory. Not sure if there’s something like that you could do / or need.

Another option would be using postgres and proper full text search or even elasticsearch. Fuzzy text search requires a lot of details and sometimes one is better of using an existing solution for a problem.

stefanchrobot

stefanchrobot

I’d go with LiveView if you can. Consider actually running the query only after certain amount of characters were typed in. I would not store the definition of cities/countries in the database - just keep it in memory. If you need to query the data by location, I’d suggest adding an index on a string column.

Where Next?

Popular in Questions Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New

Other popular topics Top

WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement