UNPKG

maplair

Version:

JavaScript library for mobile-friendly interactive maps

104 lines (103 loc) • 4.3 kB
import { Point } from "../../geometry/Point.js"; import { LatLng } from "../../geo/LatLng.js"; import { Marker } from "./Marker.js"; // src/layer/marker/Marker.Move.js /** @import {LatLngExpression} from '../../geo/LatLng.js' */ /** @import {MaplairMap} from '../../map/Map.js' */ /** @import {MaplairEvent} from '../../core/Events.js' */ /** * Options of the movement of markers, see `moveAlong()`. * @typedef {object} MarkerMovementOptions * @property {number} [duration=1000] The duration of the movement in milliseconds. * @property {number} [speed] The speed in meters (or units of the CRS) per second * instead of the `duration`. * @property {boolean} [rotate=false] Whether to set the `rotation` of the marker to * the heading of the movement (for icons pointing up). */ const { onRemove } = Marker.prototype; /** @typedef {typeof markerMoveMethods} MarkerMoveMethods */ Marker.include({ /** * Moves the marker smoothly to `latlng`, see `moveAlong()`. * @group Marker movement * @param {LatLngExpression} latlng * @param {MarkerMovementOptions} [options] * @returns {this} */ moveTo(latlng, options) { return this.moveAlong([latlng], options); }, /** * Moves the marker smoothly along the given points, frame by frame at a constant * speed, e.g. to follow a vehicle or to replay a track. Fires `move` with each * frame and `moveend` at the end. A new movement or `stopMoving()` stops it; with * [reduced motion](https://developer.mozilla.org/docs/Web/CSS/@media/prefers-reduced-motion), * the marker jumps to the end. * Inspired by the [Leaflet.MovingMarker](https://github.com/ewoken/Leaflet.MovingMarker) * and [Leaflet.AnimatedMarker](https://github.com/openplans/Leaflet.AnimatedMarker) plugins. * @group Marker movement * @param {LatLngExpression[]} latlngs * @param {MarkerMovementOptions} [options] * @returns {this} */ moveAlong(latlngs, options = {}) { this.stopMoving(); const points = [this._latlng, ...latlngs.map((latlng) => LatLng.from(latlng))], lengths = points.slice(1).map((point, i) => this._map?.distance(points[i], point) ?? points[i].distanceTo(point)), total = lengths.reduce((sum, length) => sum + length, 0), duration = options.speed ? total / options.speed * 1e3 : options.duration ?? 1e3, start = performance.now(); const frame = () => { let distance = duration && !this._map?._prefersReducedMotion() ? Math.min(1, (performance.now() - start) / duration) * total : total, i = 0; const moved = distance; while (i < lengths.length - 1 && distance > lengths[i]) { distance -= lengths[i]; i++; } const from = points[i], to = points[i + 1]; if (options.rotate && !from.equals(to)) this.options.rotation = this._heading(from, to); this.setLatLng(this._interpolate(from, to, lengths[i] ? Math.min(1, distance / lengths[i]) : 1)); if (moved < total) this._moveFrame = requestAnimationFrame(frame); else { this._moveFrame = null; this.fire("moveend"); } }; this._moveFrame = requestAnimationFrame(frame); return this; }, /** * Stops the movement of `moveTo()` or `moveAlong()`. * @group Marker movement * @returns {this} */ stopMoving() { cancelAnimationFrame(this._moveFrame); this._moveFrame = null; return this; }, /** * Returns `true` while the marker moves with `moveTo()` or `moveAlong()`. * @group Marker movement * @returns {boolean} */ isMoving() { return !!this._moveFrame; }, _interpolate(from, to, t) { const crs = this._map?.options.crs; if (!crs) return new LatLng(from.lat + (to.lat - from.lat) * t, from.lng + (to.lng - from.lng) * t); const a = crs.projection.project(from), b = crs.projection.project(to); return crs.projection.unproject(a.add(b.subtract(a).multiplyBy(t))); }, _heading(from, to) { const crs = this._map?.options.crs, a = crs ? crs.latLngToPoint(from, 0) : new Point(from.lng, -from.lat), b = crs ? crs.latLngToPoint(to, 0) : new Point(to.lng, -to.lat); return Math.atan2(b.x - a.x, a.y - b.y) * 180 / Math.PI - (this.options.rotateWithView ? 0 : this._map?.getBearing?.() ?? 0); }, /** * Stops the movement when the marker is removed from the map. * @internal * @param {MaplairMap} map */ onRemove(map) { this.stopMoving(); return onRemove.call(this, map); } }); //# sourceMappingURL=Marker.Move.js.map