Created
May 18, 2022 06:07
-
-
Save JoyceBabu/84389de1b53ba42198af92500955a46f to your computer and use it in GitHub Desktop.
Location search for Prokerala API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To use the plugin,
PK_API_CLIENT_ID
on line 2 with your client id.prokerala-location-input
to the input field to which you wish to add the location search functionality.