web-sys define js_sys::Function

903 views Asked by At

How do I define a js_sys::Function that must be used in get_current_position to get the users latitude and longitude coordinates?

I have the following

let navigator = web_sys::window().expect("Missing window").navigator();
let geolocation = navigator.geolocation().unwrap();

geolocation.get_current_position(|pos: JsValue| {
    // logic omitted for brevity
})

which gives the following compile error by wasm-pack build --target web:

    |       geolocation.get_current_position(|pos: JsValue| {
    |  ______________________________________^
    | |
    | |     });
    | |_____^ expected `&Function`, found closure

As I understand, the argument needs to be a js_sys::Function instead of an ordinary rust closure. The documentation is a bit vague, but I think I need to use new_with_args ?

0

There are 0 answers