Start consolidating encoded addresses
This commit is contained in:
parent
3abb322ad1
commit
0ea0952840
@ -1,6 +1,21 @@
|
||||
import init from 'xpin-wasm';
|
||||
import * as xpinWasm from 'xpin-wasm';
|
||||
|
||||
export const emptyxpin = {
|
||||
address: '',
|
||||
latLon: [0.0, 0.0]
|
||||
};
|
||||
|
||||
export function getxpin(xpin) {
|
||||
if (!xpin) {
|
||||
return;
|
||||
}
|
||||
return {
|
||||
address: xpin.get_address(),
|
||||
latLon: xpin.get_lat_lon()
|
||||
};
|
||||
}
|
||||
|
||||
export const WasmStatus = {
|
||||
NotLoaded: -1,
|
||||
Loaded: 0,
|
||||
|
@ -4,8 +4,8 @@
|
||||
import { onMount, onDestroy } from 'svelte';
|
||||
import AddressInput from '$lib/AddressInput.svelte';
|
||||
import { browser } from '$app/environment';
|
||||
// import tick from 'svelte';
|
||||
import Error from './Error.svelte';
|
||||
import { emptyxpin, getxpin } from '$lib/common.js';
|
||||
|
||||
let leaflet;
|
||||
import Map from '$lib/Map.svelte';
|
||||
@ -13,7 +13,9 @@
|
||||
let map;
|
||||
|
||||
let latlng = { lat: '', lng: '' };
|
||||
let addr = '';
|
||||
let addr = emptyxpin;
|
||||
let addrInputValue = '';
|
||||
|
||||
let wasm = {
|
||||
status: WasmStatus.NotLoaded,
|
||||
error: undefined,
|
||||
@ -25,11 +27,17 @@
|
||||
|
||||
try {
|
||||
latlng = e.latlng;
|
||||
addr = wasm.call.address_from_lat_lon(latlng.lat, latlng.lng);
|
||||
popup.setLatLng(e.latlng).setContent(`${addr}`).openOn(map);
|
||||
addr = getxpin(wasm.call.EncodedAddress.from_lat_lon(latlng.lat, latlng.lng));
|
||||
console.log(latlng, addr);
|
||||
popup.setLatLng({
|
||||
lat:addr.latLon[0],
|
||||
lng:addr.latLon[1]
|
||||
}).setContent(`${addr.address}`).openOn(map);
|
||||
// popup.setLatLng(e.latlng).setContent(`${addr.address}`).openOn(map);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
addr = '';
|
||||
// addr = emptyxpin;
|
||||
addrInputValue = '';
|
||||
popup.setLatLng(e.latlng).setContent(`You clicked at ${e.latlng}`).openOn(map);
|
||||
}
|
||||
};
|
||||
@ -62,7 +70,7 @@
|
||||
const input = () => {
|
||||
try {
|
||||
if (browser) {
|
||||
outputValue = wasm.call.address_to_lat_lon(addr);
|
||||
addr = getxpin(wasm.call.EncodedAddress.from_address(addr));
|
||||
let latlng = new leaflet.LatLng(outputValue[0], outputValue[1]);
|
||||
map.panTo(latlng, 20);
|
||||
leaflet.popup().setLatLng(latlng).setContent(`${outputValue}<br>hi`).openOn(map);
|
||||
@ -91,9 +99,9 @@
|
||||
{#await init()}
|
||||
<p class="text-lg js-only">Loading WebAssembly module...</p>
|
||||
{:then}
|
||||
<p>Current cursor: {addr} => ({latlng.lat}, {latlng.lng})</p>
|
||||
<p>Current cursor: {addr.address} ({addr.latLon}) => ({latlng.lat}, {latlng.lng})</p>
|
||||
|
||||
<AddressInput bind:value={addr} on:input={input} />
|
||||
<AddressInput bind:value={addrInputValue} on:input={input} />
|
||||
|
||||
<Map bind:map {onMapClick} />
|
||||
{:catch message}
|
||||
|
@ -1,25 +1,51 @@
|
||||
use std::str::FromStr;
|
||||
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
// #[wasm_bindgen]
|
||||
// extern "C" {
|
||||
// #[wasm_bindgen(js_namespace = console, js_name = log)]
|
||||
// fn log_str(s: &str);
|
||||
// }
|
||||
use xpin::Address;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn address_from_lat_lon(lat: f64, lon: f64) -> Result<String, String> {
|
||||
xpin::Address::from_lat_lon(lat, lon)
|
||||
.map(|a| a.to_string())
|
||||
.map_err(|e| e.to_string())
|
||||
pub struct EncodedAddress {
|
||||
address: String,
|
||||
pub lat: f64,
|
||||
pub lon: f64,
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn address_to_lat_lon(addr_str: &str) -> Result<Vec<f64>, String> {
|
||||
xpin::Address::from_str(addr_str)
|
||||
.as_ref()
|
||||
.map_err(|e| e.to_string())
|
||||
.map(xpin::Address::as_lat_lon)
|
||||
.map(|(lat, lon)| vec![lat, lon])
|
||||
impl EncodedAddress {
|
||||
#[wasm_bindgen]
|
||||
pub fn from_lat_lon(lat: f64, lon: f64) -> Result<EncodedAddress, String> {
|
||||
xpin::Address::from_lat_lon(lat, lon)
|
||||
.as_ref()
|
||||
.map(EncodedAddress::from)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn from_address(addr_str: &str) -> Result<EncodedAddress, String> {
|
||||
xpin::Address::from_str(addr_str)
|
||||
.as_ref()
|
||||
.map(EncodedAddress::from)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn get_address(&self) -> String {
|
||||
self.address.clone()
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn get_lat_lon(&self) -> Vec<f64> {
|
||||
vec![self.lat, self.lon]
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&'_ Address<'_>> for EncodedAddress {
|
||||
fn from(addr: &Address) -> Self {
|
||||
let (lat, lon) = addr.as_lat_lon();
|
||||
Self {
|
||||
address: addr.to_string(),
|
||||
lat,
|
||||
lon,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user