1use crate::map_impl::{MapImpl, StillImageCallback, StillImageRequest, StyleMut};
4use crate::map_observer::MapObserver;
5use crate::map_options::MapOptions;
6use mln_map_types::{
7 AnimationOptions, BoundOptions, CameraOptions, ConstrainMode, MapDebugOptions, MapMode, NorthOrientation,
8 ProjectionMode, TileLodMode, TransformState, ViewportMode,
9};
10use mln_renderer::{ExceptionPtr, RendererFrontend};
11use mln_storage::{ClientOptions, FileSourceManager, FileSourceType, ResourceOptions};
12use mln_style::Style;
13use mln_util::exception::MisuseException;
14use mln_util::geo::{EdgeInsets, ScreenCoordinate};
15use mln_util::size::Size;
16use mln_storage::FileSource;
17use mln_util::geo::{LatLng, LatLngBounds, WrapMode};
18use std::cell::Ref;
19use std::rc::Rc;
20use std::sync::Arc;
21
22pub struct Map {
23 impl_: Rc<MapImpl>,
24}
25
26impl Map {
27 pub fn new<F>(frontend: &F, observer: Rc<dyn MapObserver>, map_options: MapOptions) -> Map
33 where
34 F: RendererFrontend + Clone + 'static,
35 {
36 Self::with_resource_options(
37 frontend,
38 observer,
39 map_options,
40 &ResourceOptions::default(),
41 &ClientOptions::default(),
42 )
43 }
44
45 pub fn with_resource_options<F>(
50 frontend: &F,
51 observer: Rc<dyn MapObserver>,
52 map_options: MapOptions,
53 resource_options: &ResourceOptions,
54 client_options: &ClientOptions,
55 ) -> Map
56 where
57 F: RendererFrontend + Clone + 'static,
58 {
59 let file_source =
60 FileSourceManager::get().get_file_source(FileSourceType::ResourceLoader, resource_options, client_options);
61 Map {
62 impl_: MapImpl::new(Box::new(frontend.clone()), observer, file_source, &map_options),
63 }
64 }
65
66 pub fn with_file_source<F>(
71 frontend: &F,
72 observer: Rc<dyn MapObserver>,
73 file_source: Arc<dyn FileSource>,
74 map_options: MapOptions,
75 ) -> Map
76 where
77 F: RendererFrontend + Clone + 'static,
78 {
79 Map {
80 impl_: MapImpl::new(Box::new(frontend.clone()), observer, Some(file_source), &map_options),
81 }
82 }
83
84 pub fn get_client_options(&self) -> ClientOptions {
86 match &self.impl_.file_source {
87 Some(file_source) => file_source.get_client_options(),
88 None => ClientOptions::default(),
89 }
90 }
91
92
93 pub fn render_still(&mut self, callback: StillImageCallback) {
96 let impl_ = &self.impl_;
97 if impl_.mode != MapMode::Static && impl_.mode != MapMode::Tile {
98 callback(Some(misuse("Map is not in static or tile image render modes")));
99 return;
100 }
101
102 if impl_.still_image_request.borrow().is_some() {
103 callback(Some(misuse("Map is currently rendering an image")));
104 return;
105 }
106
107 let last_error = impl_.style.borrow().impl_.get_last_error();
108 if let Some(error) = last_error {
109 callback(Some(error));
110 return;
111 }
112
113 *impl_.still_image_request.borrow_mut() = Some(StillImageRequest { callback });
114
115 impl_.on_update();
116 }
117
118 pub fn render_still_with_camera(
120 &mut self,
121 camera: &CameraOptions,
122 debug_options: MapDebugOptions,
123 callback: StillImageCallback,
124 ) {
125 self.impl_.camera_mutated.set(true);
126 self.impl_.debug_options.set(debug_options);
127 self.impl_.transform.borrow_mut().jump_to(camera);
128 self.render_still(callback);
129 }
130
131 pub fn trigger_repaint(&mut self) {
133 self.impl_.on_update();
134 }
135
136 pub fn get_style(&self) -> Ref<'_, Style> {
140 self.impl_.style.borrow()
141 }
142
143 pub fn get_style_mut(&mut self) -> StyleMut<'_> {
146 StyleMut::new(&self.impl_)
147 }
148
149 pub fn cancel_transitions(&mut self) {
154 self.impl_.transform.borrow_mut().cancel_transitions();
155 self.impl_.on_update();
156 }
157
158 pub fn set_gesture_in_progress(&mut self, in_progress: bool) {
159 self.impl_.transform.borrow_mut().set_gesture_in_progress(in_progress);
160 self.impl_.on_update();
161 }
162
163 pub fn is_gesture_in_progress(&self) -> bool {
164 self.impl_.transform.borrow().is_gesture_in_progress()
165 }
166
167 pub fn is_rotating(&self) -> bool {
168 self.impl_.transform.borrow().is_rotating()
169 }
170
171 pub fn is_scaling(&self) -> bool {
172 self.impl_.transform.borrow().is_scaling()
173 }
174
175 pub fn is_panning(&self) -> bool {
176 self.impl_.transform.borrow().is_panning()
177 }
178
179 pub fn get_camera_options(&self, padding: Option<EdgeInsets>) -> CameraOptions {
182 self.impl_.transform.borrow().get_camera_options(padding)
183 }
184
185 pub fn jump_to(&mut self, camera: &CameraOptions) {
186 self.impl_.jump_to(camera);
187 }
188
189 pub fn lat_lng_bounds_for_camera(&self, camera: &CameraOptions) -> mln_util::geo::LatLngBounds {
192 let mut shallow = crate::transform::Transform::from_state(self.impl_.transform.borrow().get_state().clone());
193 let size = shallow.get_state().get_size();
194
195 shallow.jump_to(camera);
196 mln_util::geo::LatLngBounds::hull(
197 shallow.screen_coordinate_to_lat_lng(&ScreenCoordinate::new(0.0, 0.0), mln_util::geo::WrapMode::Wrapped),
198 shallow.screen_coordinate_to_lat_lng(
199 &ScreenCoordinate::new(f64::from(size.width), f64::from(size.height)),
200 mln_util::geo::WrapMode::Wrapped,
201 ),
202 )
203 }
204
205 pub fn ease_to(&mut self, camera: &CameraOptions, animation: &AnimationOptions) {
206 self.impl_.camera_mutated.set(true);
207 self.impl_.transform.borrow_mut().ease_to(camera, animation);
208 self.impl_.on_update();
209 }
210
211 pub fn fly_to(&mut self, camera: &CameraOptions, animation: &AnimationOptions) {
212 self.impl_.camera_mutated.set(true);
213 self.impl_.transform.borrow_mut().fly_to(camera, animation, false);
214 self.impl_.on_update();
215 }
216
217 pub fn move_by(&mut self, point: &ScreenCoordinate, animation: &AnimationOptions) {
218 self.impl_.camera_mutated.set(true);
219 self.impl_.transform.borrow_mut().move_by(point, animation);
220 self.impl_.on_update();
221 }
222
223 pub fn pitch_by(&mut self, pitch: f64, animation: &AnimationOptions) {
224 let current = mln_math::rad2deg(self.impl_.transform.borrow().get_pitch());
225 self.ease_to(&CameraOptions::default().with_pitch(Some(current - pitch)), animation);
226 }
227
228 pub fn scale_by(&mut self, scale: f64, anchor: Option<ScreenCoordinate>, animation: &AnimationOptions) {
229 let zoom = {
230 let transform = self.impl_.transform.borrow();
231 transform.get_zoom() + transform.get_state().scale_zoom(scale)
232 };
233 self.ease_to(&CameraOptions::default().with_zoom(Some(zoom)).with_anchor(anchor), animation);
234 }
235
236 pub fn rotate_by(&mut self, first: &ScreenCoordinate, second: &ScreenCoordinate, animation: &AnimationOptions) {
237 self.impl_.camera_mutated.set(true);
238 self.impl_.transform.borrow_mut().rotate_by(first, second, animation);
239 self.impl_.on_update();
240 }
241
242 pub fn camera_for_lat_lng_bounds(
246 &self,
247 bounds: &LatLngBounds,
248 padding: &EdgeInsets,
249 bearing: Option<f64>,
250 pitch: Option<f64>,
251 ) -> CameraOptions {
252 self.camera_for_lat_lngs(
253 &[bounds.northwest(), bounds.southwest(), bounds.southeast(), bounds.northeast()],
254 padding,
255 bearing,
256 pitch,
257 )
258 }
259
260 pub fn camera_for_lat_lngs(
262 &self,
263 lat_lngs: &[LatLng],
264 padding: &EdgeInsets,
265 bearing: Option<f64>,
266 pitch: Option<f64>,
267 ) -> CameraOptions {
268 if bearing.is_none() && pitch.is_none() {
269 return camera_for_lat_lngs(lat_lngs, &self.impl_.transform.borrow(), padding);
270 }
271
272 let mut transform = crate::transform::Transform::from_state(self.impl_.transform.borrow().get_state().clone());
273
274 transform.jump_to(&CameraOptions::default().with_bearing(bearing).with_pitch(pitch));
275
276 camera_for_lat_lngs(lat_lngs, &transform, padding)
277 .with_bearing(Some(mln_math::rad2deg(-transform.get_bearing())))
278 .with_pitch(Some(mln_math::rad2deg(transform.get_pitch())))
279 }
280
281 pub fn lat_lng_bounds_for_camera_unwrapped(&self, camera: &CameraOptions) -> LatLngBounds {
285 let mut shallow = crate::transform::Transform::from_state(self.impl_.transform.borrow().get_state().clone());
286 let size = shallow.get_state().get_size();
287
288 shallow.jump_to(camera);
289 let width = f64::from(size.width);
290 let height = f64::from(size.height);
291 let mut nw = shallow.screen_coordinate_to_lat_lng(&ScreenCoordinate::new(0.0, 0.0), WrapMode::Wrapped);
292 let mut se = shallow.screen_coordinate_to_lat_lng(&ScreenCoordinate::new(width, height), WrapMode::Wrapped);
293 let mut ne = shallow.screen_coordinate_to_lat_lng(&ScreenCoordinate::new(width, 0.0), WrapMode::Wrapped);
294 let mut sw = shallow.screen_coordinate_to_lat_lng(&ScreenCoordinate::new(0.0, height), WrapMode::Wrapped);
295 let center =
296 shallow.screen_coordinate_to_lat_lng(&ScreenCoordinate::new(width / 2.0, height / 2.0), WrapMode::Wrapped);
297 nw.unwrap_for_shortest_path(¢er);
298 se.unwrap_for_shortest_path(¢er);
299 ne.unwrap_for_shortest_path(¢er);
300 sw.unwrap_for_shortest_path(¢er);
301 let mut bounds = LatLngBounds::hull(nw, se);
302 bounds.extend(&ne);
303 bounds.extend(&sw);
304 bounds.extend(¢er);
305 bounds
306 }
307
308 pub fn set_bounds(&mut self, options: &BoundOptions) -> Result<(), String> {
311 let mut change_camera = false;
312 let mut camera_options = CameraOptions::default();
313
314 {
315 let mut transform = self.impl_.transform.borrow_mut();
316
317 if let Some(bounds) = options.bounds {
318 change_camera = true;
319 transform.set_lat_lng_bounds(bounds)?;
320 }
321
322 if let Some(min_zoom) = options.min_zoom {
323 transform.set_min_zoom(min_zoom);
324 if transform.get_zoom() < min_zoom {
325 change_camera = true;
326 camera_options = camera_options.with_zoom(options.min_zoom);
327 }
328 }
329
330 if let Some(max_zoom) = options.max_zoom {
331 transform.set_max_zoom(max_zoom);
332 if transform.get_zoom() > max_zoom {
333 change_camera = true;
334 camera_options = camera_options.with_zoom(options.max_zoom);
335 }
336 }
337
338 if let Some(max_pitch) = options.max_pitch {
339 transform.set_max_pitch(max_pitch);
340 if transform.get_pitch() > transform.get_state().get_max_pitch() {
341 change_camera = true;
342 camera_options = camera_options.with_pitch(options.max_pitch);
343 }
344 }
345
346 if let Some(min_pitch) = options.min_pitch {
347 transform.set_min_pitch(min_pitch);
348 if transform.get_pitch() < transform.get_state().get_min_pitch() {
349 change_camera = true;
350 camera_options = camera_options.with_pitch(options.min_pitch);
351 }
352 }
353 }
354
355 if change_camera {
356 self.jump_to(&camera_options);
357 }
358 Ok(())
359 }
360
361 pub fn get_bounds(&self) -> BoundOptions {
364 let transform = self.impl_.transform.borrow();
365 let state = transform.get_state();
366 BoundOptions::default()
367 .with_lat_lng_bounds(state.get_lat_lng_bounds())
368 .with_min_zoom(state.get_min_zoom())
369 .with_max_zoom(state.get_max_zoom())
370 .with_min_pitch(mln_math::rad2deg(state.get_min_pitch()))
371 .with_max_pitch(mln_math::rad2deg(state.get_max_pitch()))
372 }
373
374 pub fn set_north_orientation(&mut self, orientation: NorthOrientation) {
377 self.impl_.transform.borrow_mut().set_north_orientation(orientation);
378 self.impl_.on_update();
379 }
380
381 pub fn set_constrain_mode(&mut self, mode: ConstrainMode) {
382 self.impl_.transform.borrow_mut().set_constrain_mode(mode);
383 self.impl_.on_update();
384 }
385
386 pub fn set_viewport_mode(&mut self, mode: ViewportMode) {
387 self.impl_.transform.borrow_mut().set_viewport_mode(mode);
388 self.impl_.on_update();
389 }
390
391 pub fn set_size(&mut self, size: Size) -> Result<(), String> {
393 self.impl_.transform.borrow_mut().resize(size)?;
394 self.impl_.on_update();
395 Ok(())
396 }
397
398 pub fn set_frustum_offset(&mut self, frustum_offset: &EdgeInsets) {
399 self.impl_.transform.borrow_mut().set_frustum_offset(frustum_offset);
400 self.impl_.on_update();
401 }
402
403 pub fn get_frustum_offset(&self) -> EdgeInsets {
404 self.impl_.transform.borrow().get_frustum_offset()
405 }
406
407 pub fn get_map_options(&self) -> MapOptions {
408 let transform = self.impl_.transform.borrow();
409 let state = transform.get_state();
410 MapOptions::new()
411 .with_north_orientation(transform.get_north_orientation())
412 .with_map_mode(self.impl_.mode)
413 .with_constrain_mode(state.get_constrain_mode())
414 .with_viewport_mode(state.get_viewport_mode())
415 .with_cross_source_collisions(self.impl_.cross_source_collisions)
416 .with_size(state.get_size())
417 .with_pixel_ratio(self.impl_.pixel_ratio)
418 }
419
420 pub fn set_projection_mode(&mut self, options: &ProjectionMode) {
423 self.impl_.transform.borrow_mut().set_projection_mode(options);
424 self.impl_.on_update();
425 }
426
427 pub fn get_projection_mode(&self) -> ProjectionMode {
428 self.impl_.transform.borrow().get_projection_mode()
429 }
430
431 pub fn get_transform_state(&self) -> TransformState {
434 self.impl_.transform.borrow().get_state().clone()
435 }
436
437 pub fn set_tile_lod_min_radius(&mut self, radius: f64) {
440 if self.impl_.tile_lod_min_radius.get() == radius {
441 return;
442 }
443 self.impl_.tile_lod_min_radius.set(radius);
444 self.impl_.on_update();
445 }
446
447 pub fn get_tile_lod_min_radius(&self) -> f64 {
448 self.impl_.tile_lod_min_radius.get()
449 }
450
451 pub fn set_tile_lod_scale(&mut self, scale: f64) {
452 if self.impl_.tile_lod_scale.get() == scale {
453 return;
454 }
455 self.impl_.tile_lod_scale.set(scale);
456 self.impl_.on_update();
457 }
458
459 pub fn get_tile_lod_scale(&self) -> f64 {
460 self.impl_.tile_lod_scale.get()
461 }
462
463 pub fn set_tile_lod_pitch_threshold(&mut self, threshold: f64) {
464 if self.impl_.tile_lod_pitch_threshold.get() == threshold {
465 return;
466 }
467 self.impl_.tile_lod_pitch_threshold.set(threshold);
468 self.impl_.on_update();
469 }
470
471 pub fn get_tile_lod_pitch_threshold(&self) -> f64 {
472 self.impl_.tile_lod_pitch_threshold.get()
473 }
474
475 pub fn set_tile_lod_zoom_shift(&mut self, shift: f64) {
476 if self.impl_.tile_lod_zoom_shift.get() == shift {
477 return;
478 }
479 self.impl_.tile_lod_zoom_shift.set(shift);
480 self.impl_.on_update();
481 }
482
483 pub fn get_tile_lod_zoom_shift(&self) -> f64 {
484 self.impl_.tile_lod_zoom_shift.get()
485 }
486
487 pub fn set_tile_lod_mode(&mut self, mode: TileLodMode) {
488 if self.impl_.tile_lod_mode.get() == mode {
489 return;
490 }
491 self.impl_.tile_lod_mode.set(mode);
492 self.impl_.on_update();
493 }
494
495 pub fn get_tile_lod_mode(&self) -> TileLodMode {
496 self.impl_.tile_lod_mode.get()
497 }
498
499 pub fn pixel_for_lat_lng(&self, lat_lng: &LatLng) -> ScreenCoordinate {
504 let transform = self.impl_.transform.borrow();
508 let mut unwrapped_lat_lng = lat_lng.wrapped();
509 unwrapped_lat_lng.unwrap_for_shortest_path(&transform.get_lat_lng(WrapMode::Wrapped));
510 transform.lat_lng_to_screen_coordinate(&unwrapped_lat_lng)
511 }
512
513 pub fn lat_lng_for_pixel(&self, pixel: &ScreenCoordinate, wrap_mode: WrapMode) -> LatLng {
515 self.impl_.transform.borrow().screen_coordinate_to_lat_lng(pixel, wrap_mode)
516 }
517
518 pub fn pixels_for_lat_lngs(&self, lat_lngs: &[LatLng]) -> Vec<ScreenCoordinate> {
520 lat_lngs.iter().map(|lat_lng| self.pixel_for_lat_lng(lat_lng)).collect()
521 }
522
523 pub fn lat_lngs_for_pixels(&self, screen_coords: &[ScreenCoordinate], wrap_mode: WrapMode) -> Vec<LatLng> {
525 screen_coords
526 .iter()
527 .map(|point| self.lat_lng_for_pixel(point, wrap_mode))
528 .collect()
529 }
530
531 pub fn set_prefetch_zoom_delta(&mut self, delta: u8) {
533 if self.impl_.prefetch_zoom_delta.get() == delta {
534 return;
535 }
536 self.impl_.prefetch_zoom_delta.set(delta);
537 self.impl_.on_update();
538 }
539
540 pub fn get_prefetch_zoom_delta(&self) -> u8 {
542 self.impl_.prefetch_zoom_delta.get()
543 }
544
545 pub fn get_free_camera_options(&self) -> mln_map_types::FreeCameraOptions {
547 self.impl_.transform.borrow().get_free_camera_options()
548 }
549
550 pub fn set_debug(&mut self, debug_options: MapDebugOptions) {
556 self.impl_.debug_options.set(debug_options);
557 self.impl_.on_update();
558 }
559
560 pub fn get_debug(&self) -> MapDebugOptions {
561 self.impl_.debug_options.get()
562 }
563
564 pub fn is_fully_loaded(&self) -> bool {
565 self.impl_.style.borrow().impl_.is_loaded() && self.impl_.renderer_fully_loaded.get()
566 }
567}
568
569fn misuse(message: &str) -> ExceptionPtr {
570 MisuseException::ptr(message)
571}
572
573pub(crate) fn camera_for_lat_lngs(
576 lat_lngs: &[LatLng],
577 transform: &crate::transform::Transform,
578 padding: &EdgeInsets,
579) -> CameraOptions {
580 if lat_lngs.is_empty() {
581 return CameraOptions::default();
582 }
583 let size = transform.get_state().get_size();
584 let mut ne_pixel = ScreenCoordinate::new(f64::NEG_INFINITY, f64::NEG_INFINITY);
586 let mut sw_pixel = ScreenCoordinate::new(f64::INFINITY, f64::INFINITY);
587 for lat_lng in lat_lngs {
588 let pixel = transform.lat_lng_to_screen_coordinate(lat_lng);
589 sw_pixel.x = if pixel.x < sw_pixel.x { pixel.x } else { sw_pixel.x };
591 ne_pixel.x = if ne_pixel.x < pixel.x { pixel.x } else { ne_pixel.x };
592 sw_pixel.y = if pixel.y < sw_pixel.y { pixel.y } else { sw_pixel.y };
593 ne_pixel.y = if ne_pixel.y < pixel.y { pixel.y } else { ne_pixel.y };
594 }
595 let width = ne_pixel.x - sw_pixel.x;
596 let height = ne_pixel.y - sw_pixel.y;
597
598 let mut min_scale = f64::INFINITY;
600 if width > 0.0 || height > 0.0 {
601 let mut scale_x = f64::from(size.width) / width;
602 let mut scale_y = f64::from(size.height) / height;
603 scale_x -= (padding.left() + padding.right()) / width;
604 scale_y -= (padding.top() + padding.bottom()) / height;
605 min_scale = mln_math::min_f64(scale_x, scale_y);
606 }
607
608 let mut zoom = transform.get_zoom();
609 if min_scale > 0.0 {
610 zoom = mln_math::clamp_f64(
611 zoom + mln_math::log2(min_scale),
612 transform.get_state().get_min_zoom(),
613 transform.get_state().get_max_zoom(),
614 );
615 } else {
616 mln_util::logging::Log::error(
617 mln_util::logging::Event::General,
618 "Unable to calculate appropriate zoom level for bounds. Vertical or horizontal padding is greater than \
619 map's height or width.",
620 );
621 }
622
623 let center_pixel = ScreenCoordinate::new((ne_pixel.x + sw_pixel.x) / 2.0, (ne_pixel.y + sw_pixel.y) / 2.0);
625
626 CameraOptions::default()
627 .with_center(Some(transform.screen_coordinate_to_lat_lng(¢er_pixel, WrapMode::Wrapped)))
628 .with_padding(Some(*padding))
629 .with_zoom(Some(zoom))
630}