Skip to content

Commit 64e92fa

Browse files
committed
Squashed commit of the following:
commit 82616b6 Author: Osei Fortune <fortune.osei@yahoo.com> Date: Mon Jan 10 15:10:53 2022 -0400 chore(google-maps): support camera position props in view commit 2c82a65 Author: Osei Fortune <fortune.osei@yahoo.com> Date: Sun Jan 9 15:18:08 2022 -0400 fix(google-maps): camera position commit 5ac5731 Author: Osei Fortune <fortune.osei@yahoo.com> Date: Sat Jan 8 05:36:28 2022 -0400 feat: google maps
1 parent a5df153 commit 64e92fa

File tree

5 files changed

+345
-39
lines changed

5 files changed

+345
-39
lines changed

packages/google-maps/common.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import {ContentView} from '@nativescript/core';
1+
import { ContentView, Property } from '@nativescript/core';
2+
import { Coordinate } from '.';
23

34
export enum MapType {
45
None = 'none',
@@ -14,6 +15,26 @@ export enum JointType {
1415
Default = 'default'
1516
}
1617

18+
export const latProperty = new Property<MapViewBase, number>({
19+
name: "lat"
20+
});
21+
22+
export const lngProperty = new Property<MapViewBase, number>({
23+
name: "lng"
24+
});
25+
26+
export const zoomProperty = new Property<MapViewBase, number>({
27+
name: "zoom"
28+
});
29+
30+
export const bearingProperty = new Property<MapViewBase, number>({
31+
name: 'bearing'
32+
})
33+
34+
export const tiltProperty = new Property<MapViewBase, number>({
35+
name: 'tilt'
36+
})
37+
1738
export class MapViewBase extends ContentView {
1839
static readyEvent = 'ready';
1940
static mapTapEvent = 'mapTap';
@@ -48,4 +69,16 @@ export class MapViewBase extends ContentView {
4869
static activeBuildingEvent = 'activeBuilding';
4970
static activeLevelEvent = 'activeLevel';
5071

72+
lat: number;
73+
lng: number;
74+
zoom: number;
75+
bearing: number;
76+
tilt: number;
77+
5178
}
79+
80+
latProperty.register(MapViewBase);
81+
lngProperty.register(MapViewBase);
82+
zoomProperty.register(MapViewBase);
83+
bearingProperty.register(MapViewBase);
84+
tiltProperty.register(MapViewBase);

packages/google-maps/index.android.ts

Lines changed: 153 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import {Application, Color, EventData, ImageSource, Utils, View} from "@nativescript/core";
1+
import { Application, Color, EventData, ImageSource, Utils, View } from "@nativescript/core";
2+
import { isNullOrUndefined } from "@nativescript/core/utils/types";
23
import {
34
ActiveBuildingEvent,
45
ActiveLevelEvent,
@@ -43,7 +44,7 @@ import {
4344
Style,
4445
TileOverlayOptions
4546
} from ".";
46-
import {JointType, MapType, MapViewBase} from "./common";
47+
import { bearingProperty, JointType, latProperty, lngProperty, MapType, MapViewBase, tiltProperty, zoomProperty } from "./common";
4748

4849
import {
4950
intoNativeMarkerOptions,
@@ -84,7 +85,7 @@ export class MapView extends MapViewBase {
8485
createdBundle = null;
8586
savedBundle = null;
8687
#listener: com.google.android.gms.maps.OnMapReadyCallback;
87-
88+
_map: com.google.android.gms.maps.GoogleMap;
8889
constructor() {
8990
super();
9091
const ref = new WeakRef(this);
@@ -158,6 +159,7 @@ export class MapView extends MapViewBase {
158159
const ref = new WeakRef(this);
159160
this.#listener = new com.google.android.gms.maps.OnMapReadyCallback({
160161
onMapReady(map: com.google.android.gms.maps.GoogleMap): void {
162+
const owner = ref.get?.();
161163
(<any>org).nativescript.plugins.google_maps.GoogleMaps.registerMapListeners(map, new (<any>org).nativescript.plugins.google_maps.GoogleMaps.Callback({
162164
onCameraEvent(position: com.google.android.gms.maps.model.CameraPosition, event: string, isGesture: boolean) {
163165
if (event === 'start') {
@@ -309,28 +311,42 @@ export class MapView extends MapViewBase {
309311

310312
},
311313
onInfoWindowAdapterEvent(marker: com.google.android.gms.maps.model.Marker, event: string) {
312-
let info = <MarkerInfoEvent>{
313-
eventName: '',
314-
object: ref?.get?.(),
315-
marker: Marker.fromNative(marker),
316-
view: null
317-
};
318-
319-
if (event === 'contents') {
320-
info.eventName = MapView.markerInfoContentsEvent;
321-
} else if (event === 'window') {
322-
info.eventName = MapView.markerInfoWindowEvent;
323-
} else {
324-
info = null;
325-
}
314+
const owner: MapView = ref.get?.();
315+
if (owner) {
316+
let info = <MarkerInfoEvent>{
317+
eventName: '',
318+
object: ref?.get?.(),
319+
marker: Marker.fromNative(marker),
320+
view: null
321+
};
322+
323+
if (event === 'contents') {
324+
info.eventName = MapView.markerInfoContentsEvent;
325+
} else if (event === 'window') {
326+
info.eventName = MapView.markerInfoWindowEvent;
327+
} else {
328+
info = null;
329+
}
326330

327-
if (info) {
328-
ref?.get?.().notify?.(info);
329-
if (info.view instanceof View) {
330-
if (!info.view?.nativeView) {
331-
ref?.get?.()._addView?.(info.view);
331+
if (info) {
332+
owner.notify(info);
333+
if (info.view instanceof View) {
334+
if (!info.view.parent && !info.view?.nativeView) {
335+
owner._addView(info.view);
336+
}
337+
if (info.view.nativeView && !(<any>marker)._view) {
338+
(<any>marker)._view = new android.widget.RelativeLayout(owner._context);
339+
}
340+
const parent = info.view.nativeView?.getParent?.();
341+
if (info.view.nativeView && parent !== (<any>marker)._view) {
342+
if (parent && parent.removeView) {
343+
parent.removeView(info.view.nativeView);
344+
}
345+
const container: android.widget.RelativeLayout = (<any>marker)._view;
346+
container.addView(info.view.nativeView);
347+
}
348+
return (<any>marker)?._view ?? null;
332349
}
333-
return info.view?.nativeView ?? null;
334350
}
335351
}
336352

@@ -355,6 +371,18 @@ export class MapView extends MapViewBase {
355371
}
356372
},
357373
}));
374+
375+
if (owner) {
376+
owner._map = map;
377+
owner._updateCamera(map, {
378+
lat: owner.lat,
379+
lng: owner.lng,
380+
bearing: owner.bearing,
381+
tilt: owner.tilt,
382+
zoom: owner.zoom
383+
})
384+
}
385+
358386
ref.get?.().notify?.({
359387
eventName: 'ready',
360388
object: ref.get?.(),
@@ -368,6 +396,99 @@ export class MapView extends MapViewBase {
368396
nativeView.getMapAsync(this.#listener);
369397
return nativeView;
370398
}
399+
400+
[latProperty.setNative](value) {
401+
if (this._map) {
402+
this._updateCamera(this._map, {
403+
lat: value
404+
})
405+
}
406+
}
407+
408+
[lngProperty.setNative](value) {
409+
if (this._map) {
410+
this._updateCamera(this._map, {
411+
lng: value
412+
})
413+
}
414+
}
415+
416+
417+
[zoomProperty.setNative](value) {
418+
if (this._map) {
419+
this._updateCamera(this._map, {
420+
zoom: value
421+
})
422+
}
423+
}
424+
425+
[tiltProperty.setNative](value) {
426+
if (this._map) {
427+
this._updateCamera(this._map, {
428+
tilt: value
429+
})
430+
}
431+
}
432+
433+
434+
[bearingProperty.setNative](value) {
435+
if (this._map) {
436+
this._updateCamera(this._map, {
437+
bearing: value
438+
})
439+
}
440+
}
441+
442+
443+
_updateCamera(map, owner: {
444+
lat?,
445+
lng?,
446+
zoom?,
447+
tilt?,
448+
bearing?
449+
}) {
450+
const googleMap = GoogleMap.fromNative(map);
451+
if (googleMap) {
452+
const position = CameraPosition.fromNative(map.getCameraPosition());
453+
454+
let changed = false;
455+
if (!isNullOrUndefined(owner.lat)) {
456+
position.target = {
457+
lat: typeof owner.lat === 'string' ? parseFloat(owner.lat) : owner.lat,
458+
lng: position.target.lng
459+
}
460+
changed = true;
461+
}
462+
463+
if (!isNullOrUndefined(owner.lng)) {
464+
position.target = {
465+
lat: position.target.lat,
466+
lng: typeof owner.lng === 'string' ? parseFloat(owner.lng) : owner.lng
467+
}
468+
changed = true;
469+
}
470+
471+
if (!isNullOrUndefined(owner.zoom)) {
472+
position.zoom = typeof owner.zoom === 'string' ? parseFloat(owner.zoom) : owner.zoom;
473+
changed = true;
474+
}
475+
476+
if (!isNullOrUndefined(owner.tilt)) {
477+
position.tilt = typeof owner.tilt === 'string' ? parseFloat(owner.tilt) : owner.tilt;
478+
changed = true;
479+
}
480+
481+
if (!isNullOrUndefined(owner.bearing)) {
482+
position.bearing = typeof owner.bearing === 'string' ? parseFloat(owner.bearing) : owner.bearing;
483+
changed = true;
484+
}
485+
486+
if (changed) {
487+
googleMap.cameraPosition = position;
488+
}
489+
490+
}
491+
}
371492
}
372493

373494
export class IndoorLevel implements IIndoorLevel {
@@ -559,7 +680,7 @@ export class GoogleMap implements IGoogleMap {
559680

560681
set cameraPosition(value) {
561682
this.native.moveCamera(
562-
CameraUpdate.fromCameraPosition(value)
683+
CameraUpdate.fromCameraPosition(value).native
563684
)
564685
}
565686

@@ -1055,6 +1176,13 @@ export class Marker implements IMarker {
10551176
this.native.setZIndex(value);
10561177
}
10571178

1179+
hideInfoWindow() {
1180+
this.native.hideInfoWindow();
1181+
}
1182+
1183+
showInfoWindow() {
1184+
this.native.showInfoWindow();
1185+
}
10581186
}
10591187

10601188
export class Circle implements ICircle {
@@ -1817,7 +1945,7 @@ export class Projection implements IProjection {
18171945
coordinate.lng
18181946
)
18191947
);
1820-
return {x: point.x, y: point.y};
1948+
return { x: point.x, y: point.y };
18211949
}
18221950
}
18231951

packages/google-maps/index.d.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ export class CameraPosition implements ICameraPosition, Partial<NativeObject> {
245245
zoom: number;
246246
}
247247

248-
export interface IMapView {
248+
export class MapView extends MapViewBase {
249249
on(event: 'ready', callback: (args: MapReadyEvent) => void, thisArg?: any);
250250

251251
on(event: 'mapTap', callback: (args: MapTapEvent) => void, thisArg?: any);
@@ -270,9 +270,29 @@ export interface IMapView {
270270

271271
on(event: 'cameraPosition', callback: (args: CameraPositionEvent) => void, thisArg?: any);
272272

273-
}
273+
on(event: 'circle', callback: (args: CircleTapEvent) => void, thisArg?: any);
274+
275+
on(event: 'polygon', callback: (args: PolygonTapEvent) => void, thisArg?: any);
276+
277+
on(event: 'polyline', callback: (args: PolylineTapEvent) => void, thisArg?: any);
278+
279+
on(event: 'poi', callback: (args: PoiTapEvent) => void, thisArg?: any);
280+
281+
on(event: 'groundOverlay', callback: (args: GroundOverlayTapEvent) => void, thisArg?: any);
282+
283+
on(event: 'infoWindowTap', callback: (args: InfoWindowEvent) => void, thisArg?: any);
284+
285+
on(event: 'infoWindowLongPress', callback: (args: InfoWindowEvent) => void, thisArg?: any);
274286

275-
export class MapView extends MapViewBase implements IMapView {
287+
on(event: 'infoWindowClose', callback: (args: InfoWindowEvent) => void, thisArg?: any);
288+
289+
on(event: 'markerInfoContents', callback: (args: MarkerInfoEvent) => void, thisArg?: any);
290+
291+
on(event: 'markerInfoWindow', callback: (args: MarkerInfoEvent) => void, thisArg?: any);
292+
293+
on(event: 'activeBuilding', callback: (args: ActiveBuildingEvent) => void, thisArg?: any);
294+
295+
on(event: 'activeLevel', callback: (args: ActiveLevelEvent) => void, thisArg?: any);
276296
}
277297

278298
export interface Coordinate {
@@ -312,6 +332,10 @@ export class Marker implements IMarker, Partial<NativeObject> {
312332
rotation: number;
313333
flat: boolean;
314334
zIndex: number;
335+
336+
hideInfoWindow(): void;
337+
338+
showInfoWindow(): void;
315339
}
316340

317341
export interface ILocationSource {

0 commit comments

Comments
 (0)