Closed darkterminal closed 1 year ago
If you would like to continue contributing to open source and would like to do it with an awesome inclusive community, you should join our GitHub Organisation - we help and encourage each other to contribute to open source little and often :neckbeard:. Any questions let us know.
The Back Story about your Javascript Metaphor
As a software freestyle engineer, you know that location-based features are becoming more and more common in web and mobile applications. However, sometimes it's not enough to simply display a set of coordinates to the user. To provide a better user experience, it's important to translate those coordinates into a human-readable address that users can easily understand. This is where reverse geocoding comes in.
The javascript Story!
Requirements
In order to achieve the functionality I needed, I started by importing two useful tools:
useState
anduseEffect
from the React library, and thereact-geocode
package. I also imported a utility function calledtoObjectCoordinate
which converts a comma-separated string into an object withlat
andlng
keys. With these tools, I was able to create a custom hook calleduseReverseGeocoding
, which accepts two optional parameters for latitude and longitude and returns an address string and two state setters for latitude and longitude.The Utils
The above function takes a string parameter
latLong
, which is a comma-separated string of latitude and longitude values. The function splits the string using the comma as a separator and returns an object with two properties -lat
andlng
.The Hook
The code then defines a custom hook called
useReverseGeocoding
. The hook takes two parameters -lat
andlng
- which are set to null by default. Inside the hook, three state variables are defined using theuseState
hook -address
,latitude
, andlongitude
. The hook also defines an effect that runs whenever latitude or longitude changes. The effect calls thefromLatLng
method of theGeocode
object to get the address from the geographic coordinates. If the call is successful, the address is stored in the address state variable. If there is an error, it is logged to the console.The hook returns an object with three properties -
address
,setLatitude
, andsetLongitude
. Theaddress
property contains the human-readable address obtained from the geographic coordinates. ThesetLatitude
andsetLongitude
functions can be used to set thelatitude
andlongitude
state variables, respectively.The Component
The
FormCustomer
component is a form that allows the user to input customer details, including their coordinate and address. The component utilizes theuseReverseGeocoding
hook to simplify the process of inputting the customer's address. When the user inputs the customer's coordinate, thetoObjectCoordinate
function is used to extract the latitude and longitude values and pass them to thesetLatitude
andsetLongitude
functions. This triggers theuseEffect
hook in theuseReverseGeocoding
function, which performs the reversegeocoding
and updates the address variable. TheAddressReceiver
component is used to update thecustomerAddress
field in the form with the retrieved address.The Result
The Conclusion
Reverse geocoding is an important feature for location-based applications that allows coordinates to be translated into human-readable addresses. The useReverseGeocoding hook and toObjectCoordinate utility function provided in the code snippet make it easy to implement reverse geocoding in a React application. By using these tools, developers can provide a better user experience by displaying easily understandable addresses to their users.
A Javascript demo/repos link
None
PayPal Link for Donation (Javascript Storyteller)
https://www.paypal.me/lazarusalhambra