Skip to content

Instantly share code, notes, and snippets.

@JoyceBabu
Created May 18, 2022 06:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoyceBabu/84389de1b53ba42198af92500955a46f to your computer and use it in GitHub Desktop.
Save JoyceBabu/84389de1b53ba42198af92500955a46f to your computer and use it in GitHub Desktop.
Location search for Prokerala API
// Visit https://api.prokerala.com/demo/birth-details.php to see the following code in action
const PK_API_CLIENT_ID = '';
(function () {
function loadScript(cb) {
var script = document.createElement('script');
script.src = 'https://client-api.prokerala.com/static/js/location.min.js';
script.onload = cb;
script.async = 1;
document.head.appendChild(script);
}
function createInput(name, value) {
const input = document.createElement('input');
input.name = name;
input.type = 'hidden';
return input;
}
function initWidget(input) {
const form = input.form;
const inputPrefix = input.dataset.location_input_prefix ? input.dataset.location_input_prefix : '';
const coordinates = createInput(inputPrefix +'coordinates');
const timezone = createInput(inputPrefix +'timezone');
form.appendChild(coordinates);
form.appendChild(timezone);
new LocationSearch(input, function (data) {
coordinates.value = `${data.latitude},${data.longitude}`;
timezone.value = data.timezone;
input.setCustomValidity('');
}, {clientId: PK_API_CLIENT_ID, persistKey: `${inputPrefix}loc`});
input.addEventListener('change', function (e) {
input.setCustomValidity('Please select a location from the suggestions list');
});
}
loadScript(function() {
let location = document.querySelectorAll('.prokerala-location-input');
Array.from(location).map(initWidget);
});
})();
@JoyceBabu
Copy link
Author

To use the plugin,

  1. Login to your api.prokerala.com dashboard and create a new client.
  2. Update the constant PK_API_CLIENT_ID on line 2 with your client id.
  3. Add class prokerala-location-input to the input field to which you wish to add the location search functionality.
    <input type="text" name="location" class="prokerala-location-input">
    

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment