this_algorithm/xpin-wasm/index.js
2023-03-02 18:55:40 -05:00

55 lines
1.5 KiB
JavaScript

// import init, { log_to_address, greet } from "./pkg/xpin_wasm.js";
// init().then(() => {
// console.log(greet());
// });
import init, { address_from_lat_lon, address_to_lat_lon } from "./pkg/xpin_wasm.js";
init().then(() => {
let updateEncode = () => {
if (!encodeAddressLat.value || !encodeAddressLon.value) {
encodeAddressResult.value = "";
encodeAddressError.textContent = "";
return;
}
let lat = parseFloat(encodeAddressLat.value);
let lon = parseFloat(encodeAddressLon.value);
try {
encodeAddressResult.value = address_from_lat_lon(lat, lon);
encodeAddressError.textContent = "";
} catch(e) {
encodeAddressResult.value = "";
encodeAddressError.textContent = e;
}
};
let updateDecode = () => {
let addressString = decodeAddressInput.value.trim();
if (!addressString) {
return;
}
try {
let latLon = address_to_lat_lon(addressString);
decodeAddressError.textContent = "";
decodeAddressLat.value = latLon[0];
decodeAddressLon.value = latLon[1];
} catch(e) {
decodeAddressError.textContent = e;
decodeAddressLat.value = "";
decodeAddressLon.value = "";
}
}
// Bind events
encodeAddressLat.addEventListener("input", updateEncode);
encodeAddressLon.addEventListener("input", updateEncode);
decodeAddressInput.addEventListener("input", updateDecode);
// Run in case either field contains data
updateEncode();
updateDecode();
});