Skip to content

fix: added missing js reference for map layers #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 18 additions & 51 deletions src/ui-carto/ui/index.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ export class CartoMap<T = DefaultLatLonKeys> extends CartoViewBase {
};
mProjection: IProjection;

get mapView() {
return this.nativeViewProtected;
override get mapView(): com.akylas.carto.additions.AKMapView {
return super.mapView;
}

get projection() {
return this.mProjection;
}
Expand Down Expand Up @@ -229,42 +230,10 @@ export class CartoMap<T = DefaultLatLonKeys> extends CartoViewBase {
this.mapView.getOptions().setRestrictedPanning(value);
}

getLayers() {
if (this.mapView) {
return new Layers(this.mapView.getLayers());
}
return null;
}
addLayer(layer: TileLayer<any, any>, index?: number) {
if (this.mapView) {
const native: com.carto.layers.TileLayer = 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);
}
}
}
createLayersInstance(): Layers {
return new Layers(this.mapView.getLayers());
}

removeLayer(layer: TileLayer<any, any>) {
if (this.mapView) {
this.mapView.getLayers().remove(layer.getNative());
}
}
removeAllLayers(layers: TileLayer<any, any>[]) {
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();
}
Expand Down Expand Up @@ -324,37 +293,35 @@ export class Layers extends BaseLayers<com.carto.components.Layers> {
return this.native.count();
}
insert(index: number, layer: Layer<any, any>) {
return this.native.insert(index, layer.getNative());
super.insert(index, layer);
this.native.insert(index, layer.getNative());
}
//@ts-ignore
set(index: number, layer: Layer<any, any>) {
return this.native.set(index, layer.getNative());
}
removeAll(layers: Layer<any, any>[]) {
layers.forEach(this.remove);
super.set(index, layer);
this.native.set(index, layer.getNative());
}
remove(layer: Layer<any, any>) {
return this.native.remove(layer.getNative());
const removed = this.native.remove(layer.getNative());
if (removed) {
super.remove(layer);
}
return removed;
}
add(layer: Layer<any, any>) {
return this.native.add(layer.getNative());
super.add(layer);
this.native.add(layer.getNative());
}
//@ts-ignore
get(index: number) {
return this.native.get(index);
}
addAll(layers: Layer<any, any>[]) {
layers.forEach(this.add);
}
setAll(layers: Layer<any, any>[]) {
this.clear();
this.addAll(layers);
}
getAll() {
return nativeVectorToArray(this.native.getAll());
}
clear() {
return this.native.clear();
super.clear();
this.native.clear();
}

// public getNative() {
Expand Down
104 changes: 96 additions & 8 deletions src/ui-carto/ui/index.common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,64 @@ export function mapProperty(...args) {
}

