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