Skip to content

Commit d4f287b

Browse files
committed
chore: bunch of new features
1 parent 1f8363f commit d4f287b

24 files changed

+206
-18
lines changed

src/core/index.android.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,15 @@ export const ClickType = {
3030
};
3131

3232
export class MapBounds<T = DefaultLatLonKeys> extends BaseNative<com.carto.core.MapBounds, {}> {
33-
constructor(public northeast: GenericMapPos<T>, public southwest: GenericMapPos<T>, native?: com.carto.core.MapBounds) {
33+
constructor(public northeast?: GenericMapPos<T>, public southwest?: GenericMapPos<T>, native?: com.carto.core.MapBounds) {
3434
super(undefined, native);
3535
}
3636
createNative() {
37-
return new com.carto.core.MapBounds(toNativeMapPos<T>(this.southwest), toNativeMapPos<T>(this.northeast));
37+
if (this.southwest && this.northeast) {
38+
return new com.carto.core.MapBounds(toNativeMapPos<T>(this.southwest), toNativeMapPos<T>(this.northeast));
39+
} else {
40+
return new com.carto.core.MapBounds();
41+
}
3842
}
3943
contains(position: GenericMapPos<T> | MapBounds<T>) {
4044
if (position['southwest']) {
@@ -46,6 +50,9 @@ export class MapBounds<T = DefaultLatLonKeys> extends BaseNative<com.carto.core.
4650
intersects(position: MapBounds) {
4751
return this.getNative().intersects(toNativeMapBounds(position));
4852
}
53+
shrinkToIntersection(position: MapBounds) {
54+
return this.getNative().shrinkToIntersection(toNativeMapBounds(position));
55+
}
4956
equals(position: MapBounds) {
5057
return this.getNative().equals(toNativeMapBounds(position));
5158
}

src/core/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export class IntVector extends NativeVector<number> {}
6060
export class MapBounds<T = DefaultLatLonKeys> extends BaseNative<any, {}> {
6161
public northeast: GenericMapPos<T>;
6262
public southwest: GenericMapPos<T>;
63-
constructor(northeast: GenericMapPos<T>, southwest: GenericMapPos<T>);
63+
constructor(northeast?: GenericMapPos<T>, southwest?: GenericMapPos<T>);
6464
contains(position: GenericMapPos<T> | MapBounds): boolean;
6565
intersects(position: MapBounds<T>): boolean;
6666
equals(position: MapBounds<T>): boolean;

src/core/index.ios.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ export enum ClickType {
1717

1818

1919
export class MapBounds<T = DefaultLatLonKeys> extends BaseNative<NTMapBounds, {}> {
20-
constructor(public northeast: GenericMapPos<T>, public southwest: GenericMapPos<T>, native?: NTMapBounds) {
20+
constructor(public northeast?: GenericMapPos<T>, public southwest?: GenericMapPos<T>, native?: NTMapBounds) {
2121
super(undefined, native);
2222
}
2323
createNative() {
24-
return NTMapBounds.alloc().initWithMinMax(toNativeMapPos(this.southwest), toNativeMapPos(this.northeast));
24+
if (this.southwest && this.northeast) {
25+
return NTMapBounds.alloc().initWithMinMax(toNativeMapPos<T>(this.southwest), toNativeMapPos<T>(this.northeast));
26+
} else {
27+
return NTMapBounds.alloc().init();
28+
}
2529
}
2630
contains(position: GenericMapPos<T> | MapBounds<T>) {
2731
if (position['southwest']) {
@@ -33,6 +37,9 @@ export class MapBounds<T = DefaultLatLonKeys> extends BaseNative<NTMapBounds, {}
3337
intersects(position: MapBounds<T>) {
3438
return this.getNative().intersects(toNativeMapBounds<T>(position));
3539
}
40+
shrinkToIntersection(position: MapBounds) {
41+
return this.getNative().shrinkToIntersection(toNativeMapBounds(position));
42+
}
3643
equals(position: MapBounds<T>) {
3744
return this.getNative().isEqualInternal(toNativeMapBounds<T>(position));
3845
}
@@ -119,6 +126,9 @@ export function toNativeMapBounds<T = DefaultLatLonKeys>(bounds: MapBounds<T>) {
119126
if (bounds instanceof NTMapBounds) {
120127
return bounds;
121128
}
129+
if (typeof bounds.getNative === 'function') {
130+
return bounds.getNative();
131+
}
122132
return NTMapBounds.alloc().initWithMinMax(toNativeMapPos<T>(bounds.southwest), toNativeMapPos<T>(bounds.northeast));
123133
}
124134

src/geometry/feature.android.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Feature, FeatureCollection as IFeatureCollection, VectorTileFeature } from './feature';
22
import { nativeVariantToJS } from '../utils';
33
import { Geometry } from '.';
4+
import { LatitudeKey, LongitudeKey, MapBounds, fromNativeMapBounds } from '../core';
45

56
export class FeatureCollection implements IFeatureCollection {
67
constructor(protected native: com.carto.geometry.FeatureCollection) {}
@@ -24,6 +25,31 @@ export class FeatureCollection implements IFeatureCollection {
2425
getNative() {
2526
return this.native;
2627
}
28+
getBounds() {
29+
let minLat = Number.MAX_SAFE_INTEGER;
30+
let minLon = Number.MAX_SAFE_INTEGER;
31+
let maxLat = -Number.MAX_SAFE_INTEGER;
32+
let maxLon = -Number.MAX_SAFE_INTEGER;
33+
const featureCount = this.featureCount;
34+
for (let index = 0; index < featureCount; index++) {
35+
const geometry = this.getGeometry(index);
36+
const bounds = fromNativeMapBounds(geometry.getBounds());
37+
minLat = Math.min(minLat, bounds.northeast[LatitudeKey]);
38+
minLon = Math.min(minLon, bounds.northeast[LongitudeKey]);
39+
maxLat = Math.max(maxLat, bounds.southwest[LatitudeKey]);
40+
maxLon = Math.max(maxLon, bounds.southwest[LongitudeKey]);
41+
}
42+
return new MapBounds(
43+
{
44+
[LatitudeKey]: minLat,
45+
[LongitudeKey]: minLon,
46+
},
47+
{
48+
[LatitudeKey]: maxLat,
49+
[LongitudeKey]: maxLon,
50+
}
51+
);
52+
}
2753
}
2854

2955
export class VectorTileFeatureCollection extends FeatureCollection {

src/geometry/feature.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export class FeatureCollection<T = DefaultLatLonKeys> {
2121
getFeatureCount(): number;
2222
readonly featureCount: number;
2323
getNative();
24+
getBounds(): MapBounds<T>
2425
}
2526
export class VectorTileFeatureCollection<T = DefaultLatLonKeys> extends FeatureCollection {
2627
getFeature(index: number): VectorTileFeature<T>;

src/geometry/feature.ios.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Feature, FeatureCollection as IFeatureCollection, VectorTileFeature } from './feature';
22
import { nativeVariantToJS } from '../utils';
33
import { Geometry } from '.';
4-
import { DefaultLatLonKeys } from '../core';
4+
import { DefaultLatLonKeys, fromNativeMapBounds, LatitudeKey, LongitudeKey, MapBounds } from '../core';
55

66
export class FeatureCollection implements IFeatureCollection {
77
constructor(protected native: NTFeatureCollection) {}
@@ -27,6 +27,31 @@ export class FeatureCollection implements IFeatureCollection {
2727
getNative() {
2828
return this.native;
2929
}
30+
getBounds() {
31+
let minLat = Number.MAX_SAFE_INTEGER;
32+
let minLon = Number.MAX_SAFE_INTEGER;
33+
let maxLat = -Number.MAX_SAFE_INTEGER;
34+
let maxLon = -Number.MAX_SAFE_INTEGER;
35+
const featureCount = this.featureCount;
36+
for (let index = 0; index < featureCount; index++) {
37+
const geometry = this.getGeometry(index);
38+
const bounds = fromNativeMapBounds(geometry.getBounds());
39+
minLat = Math.min(minLat, bounds.northeast[LatitudeKey]);
40+
minLon = Math.min(minLon, bounds.northeast[LongitudeKey]);
41+
maxLat = Math.max(maxLat, bounds.southwest[LatitudeKey]);
42+
maxLon = Math.max(maxLon, bounds.southwest[LongitudeKey]);
43+
}
44+
return new MapBounds(
45+
{
46+
[LatitudeKey]: minLat,
47+
[LongitudeKey]: minLon,
48+
},
49+
{
50+
[LatitudeKey]: maxLat,
51+
[LongitudeKey]: maxLon,
52+
}
53+
);
54+
}
3055
}
3156

3257
export class VectorTileFeatureCollection extends FeatureCollection {

src/geometry/writer.android.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { FeatureCollection } from './feature';
44
import { Projection } from '../projections';
55
import { Geometry } from '.';
66
import { MapPosVector } from '../core';
7-
import { mapPosVectorFromArgs, nativeProperty } from '..';
7+
import { featureCollectionFromArgs, mapPosVectorFromArgs, nativeProperty } from '..';
88

99
export class GeoJSONGeometryWriter extends BaseNative<com.carto.geometry.GeoJSONGeometryWriter, GeoJSONGeometryWriterOptions> {
1010
createNative() {
@@ -13,6 +13,9 @@ export class GeoJSONGeometryWriter extends BaseNative<com.carto.geometry.GeoJSON
1313
writePoses(value: MapPosVector) {
1414
this.getNative().writeGeometry(new com.carto.geometry.LineGeometry(mapPosVectorFromArgs(value)));
1515
}
16+
writeFeatureCollection(value: FeatureCollection) {
17+
this.getNative().writeFeatureCollection(featureCollectionFromArgs(value));
18+
}
1619
set sourceProjection(value: Projection) {
1720
this.native && this.native.setSourceProjection(value.getNative());
1821
}

src/geometry/writer.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ export interface GeoJSONGeometryWriterOptions {
77
}
88
export class GeoJSONGeometryWriter<T = DefaultLatLonKeys> extends BaseNative<any, GeoJSONGeometryWriterOptions> {
99
targetProjection?: Projection;
10-
writePoses(value: MapPosVector);
10+
writePoses(value: MapPosVector): string;
11+
writeFeatureCollection(value: FeatureCollection): string;
1112
}
1213

1314
export interface WKBGeometryWriterOptions {

src/geometry/writer.ios.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { BaseNative } from '../index.common';
22
import { Projection } from '../projections';
33
import { MapPosVector } from '../core';
4-
import { mapPosVectorFromArgs, nativeProperty } from '..';
4+
import { featureCollectionFromArgs, mapPosVectorFromArgs, nativeProperty } from '..';
55
import { GeoJSONGeometryWriterOptions, WKBGeometryWriterOptions, WKTGeometryWriterOptions } from './writer';
66
import { Geometry } from '.';
7+
import { FeatureCollection } from './feature';
78

89
export class GeoJSONGeometryWriter extends BaseNative<NTGeoJSONGeometryWriter, GeoJSONGeometryWriterOptions> {
910
createNative() {
@@ -12,6 +13,9 @@ export class GeoJSONGeometryWriter extends BaseNative<NTGeoJSONGeometryWriter, G
1213
writePoses(value: MapPosVector) {
1314
this.getNative().writeGeometry(new NTLineGeometry(mapPosVectorFromArgs(value)));
1415
}
16+
writeFeatureCollection(value: FeatureCollection) {
17+
this.getNative().writeFeatureCollection(featureCollectionFromArgs(value));
18+
}
1519
set sourceProjection(value: Projection) {
1620
this.native && this.native.setSourceProjection(value.getNative());
1721
}

src/index.android.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { Color } from '@nativescript/core/color';
33
import { NativePropertyOptions } from '.';
44
import { BaseNative, _createImageSourceFromSrc, nativeProperty } from './index.common';
55
import { DefaultLatLonKeys, GenericMapPos, MapPos, MapPosVector, MapPosVectorVector, toNativeMapPos } from './core';
6+
import { FeatureCollection } from './geometry/feature';
7+
import { Geometry } from './geometry';
68
export { BaseNative, nativeProperty };
79

810
export function nativeColorProperty(target: any, k?, desc?: PropertyDescriptor): any;
@@ -83,6 +85,29 @@ export function nativeImageProperty(...args) {
8385
...args
8486
);
8587
}
88+
export function featureCollectionFromArgs<T = DefaultLatLonKeys>(collection: FeatureCollection<T>) {
89+
if (!collection) {
90+
return null;
91+
}
92+
let nativeCollection: com.carto.geometry.FeatureCollection = collection as any;
93+
94+
if (typeof (collection as any).getNative === 'function') {
95+
nativeCollection = collection.getNative();
96+
}
97+
return nativeCollection;
98+
}
99+
100+
export function geometryFromArgs<T = DefaultLatLonKeys>(geometry: Geometry<T>) {
101+
if (!geometry) {
102+
return null;
103+
}
104+
let nativegeometry: com.carto.geometry.Geometry = geometry as any;
105+
106+
if (typeof (geometry as any).getNative === 'function') {
107+
nativegeometry = geometry.getNative();
108+
}
109+
return nativegeometry;
110+
}
86111

87112
export function mapPosVectorFromArgs<T = DefaultLatLonKeys>(positions: MapPosVector<T> | GenericMapPos<T>[] | com.carto.core.MapPosVector, ignoreAltitude = true) {
88113
if (!positions) {

0 commit comments

Comments
 (0)