diff --git a/src/lib.rs b/src/lib.rs index 6e2dd90..34726c6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 diff --git a/this_algorithm-wasm/index.html b/this_algorithm-wasm/index.html index 12548a3..fcf7030 100644 --- a/this_algorithm-wasm/index.html +++ b/this_algorithm-wasm/index.html @@ -5,14 +5,42 @@ hello-wasm example - - -

(None)

+
+ + +
+ + +
+ + +
+

+
+ +
+ + +
+ + +
+ + +
+

+
+ diff --git a/this_algorithm-wasm/index.js b/this_algorithm-wasm/index.js index af725cc..e4196a6 100644 --- a/this_algorithm-wasm/index.js +++ b/this_algorithm-wasm/index.js @@ -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(); }); diff --git a/this_algorithm-wasm/src/lib.rs b/this_algorithm-wasm/src/lib.rs index c695bec..1f9a66f 100644 --- a/this_algorithm-wasm/src/lib.rs +++ b/this_algorithm-wasm/src/lib.rs @@ -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 { +pub fn address_from_lat_lon(lat: f64, lon: f64) -> Result { 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, 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]) +}