Skip to content

Commit

Permalink
Implement LngLat and LngLatBounds
Browse files Browse the repository at this point in the history
* Change LatLng to LngLat
* LngLat and LngLatBoulds holds a reference to JavaScript object.
  It might be slower than plain Rust object. I will condier to
  cache value if it really matters in the future
* Bump to v0.1.0
  • Loading branch information
yukinarit committed Nov 21, 2022
1 parent 921d801 commit f47fb56
Show file tree
Hide file tree
Showing 14 changed files with 226 additions and 84 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mapboxgl"
version = "0.0.1"
version = "0.1.0"
edition = "2021"
description = "Unofficial Rust binding for mapbox-gl-js"
readme = "README.md"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ trunk serve
* [ ] [ScaleControl](https://docs.mapbox.com/mapbox-gl-js/api/markers/#scalecontrol)
* Geography and Geometry
* [x] [LngLat](https://docs.mapbox.com/mapbox-gl-js/api/geography/#lnglat)
* [ ] [LngLatBounds](https://docs.mapbox.com/mapbox-gl-js/api/geography/#lnglatbounds)
* [x] [LngLatBounds](https://docs.mapbox.com/mapbox-gl-js/api/geography/#lnglatbounds)
* [ ] [LngLatBoundsLike](https://docs.mapbox.com/mapbox-gl-js/api/geography/#lnglatboundslike)
* [ ] [LngLatLike](https://docs.mapbox.com/mapbox-gl-js/api/geography/#lnglatlike)
* [ ] [MercatorCoordinate](https://docs.mapbox.com/mapbox-gl-js/api/geography/#mercatorcoordinate)
Expand Down
2 changes: 1 addition & 1 deletion examples/geojson-source/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions examples/geojson-source/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use futures::channel::oneshot;
use log::*;
use mapboxgl::{event, layer, LatLng, Layer, Map, MapEventListner, MapFactory, MapOptions};
use mapboxgl::{event, layer, Layer, LngLat, Map, MapEventListner, MapFactory, MapOptions};
use std::borrow::BorrowMut;
use std::{cell::RefCell, rc::Rc};
use yew::prelude::*;
Expand Down Expand Up @@ -108,10 +108,7 @@ pub fn create_map() -> MapFactory {
.unwrap_or("pk.eyJ1IjoieXVraW5hcml0IiwiYSI6ImNsYTdncnVsZDBuYTgzdmxkanhqanZwdnoifQ.m3FLgX5Elx1fUIyyn7dZYg");

let opts = MapOptions::new(token.into(), "map".into())
.center(LatLng {
lat: 37.830348,
lng: -122.486052,
})
.center(LngLat::new(-122.486052, 37.830348))
.zoom(15.0);

mapboxgl::MapFactory::new(opts).unwrap()
Expand Down
2 changes: 1 addition & 1 deletion examples/popup/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 3 additions & 9 deletions examples/popup/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use futures::channel::oneshot;
use log::*;
use mapboxgl::{event, LatLng, Map, MapEventListner, MapFactory, MapOptions, Popup, PopupOptions};
use mapboxgl::{event, LngLat, Map, MapEventListner, MapFactory, MapOptions, Popup, PopupOptions};
use std::borrow::BorrowMut;
use std::{cell::RefCell, rc::Rc};
use yew::prelude::*;
Expand All @@ -16,10 +16,7 @@ impl MapEventListner for Listner {
}

fn on_click(&mut self, map: &Map, e: event::MapMouseEvent) {
let latlng = LatLng {
lat: e.lng_lat.lat,
lng: e.lng_lat.lng,
};
let latlng = LngLat::new(e.lng_lat.lng, e.lng_lat.lat);

let popup = Popup::new(latlng, PopupOptions::new());
popup.set_html("<h1>Hello</h1>");
Expand Down Expand Up @@ -69,10 +66,7 @@ pub fn create_map() -> MapFactory {
.unwrap_or("pk.eyJ1IjoieXVraW5hcml0IiwiYSI6ImNsYTdncnVsZDBuYTgzdmxkanhqanZwdnoifQ.m3FLgX5Elx1fUIyyn7dZYg");

let opts = MapOptions::new(token.into(), "map".into())
.center(LatLng {
lat: 35.6812373,
lng: 139.7647863,
})
.center(LngLat::new(139.7647863, 35.6812373))
.zoom(15.0);

mapboxgl::MapFactory::new(opts).unwrap()
Expand Down
2 changes: 1 addition & 1 deletion examples/set-data/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 6 additions & 9 deletions examples/set-data/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use futures::channel::oneshot;
use gloo::timers::future::TimeoutFuture;
use log::*;
use mapboxgl::{layer, LatLng, Layer, Map, MapEventListner, MapFactory, MapOptions};
use mapboxgl::{layer, Layer, LngLat, Map, MapEventListner, MapFactory, MapOptions};
use std::borrow::BorrowMut;
use std::{cell::RefCell, ops::Deref, rc::Rc};
use wasm_bindgen::JsCast;
Expand Down Expand Up @@ -85,10 +85,10 @@ fn app() -> Html {
if let geojson::Value::LineString(coordinates) =
&path.features[0].geometry.as_ref().unwrap().value
{
let latlng = LatLng {
lat: coordinates.last().unwrap()[1],
lng: coordinates.last().unwrap()[0],
};
let latlng = LngLat::new(
coordinates.last().unwrap()[0],
coordinates.last().unwrap()[1],
);
info!("latlng = {:?}", latlng);
update_data(source, path);
map.map.pan_to(latlng);
Expand Down Expand Up @@ -151,10 +151,7 @@ pub fn create_map() -> MapFactory {
let token = std::option_env!("MAPBOX_TOKEN")
.unwrap_or("pk.eyJ1IjoieXVraW5hcml0IiwiYSI6ImNsYTdncnVsZDBuYTgzdmxkanhqanZwdnoifQ.m3FLgX5Elx1fUIyyn7dZYg");
let opts = MapOptions::new(token.into(), "map".into())
.center(LatLng {
lat: 45.632433,
lng: -122.019807,
})
.center(LngLat::new(-122.019807, 45.632433))
.zoom(15.0);
mapboxgl::MapFactory::new(opts).unwrap()
}
Expand Down
2 changes: 1 addition & 1 deletion examples/simple/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions examples/simple/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use futures::channel::oneshot;
use log::*;
use mapboxgl::{event, LatLng, Map, MapEventListner, MapFactory, MapOptions};
use mapboxgl::{event, LngLat, Map, MapEventListner, MapFactory, MapOptions};
use std::borrow::BorrowMut;
use std::{cell::RefCell, rc::Rc};
use yew::prelude::*;
Expand Down Expand Up @@ -57,10 +57,7 @@ pub fn create_map() -> MapFactory {
.unwrap_or("pk.eyJ1IjoieXVraW5hcml0IiwiYSI6ImNsYTdncnVsZDBuYTgzdmxkanhqanZwdnoifQ.m3FLgX5Elx1fUIyyn7dZYg");

let opts = MapOptions::new(token.into(), "map".into())
.center(LatLng {
lat: 35.6812373,
lng: 139.7647863,
})
.center(LngLat::new(139.7647863, 35.6812373))
.zoom(15.0);

mapboxgl::MapFactory::new(opts).unwrap()
Expand Down
106 changes: 77 additions & 29 deletions src/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,89 @@ use wasm_bindgen::prelude::*;

#[wasm_bindgen]
extern "C" {
pub type LngLatLike;
pub type LngLat;

#[wasm_bindgen(constructor, js_namespace = mapboxgl)]
pub fn new(lng: f64, lat: f64) -> LngLatLike;
pub fn new(lng: f64, lat: f64) -> LngLat;

#[wasm_bindgen(method, getter)]
pub fn lng(this: &LngLatLike) -> f64;
pub fn lng(this: &LngLat) -> f64;

#[wasm_bindgen(method, setter)]
pub fn set_lng(this: &LngLatLike, v: f64) -> LngLatLike;
pub fn set_lng(this: &LngLat, v: f64) -> LngLat;

#[wasm_bindgen(method, getter)]
pub fn lat(this: &LngLatLike) -> f64;
pub fn lat(this: &LngLat) -> f64;

#[wasm_bindgen(method, setter)]
pub fn set_lat(this: &LngLatLike, v: f64) -> LngLatLike;
pub fn set_lat(this: &LngLat, v: f64) -> LngLat;

#[wasm_bindgen(method)]
pub fn wrap(this: &LngLat) -> LngLat;

#[wasm_bindgen(method)]
pub fn toArray(this: &LngLat) -> Vec<f64>;

#[wasm_bindgen(method)]
pub fn toString(this: &LngLat) -> String;

#[wasm_bindgen(method)]
pub fn distanceTo(this: &LngLat, lngLat: &LngLat) -> f64;

#[wasm_bindgen(method)]
pub fn toBounds(this: &LngLat, radius: f64) -> LngLatBounds;

// --

pub type LngLatBounds;

#[wasm_bindgen(constructor, js_namespace = mapboxgl)]
pub fn new(sw: LngLatLike, ne: LngLatLike) -> LngLatBounds;
pub fn new(sw: LngLat, ne: LngLat) -> LngLatBounds;

#[wasm_bindgen(method, getter)]
pub fn sw(this: &LngLatBounds) -> LngLatLike;
#[wasm_bindgen(method)]
pub fn setNorthEast(this: &LngLatBounds, ne: LngLat) -> LngLat;

#[wasm_bindgen(method, getter)]
pub fn ne(this: &LngLatBounds) -> LngLatLike;
#[wasm_bindgen(method)]
pub fn setSouthWest(this: &LngLatBounds, sw: LngLat) -> LngLat;

#[wasm_bindgen(method)]
pub fn getCenter(this: &LngLatBounds) -> LngLat;

#[wasm_bindgen(method)]
pub fn getSouthWest(this: &LngLatBounds) -> LngLat;

#[wasm_bindgen(method)]
pub fn getNorthEast(this: &LngLatBounds) -> LngLat;

#[wasm_bindgen(method)]
pub fn getNorthWest(this: &LngLatBounds) -> LngLat;

#[wasm_bindgen(method)]
pub fn getSouthEast(this: &LngLatBounds) -> LngLat;

#[wasm_bindgen(method)]
pub fn getWest(this: &LngLatBounds) -> f64;

#[wasm_bindgen(method)]
pub fn getSouth(this: &LngLatBounds) -> f64;

#[wasm_bindgen(method)]
pub fn getEast(this: &LngLatBounds) -> f64;

#[wasm_bindgen(method)]
pub fn getNorth(this: &LngLatBounds) -> f64;

#[wasm_bindgen(method)]
pub fn isEmpty(this: &LngLatBounds) -> f64;

#[wasm_bindgen(method)]
pub fn contains(this: &LngLat) -> bool;

#[wasm_bindgen(method)]
pub fn toString(this: &LngLatBounds) -> String;

// --

///
/// Map
///
pub type Map;

#[wasm_bindgen(constructor, js_namespace = mapboxgl)]
Expand Down Expand Up @@ -112,11 +164,10 @@ extern "C" {
pub fn set_handler(this: &Map, name: &str, handler: BoxZoomHandler) -> JsValue;

#[wasm_bindgen(method, js_name=panTo)]
pub fn Map_panTo(this: &Map, lngLat: crate::LatLng, options: JsValue, eventData: JsValue);
pub fn Map_panTo(this: &Map, lngLat: &LngLat, options: JsValue, eventData: JsValue);

// --

///
/// Handler
///
pub type BoxZoomHandler;

#[wasm_bindgen(constructor, js_namespace = mapboxgl)]
Expand All @@ -140,26 +191,24 @@ extern "C" {
#[wasm_bindgen(method, js_name=enableRotation)]
pub fn BoxZoomHandler_enableRotation(this: &BoxZoomHandler);

///
/// Marker
///
// --

pub type Marker;

#[wasm_bindgen(constructor, js_namespace = mapboxgl)]
pub fn maker_new(options: JsValue) -> Marker;

#[wasm_bindgen(method)]
pub fn setLngLat(this: &Marker, lngLat: crate::LatLng);
pub fn setLngLat(this: &Marker, lngLat: &LngLat);

#[wasm_bindgen(method)]
pub fn addTo(this: &Marker, map: &Map);

#[wasm_bindgen(method)]
pub fn remove(this: &Marker);

///
/// Popup
///
// --

pub type Popup;

#[wasm_bindgen(constructor, js_namespace = mapboxgl)]
Expand All @@ -169,14 +218,13 @@ extern "C" {
pub fn Popup_setHTML(this: &Popup, html: String);

#[wasm_bindgen(method, js_name=setLngLat)]
pub fn Popup_setLngLat(this: &Popup, lngLat: crate::LatLng);
pub fn Popup_setLngLat(this: &Popup, lngLat: &LngLat);

#[wasm_bindgen(method, js_name=addTo)]
pub fn Popup_addTo(this: &Popup, map: &Map);

///
/// Popup
///
// --

pub type GeoJSONSource;

#[wasm_bindgen(method, js_name=setData)]
Expand Down
Loading

0 comments on commit f47fb56

Please sign in to comment.