Add osm url

This commit is contained in:
Austen Adler 2023-03-26 21:45:05 -04:00
parent 773063daf0
commit c1ed165973
3 changed files with 57 additions and 3 deletions

View File

@ -2,10 +2,15 @@ use crate::{Error, LatLon};
use std::str::FromStr;
use url::Url;
// TODO: Set a reasonable OSM zoom level
const OPENSTREETMAP_ZOOM_LEVEL: u8 = 19;
#[derive(PartialEq, Debug, Clone)]
pub struct CoordinateUrls {
google_maps: String,
latlon: LatLon,
// TODO: These should be getters only
pub google_maps: String,
pub openstreetmap: String,
pub latlon: LatLon,
}
impl CoordinateUrls {
@ -38,13 +43,23 @@ impl TryFrom<LatLon> for CoordinateUrls {
type Error = Error;
fn try_from(latlon: LatLon) -> Result<Self, Self::Error> {
let latlon_str = format!("{},{}", latlon.get_lat(), latlon.get_lon());
let (lat, lon) = (latlon.get_lat(), latlon.get_lon());
let latlon_str = format!("{lat},{lon}");
let openstreetmap = {
let mut ret = Url::parse("https://www.openstreetmap.org/")?;
ret.set_fragment(Some(&format!("map={OPENSTREETMAP_ZOOM_LEVEL}/{lat}/{lon}")));
String::from(ret)
};
Ok(Self {
google_maps: String::from(Url::parse_with_params(
"https://www.google.com/maps/search/",
&[("api", "1"), ("query", &latlon_str)],
)?),
openstreetmap,
latlon,
})
}

View File

@ -4,6 +4,7 @@
export let xpin;
let formats = ['dd', 'dms', 'dmm', 'utm', 'plus'];
let urlFormats = ['google_maps', 'openstreetmap'];
onMount(() => {
// TODO: Indicate that the data is copied
@ -25,4 +26,16 @@
<td><CopyButton data={xpin.allCoordinates[format]} /></td>
</tr>
{/each}
{#each urlFormats as format}
<tr>
<th>{format.toUpperCase()}</th>
<td>
<a class="text-blue-600" href={xpin.coordinateUrls[format]} target="_blank"
>Open in {format}</a
>
</td>
<td><CopyButton data={xpin.coordinateUrls[format]} /></td>
</tr>
{/each}
</table>

View File

@ -47,6 +47,10 @@ pub struct EncodedAddress {
pub address: String,
/// The coordinates used to encode this address
src_coords: Coordinate,
#[wasm_bindgen(js_name = coordinateUrls)]
pub coordinate_urls: CoordinateUrls,
#[wasm_bindgen(js_name = latLon)]
pub lat_lon: Box<[f64]>,
@ -173,12 +177,34 @@ impl TryFrom<&'_ Address<'_>> for EncodedAddress {
address: addr.to_string(),
lat_lon: Box::new([lat, lon]),
src_coords,
coordinate_urls: spatial_coordinate_systems::urls::CoordinateUrls::try_from(
all_coordinates.latlon,
)
.map_err(|e| e.to_string())?
.into(),
decimal_degrees: format!("{}, {}", lat, lon),
all_coordinates: Coordinates::from(&all_coordinates),
})
}
}
#[wasm_bindgen(getter_with_clone)]
#[derive(Debug, Clone)]
pub struct CoordinateUrls {
// TODO: These should be getters only
pub google_maps: String,
pub openstreetmap: String,
}
impl From<spatial_coordinate_systems::urls::CoordinateUrls> for CoordinateUrls {
fn from(value: spatial_coordinate_systems::urls::CoordinateUrls) -> Self {
Self {
google_maps: value.google_maps,
openstreetmap: value.openstreetmap,
}
}
}
#[cfg(test)]
mod tests {
use super::*;