Allow encoding and decoding
This commit is contained in:
parent
7b88580fe7
commit
d8958e0645
@ -196,6 +196,12 @@ impl Address<'_> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Test this
|
||||
pub fn to_lat_lon(&self) -> (f64, f64) {
|
||||
let lat_lng = LatLng::from(self.as_cell_id());
|
||||
(lat_lng.lat.deg(), lat_lng.lng.deg())
|
||||
}
|
||||
}
|
||||
|
||||
/// Extracts a version number from a number
|
||||
|
@ -5,14 +5,42 @@
|
||||
<title>hello-wasm example</title>
|
||||
</head>
|
||||
<body>
|
||||
<input id="address-lat" type="text"></input>
|
||||
<input id="address-lon" type="text"></input>
|
||||
<p id="address-result">(None)</p>
|
||||
<fieldset>
|
||||
<label for="encode-address-lat">Latitude</label>
|
||||
<input id="encode-address-lat" type="text" />
|
||||
<br>
|
||||
<label for="encode-address-lon">Longitude</label>
|
||||
<input id="encode-address-lon" type="text" />
|
||||
<br>
|
||||
<label for="encode-address-result">encode-address-result</label>
|
||||
<input disabled id="encode-address-result" />
|
||||
<br>
|
||||
<p type="text" id="encode-address-error"></p>
|
||||
</fieldset>
|
||||
|
||||
<fieldset>
|
||||
<label for="decode-address-input">decode-address-input</label>
|
||||
<input id="decode-address-input" type="text" />
|
||||
<br>
|
||||
<label for="decode-address-lat">Latitude</label>
|
||||
<input disabled id="decode-address-lat" type="text" />
|
||||
<br>
|
||||
<label for="decode-address-lon">Longitude</label>
|
||||
<input disabled id="decode-address-lon" type="text" />
|
||||
<br>
|
||||
<p type="text" id="decode-address-error"></p>
|
||||
</fieldset>
|
||||
|
||||
<script>
|
||||
const addressResult = document.getElementById("address-result");
|
||||
const addressLat = document.getElementById("address-lat");
|
||||
const addressLon = document.getElementById("address-lon");
|
||||
var lta;
|
||||
const encodeAddressResult = document.getElementById("encode-address-result");
|
||||
const encodeAddressError = document.getElementById("encode-address-error");
|
||||
const encodeAddressLat = document.getElementById("encode-address-lat");
|
||||
const encodeAddressLon = document.getElementById("encode-address-lon");
|
||||
|
||||
const decodeAddressInput = document.getElementById("decode-address-input");
|
||||
const decodeAddressError = document.getElementById("decode-address-error");
|
||||
const decodeAddressLat = document.getElementById("decode-address-lat");
|
||||
const decodeAddressLon = document.getElementById("decode-address-lon");
|
||||
</script>
|
||||
<script type="module" src="index.js">
|
||||
</script>
|
||||
|
@ -3,21 +3,49 @@
|
||||
// console.log(greet());
|
||||
// });
|
||||
|
||||
import init, { address_lat_lon } from "./pkg/this_algorithm_wasm.js";
|
||||
import init, { address_from_lat_lon, address_to_lat_lon } from "./pkg/this_algorithm_wasm.js";
|
||||
init().then(() => {
|
||||
let update = () => {
|
||||
let lat = parseFloat(addressLat.value);
|
||||
let lon = parseFloat(addressLon.value);
|
||||
|
||||
if (!(lat || lat == 0.0) && !(lon || lon == 0.0)) {
|
||||
let updateEncode = () => {
|
||||
if (!encodeAddressLat.value || !encodeAddressLon.value) {
|
||||
return;
|
||||
}
|
||||
|
||||
let ret = address_lat_lon(lat, lon);
|
||||
addressResult.textContent = ret;
|
||||
let lat = parseFloat(encodeAddressLat.value);
|
||||
let lon = parseFloat(encodeAddressLon.value);
|
||||
|
||||
try {
|
||||
encodeAddressResult.value = address_from_lat_lon(lat, lon);
|
||||
encodeAddressError.textContent = "";
|
||||
} catch(e) {
|
||||
encodeAddressError.textContent = e;
|
||||
}
|
||||
};
|
||||
|
||||
addressLat.addEventListener("input", update);
|
||||
addressLon.addEventListener("input", update);
|
||||
update();
|
||||
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();
|
||||
});
|
||||
|
@ -1,3 +1,5 @@
|
||||
use std::str::FromStr;
|
||||
|
||||
use this_algorithm::Address;
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
@ -9,13 +11,17 @@ extern "C" {
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn address_lat_lon(lat: f64, lon: f64) -> Result<String, String> {
|
||||
pub fn address_from_lat_lon(lat: f64, lon: f64) -> Result<String, String> {
|
||||
Address::from_lat_lon(lat, lon)
|
||||
.map(|a| a.to_string())
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
// #[wasm_bindgen]
|
||||
// pub fn to_lat_lon(address: &str) -> Result>< {
|
||||
|
||||
// }
|
||||
#[wasm_bindgen]
|
||||
pub fn address_to_lat_lon(addr_str: &str) -> Result<Vec<f64>, String> {
|
||||
Address::from_str(addr_str)
|
||||
.as_ref()
|
||||
.map_err(|e| e.to_string())
|
||||
.map(Address::to_lat_lon)
|
||||
.map(|(lat, lon)| vec![lat, lon])
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user