Display a map with HTMLElement 🔗
Initialize a map with Yew and the unofficial MapLibre GL JS Rust bindings by passing an HTMLElement reference.
// src/main.rs use maplibre_gl_js::interface::{HtmlElement, MapOptions}; use yew::{Html, function_component, html, use_effect, use_node_ref, use_state}; #[function_component(App)] fn app() -> Html { let container_ref = use_node_ref(); let map_rendered = use_state(|| false); { let container_ref = container_ref.clone(); use_effect(move || { if *map_rendered { return; } let container = container_ref .cast::<HtmlElement>() .expect("Container should be a valid HtmlElement"); MapOptions::new(container) .with_style("https://demotiles.maplibre.org/style.json") .with_center([0., 0.]) .with_zoom(1.0) .with_maplibre_logo() .build() .expect("Creating a map should work"); map_rendered.set(true); }); } html! { <div ref={container_ref}></div> } } fn main() { wasm_logger::init(wasm_logger::Config::new(log::Level::Trace)); console_error_panic_hook::set_once(); yew::Renderer::<App>::new().render(); }
<!-- index.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link rel="shortcut icon"type="image/x-icon" href="data:image/x-icon;,">
<title>Display a map</title>
<link rel="scss" data-trunk href="scss/style.scss" />
<script src="https://unpkg.com/maplibre-gl@^5.12.0/dist/maplibre-gl.js"></script>
<link href="https://unpkg.com/maplibre-gl@^5.12.0/dist/maplibre-gl.css" rel="stylesheet" />
</head>
<body>loading...</body>
</html>
/* scss/style.scss */
body { margin: 0; padding: 0; }
html, body, body > div { height: 100%; }