geocoder-php / Geocoder

The most featured Geocoder library written in PHP.
https://geocoder-php.org
MIT License
3.95k stars 516 forks source link

Store Address In Database #1181

Closed mrelliot69 closed 1 year ago

mrelliot69 commented 1 year ago

So i have a project were i have users and they can define their address using the geocoder. How i can store this address on database so i can then display ?

Anyone have a solution for this?

jbelien commented 1 year ago

Hello @mrelliot69,

This library is only focusing on geocoding and reverse geocoding. Any logic that happens after should be handled by your own application, not by this library.

If you want to store the result, I suggest you check our Address model, you can base your database table structure on it.

mrelliot69 commented 1 year ago

Heyy thanks for the fast reply. My problem right now is that I have the autocomplete on my website, but then when the form is submitted, I need to get the result from the form. Right now I was encoding the latitude and the longitude on a string so the value summited by the form had the latitude and longitude on the value but when I do the reverse query I never get accurate results meaning that the address that I select on the autocomplete doesn't match with the one on the reverse query.

One thing that would solve this was if we get the place_id on the base array of the providers and the location object, but im open to other approaches .

Thanks for the help @jbelien

jbelien commented 1 year ago

Indeed, a same location (longitude, latitude) can have several address, so you can't not be assured you will get the same address as the address that gave you the location. There is no workaround for this, this is how (reverse) geocoding works.

Why don't you store the address AND the location in your form (instead of only the location) ?

Some providers return an address/place identifier but not all of them. Which provider(s) are you using ?

mrelliot69 commented 1 year ago

I'm not sending on form submit the address right away because i need more details from that. I understand that, but having that generally would mean that the ones that don't have the id simply could have that property set to null. I'm using the Mapbox, Arcgis, Nomination.

jbelien commented 1 year ago

I get what you're asking for but this is out of the scope of the Geocoder library because we only provide a layer on top of external APIs ; we don't have any control on what the external APIs are returning as response.

mrelliot69 commented 1 year ago

I dont understand why you say that its out of scope if its a property that the providers have, that is only missing on the location object.

Could you please elaborate on that @jbelien . Sorry for all the questions

mtmail commented 1 year ago

The geocoding library helps you retrieve data from an external API data provider. How you store it and what you do with the data after is up to you.

mrelliot69 commented 1 year ago

@mtmail thats exacly my point, right now the library dosent provide the place_id on the location map, but that id comes on the query, thats why i'm asking why that can't be added since comes in the response from the provider

mtmail commented 1 year ago

At least for Nominatim the place_id is different between servers and not good as permanent id. Not sure about Mapbox. https://nominatim.org/release-docs/latest/api/Output/#place_id-is-not-a-persistent-id

jbelien commented 1 year ago

@mtmail thats exacly my point, right now the library dosent provide the place_id on the location map, but that id comes on the query, thats why i'm asking why that can't be added since comes in the response from the provider

Sorry, I didn't understand that was your request (the title of this ticket is about storing address in database). As said before some providers return an identifier (and we catch it), some providers return an identifier (and we don't catch it ; but it should be easy to add), and some providers don't return any identifier.

In the providers you listed, we do catch the identifier for Mapbox and Nominatim (but as said by @mtmail, you must indeed check Nominatim documentation because OpenStreetMap identifiers are not persistent). For ArcGISOnline, we don't catch the identifier but I don't know if there is one returned by the API.