@nativescript-community/ui-carto
Version:
NativeScript plugin for CARTO Mobile SDK
320 lines • 11.5 kB
JavaScript
import { MapBounds, fromNativeMapBounds, fromNativeMapPos, fromNativeScreenPos, nativeVectorToArray, toNativeMapBounds, toNativeMapPos, toNativeScreenBounds, toNativeScreenPos } from '../core';
import { restrictedPanningProperty } from './cssproperties';
import { Layers as BaseLayers, CartoViewBase, MapClickedEvent, MapIdleEvent, MapInteractionEvent, MapMovedEvent, MapReadyEvent, MapStableEvent } from './index.common';
import { ImageSource, Property, booleanConverter } from '@nativescript/core';
import { EPSG4326 } from '../projections/epsg4326';
export { MapClickedEvent, MapIdleEvent, MapMovedEvent, MapReadyEvent, MapStableEvent };
export const RenderProjectionMode = {
get RENDER_PROJECTION_MODE_PLANAR() {
return com.carto.components.RenderProjectionMode.RENDER_PROJECTION_MODE_PLANAR;
},
get RENDER_PROJECTION_MODE_SPHERICAL() {
return com.carto.components.RenderProjectionMode.RENDER_PROJECTION_MODE_SPHERICAL;
}
};
export const PanningMode = {
get PANNING_MODE_FREE() {
return com.carto.components.PanningMode.PANNING_MODE_FREE;
},
get PANNING_MODE_STICKY() {
return com.carto.components.PanningMode.PANNING_MODE_STICKY;
},
get PANNING_MODE_STICKY_FINAL() {
return com.carto.components.PanningMode.PANNING_MODE_STICKY_FINAL;
}
};
export class CartoMap extends CartoViewBase {
static setRunOnMainThread(value) {
com.akylas.carto.additions.AKMapView.setRunOnMainThread(value);
}
get mapView() {
return this.nativeViewProtected;
}
get projection() {
return this.mProjection;
}
set projection(proj) {
this.mProjection = proj;
if (this.nativeViewProtected) {
this.mapView.getOptions().setBaseProjection(proj ? proj.getNative() : null);
}
}
createNativeView() {
let view;
if (this.useTextureView) {
view = new com.akylas.carto.additions.AKTextureMapView(this._context);
}
else {
view = new com.akylas.carto.additions.AKMapView(this._context);
}
return view;
}
onLoaded() {
super.onLoaded();
if (this.nativeViewProtected) {
this.nativeViewProtected.onResume();
}
}
onUnloaded() {
super.onUnloaded();
if (this.nativeViewProtected) {
this.nativeViewProtected.onPause();
}
}
getOptions() {
if (this.mapReady) {
return this.mapView.getOptions();
}
return null;
}
initNativeView() {
super.initNativeView();
if (!this.projection) {
this.projection = new EPSG4326();
}
const listener = new com.akylas.carto.additions.AKMapEventListener({
onMapIdle: () => {
this.sendEvent(MapIdleEvent);
},
onMapMoved: (userAction) => {
this.sendEvent(MapMovedEvent, { userAction });
},
onMapInteraction: (interaction, userAction) => {
this.sendEvent(MapInteractionEvent, {
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();
}
}
});
},
onMapStable: (userAction) => {
this.sendEvent(MapStableEvent, { userAction });
},
onMapClicked: (mapClickInfo) => {
this.sendEvent(MapClickedEvent, {
android: mapClickInfo,
get clickInfo() {
return {
get duration() {
return mapClickInfo.getClickInfo().getDuration();
}
};
},
get clickType() {
// This will return an integer value that can be compared with the actual enum
return mapClickInfo.getClickType().swigValue();
},
get position() {
return fromNativeMapPos(mapClickInfo.getClickPos());
}
});
}
});
this.nativeViewProtected.listener = listener;
this.nativeViewProtected.setMapEventListener(listener);
}
disposeNativeView() {
this.mProjection = null;
this.nativeProjection = null;
if (this.nativeViewProtected.listener) {
this.nativeViewProtected.listener = null;
this.nativeViewProtected.setMapEventListener(null);
}
this.nativeView.owner = null;
super.disposeNativeView();
}
fromNativeMapPos(position) {
return fromNativeMapPos(position);
}
fromNativeMapBounds(position) {
return fromNativeMapBounds(new com.carto.core.MapBounds(position.getMin(), position.getMax()));
}
toNativeMapPos(position) {
return toNativeMapPos(position);
}
toNativeMapBounds(position) {
return toNativeMapBounds(position);
}
setFocusPos(value, duration = 0) {
this.mapView.setFocusPos(toNativeMapPos(value), duration / 1000);
}
getFocusPos() {
return fromNativeMapPos(this.mapView.getFocusPos());
}
getMapBounds() {
const screenBounds = toNativeScreenBounds({ min: { x: this.getMeasuredWidth(), y: 0 }, max: { x: 0, y: this.getMeasuredHeight() } });
return new MapBounds(fromNativeMapPos(this.mapView.screenToMap(screenBounds.getMin())), fromNativeMapPos(this.mapView.screenToMap(screenBounds.getMax())));
}
setMapRotation(value, targetPos, duration = 0) {
if (typeof targetPos === 'number') {
this.mapView.setMapRotation(value, targetPos / 1000);
}
else {
this.mapView.setMapRotation(value, toNativeMapPos(targetPos), duration / 1000);
}
}
getZoom() {
return this.mapView.getZoom();
}
setZoom(value, targetPos, duration = 0) {
if (typeof targetPos === 'number') {
this.mapView.setZoom(value, targetPos / 1000);
}
else {
this.mapView.setZoom(value, toNativeMapPos(targetPos), duration / 1000);
}
}
setTilt(value, duration = 0) {
this.mapView.setTilt(value, duration / 1000);
}
setBearing(value, duration = 0) {
this.mapView.setMapRotation(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.moveToFitBounds(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) {
try {
const layers = this.mapView.getLayers();
if (index !== undefined && index < layers.count()) {
layers.insert(index, native);
}
else {
layers.add(native);
}
}
catch (error) {
console.error(error);
}
}
}
}
removeLayer(layer) {
if (this.mapView) {
this.mapView.getLayers().remove(layer.getNative());
}
}
removeAllLayers(layers) {
if (this.mapView) {
const vector = new com.carto.layers.LayerVector();
layers.forEach((l) => vector.add(l.getNative()));
this.mapView.getLayers().removeAll(vector);
}
}
clearAllCaches() {
this.mapView && this.mapView.clearAllCaches();
}
clearPreloadingCaches() {
this.mapView && this.mapView.clearPreloadingCaches();
}
requestRedraw() {
this.mapView && this.mapView.getMapRenderer().requestRedraw();
}
cancelAllTasks() {
this.mapView && this.mapView.cancelAllTasks();
}
screenToMap(pos) {
if (this.mapView) {
if (pos instanceof com.carto.core.ScreenPos) {
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 com.carto.core.MapPos) {
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().captureRendering(new com.akylas.carto.additions.RendererCaptureListener(new com.akylas.carto.additions.RendererCaptureListener.Listener({
onMapRendered(bitmap) {
resolve(new ImageSource(com.carto.utils.BitmapUtils.createAndroidBitmapFromBitmap(bitmap)));
}
})), wait);
});
}
}
export const useTextureViewProperty = new Property({
defaultValue: false,
name: 'useTextureView',
valueConverter: booleanConverter
});
useTextureViewProperty.register(CartoMap);
export class Layers extends BaseLayers {
count() {
return this.native.count();
}
insert(index, layer) {
return this.native.insert(index, layer.getNative());
}
//@ts-ignore
set(index, layer) {
return this.native.set(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.android.js.map