@nativescript-community/ui-carto
Version:
NativeScript plugin for CARTO Mobile SDK
342 lines • 12.8 kB
JavaScript
import { MapBounds, fromNativeMapBounds, fromNativeMapPos, fromNativeScreenPos, nativeVectorToArray, toNativeMapPos, toNativeScreenBounds, toNativeScreenPos } from '../core';
import { EPSG4326 } from '../projections/epsg4326';
import { restrictedPanningProperty } from './cssproperties';
import { Layers as BaseLayers, CartoViewBase, MapClickedEvent, MapIdleEvent, MapInteractionEvent, MapMovedEvent, MapReadyEvent, MapStableEvent } from './index.common';
import { ImageSource } from '@nativescript/core';
import { executeOnMainThread } from '@nativescript/core/utils';
export { MapClickedEvent, MapIdleEvent, MapMovedEvent, MapReadyEvent, MapStableEvent };
export var RenderProjectionMode;
(function (RenderProjectionMode) {
RenderProjectionMode[RenderProjectionMode["RENDER_PROJECTION_MODE_PLANAR"] = 0] = "RENDER_PROJECTION_MODE_PLANAR";
RenderProjectionMode[RenderProjectionMode["RENDER_PROJECTION_MODE_SPHERICAL"] = 1] = "RENDER_PROJECTION_MODE_SPHERICAL";
})(RenderProjectionMode || (RenderProjectionMode = {}));
export var PanningMode;
(function (PanningMode) {
PanningMode[PanningMode["PANNING_MODE_FREE"] = 0] = "PANNING_MODE_FREE";
PanningMode[PanningMode["PANNING_MODE_STICKY"] = 1] = "PANNING_MODE_STICKY";
PanningMode[PanningMode["PANNING_MODE_STICKY_FINAL"] = 2] = "PANNING_MODE_STICKY_FINAL";
})(PanningMode || (PanningMode = {}));
let runOnMainThread = true;
function mainThread(target, propertyKey, descriptor) {
const originalMethod = descriptor.value;
//wrapping the original method
descriptor.value = function (...args) {
if (runOnMainThread) {
executeOnMainThread(() => {
originalMethod.apply(this, args);
});
}
else {
originalMethod.apply(this, args);
}
};
}
var AKMapEventListenerImpl = /** @class */ (function (_super) {
__extends(AKMapEventListenerImpl, _super);
function AKMapEventListenerImpl() {
return _super !== null && _super.apply(this, arguments) || this;
}
AKMapEventListenerImpl.initWithOwner = function (owner) {
var delegate = AKMapEventListenerImpl.new();
delegate._owner = owner;
return delegate;
};
AKMapEventListenerImpl.prototype.onMapIdle = function () {
var _a;
var owner = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.get();
if (owner) {
owner.sendEvent(MapIdleEvent);
}
};
AKMapEventListenerImpl.prototype.onMapMoved = function (userAction) {
var _a;
var owner = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.get();
if (owner) {
owner.sendEvent(MapMovedEvent, { userAction: userAction });
}
};
AKMapEventListenerImpl.prototype.onMapInteraction = function (interaction, userAction) {
var _a;
var owner = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.get();
if (owner) {
owner.sendEvent(MapInteractionEvent, {
userAction: userAction,
interaction: {
get isAnimationStarted() {
return interaction.isAnimationStarted();
},
get isPanAction() {
return interaction.isPanAction();
},
get isRotateAction() {
return interaction.isRotateAction();
},
get isTiltAction() {
return interaction.isTiltAction();
},
get isZoomAction() {
return interaction.isZoomAction();
}
}
});
}
};
AKMapEventListenerImpl.prototype.onMapStable = function (userAction) {
var _a;
var owner = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.get();
if (owner) {
owner.sendEvent(MapStableEvent, { userAction: userAction });
}
};
AKMapEventListenerImpl.prototype.onMapClicked = function (mapClickInfo) {
var _a;
var owner = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.get();
if (owner) {
owner.sendEvent(MapClickedEvent, {
ios: mapClickInfo,
get clickInfo() {
return {
get duration() {
return mapClickInfo.getClickInfo().getDuration();
}
};
},
get clickType() {
return mapClickInfo.getClickType();
},
get position() {
return fromNativeMapPos(mapClickInfo.getClickPos());
}
});
}
};
AKMapEventListenerImpl.ObjCProtocols = [AKMapEventListener];
return AKMapEventListenerImpl;
}(NSObject));
var NTRendererCaptureListenerImpl = /** @class */ (function (_super) {
__extends(NTRendererCaptureListenerImpl, _super);
function NTRendererCaptureListenerImpl() {
return _super !== null && _super.apply(this, arguments) || this;
}
NTRendererCaptureListenerImpl.initWithCallback = function (callback) {
var delegate = NTRendererCaptureListenerImpl.new();
delegate._callback = callback;
return delegate;
};
NTRendererCaptureListenerImpl.prototype.onMapRenderedThreaded = function (param0) {
var callback = this._callback.get();
if (callback) {
callback(NTBitmapUtils.createUIImageFromBitmap(param0));
}
};
return NTRendererCaptureListenerImpl;
}(AKRendererCaptureListener));
export class CartoMap extends CartoViewBase {
static setRunOnMainThread(value) {
runOnMainThread = value;
}
get mapView() {
return this.nativeViewProtected;
}
get projection() {
return this.mProjection;
}
set projection(proj) {
this.mProjection = proj;
this.nativeProjection = this.mProjection.getNative();
if (this.nativeViewProtected) {
this.mapView.getOptions().setBaseProjection(this.nativeProjection);
}
}
createNativeView() {
return AKMapView.alloc().init();
}
getOptions() {
if (this.mapReady) {
return this.mapView.getOptions();
}
return null;
}
initNativeView() {
super.initNativeView();
if (!this.projection) {
this.projection = new EPSG4326();
}
this.mapView.setAKMapEventListener(AKMapEventListenerImpl.initWithOwner(new WeakRef(this)));
}
disposeNativeView() {
this.mapView.setMapEventListener(null);
this.mapView.getLayers().clear();
this.nativeProjection = null;
this.mProjection = null;
super.disposeNativeView();
}
fromNativeMapPos(position) {
return fromNativeMapPos(position);
}
fromNativeMapBounds(position) {
return fromNativeMapBounds(NTMapBounds.alloc().initWithMinMax(position.getMin(), position.getMax()));
}
toNativeMapPos(position) {
return toNativeMapPos(position);
}
toNativeMapBounds(position) {
return NTMapBounds.alloc().initWithMinMax(toNativeMapPos(position.southwest), toNativeMapPos(position.northeast));
}
setFocusPos(value, duration = 0) {
this.mapView.setFocusPosDurationSeconds(toNativeMapPos(value), duration / 1000);
}
getFocusPos() {
return fromNativeMapPos(this.mapView.getFocusPos());
}
getMapBounds() {
const screenBounds = toNativeScreenBounds({ min: { x: 0, y: 0 }, max: { x: this.getMeasuredWidth(), y: this.getMeasuredHeight() } });
return new MapBounds(fromNativeMapPos(this.mapView.screenToMap(screenBounds.getMin())), fromNativeMapPos(this.mapView.screenToMap(screenBounds.getMax())));
}
getZoom() {
return this.mapView.getZoom();
}
setZoom(value, targetPos, duration = 0) {
if (typeof targetPos === 'number') {
this.mapView.setZoomDurationSeconds(value, targetPos / 1000);
}
else {
this.mapView.setZoomTargetPosDurationSeconds(value, toNativeMapPos(targetPos), duration / 1000);
}
}
setMapRotation(value, targetPos, duration = 0) {
if (typeof targetPos === 'number') {
this.mapView.setRotationDurationSeconds(value, targetPos / 1000);
}
else {
this.mapView.setRotationTargetPosDurationSeconds(value, toNativeMapPos(targetPos), duration / 1000);
}
}
setTilt(value, duration = 0) {
this.mapView.setTiltDurationSeconds(value, duration / 1000);
}
setBearing(value, duration = 0) {
this.mapView.setRotationDurationSeconds(value, duration / 1000);
}
moveToFitBounds(mapBounds, screenBounds, integerZoom, resetRotation, resetTilt, durationSeconds) {
if (!screenBounds) {
screenBounds = { min: { x: 0, y: 0 }, max: { x: this.getMeasuredWidth(), y: this.getMeasuredHeight() } };
}
this.mapView.moveToFitBoundsScreenBoundsIntegerZoomResetRotationResetTiltDurationSeconds(this.toNativeMapBounds(mapBounds), toNativeScreenBounds(screenBounds), integerZoom, resetRotation, resetTilt, durationSeconds / 1000);
}
[restrictedPanningProperty.setNative](value) {
if (!this.nativeViewProtected) {
return;
}
this.mapView.getOptions().setRestrictedPanning(value);
}
getLayers() {
if (this.mapView) {
return new Layers(this.mapView.getLayers());
}
return null;
}
addLayer(layer, index) {
if (this.mapView) {
const native = layer.getNative();
if (!!native) {
const layers = this.mapView.getLayers();
if (index !== undefined && index <= layers.count()) {
layers.insertLayer(index, native);
}
else {
layers.add(native);
}
}
}
}
removeLayer(layer) {
if (this.mapView) {
this.mapView.getLayers().remove(layer.getNative());
}
}
removeAllLayers(layers) {
if (this.mapView) {
const vector = NTLayerVector.alloc().init();
layers.forEach((l) => vector.add(l.getNative()));
this.mapView.getLayers().removeAll(vector);
}
}
clearAllCaches() {
this.mapView && this.mapView.clearAllCaches();
}
clearPreloadingCaches() {
this.mapView && this.mapView.clearPreloadingCaches();
}
cancelAllTasks() {
this.mapView && this.mapView.cancelAllTasks();
}
requestRedraw() {
this.mapView && this.mapView.getMapRenderer().requestRedraw();
}
screenToMap(pos) {
if (this.mapView) {
if (pos instanceof NTScreenPos) {
return this.fromNativeMapPos(this.mapView.screenToMap(pos));
}
return this.fromNativeMapPos(this.mapView.screenToMap(toNativeScreenPos(pos)));
}
return null;
}
mapToScreen(pos) {
if (this.mapView) {
if (pos instanceof NTMapPos) {
return fromNativeScreenPos(this.mapView.mapToScreen(pos));
}
return fromNativeScreenPos(this.mapView.mapToScreen(toNativeMapPos(pos)));
}
return null;
}
captureRendering(wait = false) {
return new Promise((resolve) => {
this.mapView.getMapRenderer().captureRenderingWaitWhileUpdating(NTRendererCaptureListenerImpl.initWithCallback(new WeakRef(function (bitmap) {
resolve(new ImageSource(bitmap));
})), wait);
});
}
}
CartoMap.projection = new EPSG4326();
export class Layers extends BaseLayers {
count() {
return this.native.count();
}
insert(index, layer) {
return this.native.insertLayer(index, layer.getNative());
}
//@ts-ignore
set(index, layer) {
return this.native.setLayer(index, layer.getNative());
}
removeAll(layers) {
layers.forEach(this.remove);
}
remove(layer) {
return this.native.remove(layer.getNative());
}
add(layer) {
return this.native.add(layer.getNative());
}
//@ts-ignore
get(index) {
return this.native.get(index);
}
addAll(layers) {
layers.forEach(this.add);
}
setAll(layers) {
this.clear();
this.addAll(layers);
}
getAll() {
return nativeVectorToArray(this.native.getAll());
}
clear() {
return this.native.clear();
}
}
//# sourceMappingURL=index.ios.js.map