export abstract class Layers<T = any> extends BaseNative<T, {}> {
private readonly mLayerArray: Layer<any, any>[] = [];

constructor(native) {
super(null, native);
}

abstract count(): number;
abstract insert(index: number, layer: Layer<any, any>);
abstract removeAll(layers: Layer<any, any>[]);
abstract remove(layer: Layer<any, any>);
abstract add(layer: Layer<any, any>);

insert(index: number, layer: Layer<any, any>) {
this.mLayerArray.splice(index, 0, layer);
}

//@ts-ignore
abstract set(index: number, layer: Layer<any, any>);
set(index: number, layer: Layer<any, any>) {
this.mLayerArray[index] = layer;
}

removeAll(layers: Layer<any, any>[]): boolean {
let hasRemovedAll: boolean = true;

layers.forEach((layer) => {
if (!this.remove(layer)) {
if (hasRemovedAll) {
hasRemovedAll = false;
}
}
});
return hasRemovedAll;
}

remove(layer: Layer<any, any>): boolean {
const index = this.mLayerArray.indexOf(layer);
if (index >= 1) {
this.mLayerArray.splice(index, 1);
return true;
}
return false;
}

add(layer: Layer<any, any>) {
this.mLayerArray.push(layer);
}

addAll(layers: Layer<any, any>[]) {
layers.forEach((layer) => this.add(layer));
}
setAll(layers: Layer<any, any>[]) {
this.clear();
this.addAll(layers);
}

//@ts-ignore
abstract get(index: number): Layer<any, any>;
abstract addAll(layers: Layer<any, any>[]);
abstract setAll(layers: Layer<any, any>[]);
abstract getAll(): Layer<any, any>[];
abstract clear();

clear() {
this.mLayerArray.splice(0);
}

// public getNative() {
// return this.native;
Expand Down Expand Up @@ -123,6 +165,12 @@ export abstract class CartoViewBase extends ContentView {
@mapProperty maxZoom: number;
@mapProperty restrictedPanning: boolean;

private mLayers: Layers;

get mapView() {
return this.nativeViewProtected;
}

public sendEvent<T extends MapInfo = MapInfo>(eventName: string, data?: T) {
if (this.hasListeners(eventName)) {
this.notify({
Expand All @@ -140,8 +188,48 @@ export abstract class CartoViewBase extends ContentView {
}
}

abstract createLayersInstance();

getLayers(): Layers {
if (!this.mLayers && this.mapView) {
this.mLayers = this.createLayersInstance();
}
return this.mLayers;
}

addLayer(layer: Layer<any, any>, index?: number) {
const layersInstance = this.getLayers();
if (layersInstance) {
if (index !== undefined && index <= layersInstance.count()) {
layersInstance.insert(index, layer);
} else {
layersInstance.add(layer);
}
}
}

removeLayer(layer: Layer<any, any>) {
const layersInstance = this.getLayers();
if (layersInstance) {
layersInstance.remove(layer);
}
}

removeAllLayers(layers: Layer<any, any>[]) {
const layersInstance = this.getLayers();
if (layersInstance) {
layersInstance.removeAll(layers);
}
}

disposeNativeView() {
this.mapReady = false;

if (this.mLayers) {
this.mLayers.clear();
this.mLayers = null;
}

super.disposeNativeView();
}

Expand Down
18 changes: 16 additions & 2 deletions src/ui-carto/ui/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { EventData, ImageSource, Style, View } from '@nativescript/core';
import { ClickType, DefaultLatLonKeys, GenericMapPos, MapBounds, ScreenBounds, ScreenPos } from '../core';
import { Layer } from '../layers';
import { Projection } from '../projections';
import { Layers } from './index.common';
export { Layers };

export enum RenderProjectionMode {
RENDER_PROJECTION_MODE_PLANAR,
Expand Down Expand Up @@ -207,6 +205,20 @@ export class MapOptions {
isLayersLabelsProcessedInReverseOrder(): boolean;
}

export class Layers<T = any> {
abstract count(): number;
abstract insert(index: number, layer: Layer<any, any>): void;
abstract removeAll(layers: Layer<any, any>[]): boolean;
abstract remove(layer: Layer<any, any>): boolean;
abstract add(layer: Layer<any, any>): void;
abstract set(index: number, layer: Layer<any, any>): void;
abstract get(index: number): Layer<any, any>;
abstract addAll(layers: Layer<any, any>[]): void;
abstract setAll(layers: Layer<any, any>[]): void;
abstract getAll(): Layer<any, any>[];
abstract clear(): void;
}

interface CartoMapStyle extends Style {
zoom: number;
focusPos: GenericMapPos;
Expand All @@ -232,10 +244,12 @@ export class CartoMap<T = DefaultLatLonKeys> extends View {
bearing: number;
tilt: number;
restrictedPanning: boolean;
readonly mapView: any;
readonly metersPerPixel: number;

addLayer(layer: Layer<any, any>, index?: number);
removeLayer(layer: Layer<any, any>);
removeAllLayers(layers: Layer<any, any>[]);
getLayers(): Layers<any>;
screenToMap(pos: ScreenPos | any): GenericMapPos<T>;
mapToScreen(pos: GenericMapPos<T> | any): ScreenPos;
Expand Down
Loading