1
- import { Color , File , ImageSource , Trace , knownFolders , path , Http , Utils } from '@nativescript/core' ;
1
+ import { Color , File , Http , ImageSource , Trace , Utils , knownFolders , path } from '@nativescript/core' ;
2
2
import {
3
3
AddExtrusionOptions ,
4
4
AddGeoJsonClusteredOptions ,
@@ -35,7 +35,7 @@ import {
35
35
Viewport ,
36
36
telemetryProperty
37
37
} from './common' ;
38
- import { LayerFactory , Layer } from './layers/layer-factory' ;
38
+ import { Layer , LayerFactory } from './layers/layer-factory' ;
39
39
import { FilterParser } from './filter/filter-parser' ;
40
40
41
41
/**
@@ -422,66 +422,79 @@ class MapLongPressHandlerImpl extends NSObject {
422
422
/**
423
423
* pan handler
424
424
*
425
- * This is used by the OnScrollListener
425
+ * This is used by scroll listeners
426
426
*/
427
427
@NativeClass
428
428
class MapPanHandlerImpl extends NSObject {
429
429
private _owner : WeakRef < Mapbox > ;
430
- private _listener : ( data ?: LatLng ) => void ;
431
- private onMoveBegin : boolean ;
430
+ private _listener : Map < UIGestureRecognizerState , ( data ?: LatLng ) => void > ;
432
431
private _mapView : MGLMapView ;
433
432
434
- public static initWithOwnerAndListenerForMap ( owner : WeakRef < Mapbox > , listener : ( data ?: LatLng ) => void , mapView : MGLMapView ) : MapPanHandlerImpl {
433
+ public static initWithOwnerAndListenerForMap ( owner : WeakRef < Mapbox > , listener : ( data ?: LatLng ) => void , panState : UIGestureRecognizerState , mapView : MGLMapView ) : MapPanHandlerImpl {
435
434
const handler = MapPanHandlerImpl . new ( ) as MapPanHandlerImpl ;
436
435
handler . _owner = owner ;
437
- handler . _listener = listener ;
436
+ handler . _listener = new Map ( [ [ panState , listener ] ] ) ;
438
437
handler . _mapView = mapView ;
439
438
440
- handler . onMoveBegin = false ;
441
-
442
439
return handler ;
443
440
}
444
441
445
- public setOnMoveBegin ( ) {
446
- this . onMoveBegin = true ;
442
+ public static ObjCExposedMethods = {
443
+ pan : { returns : interop . types . void , params : [ interop . types . id ] } ,
444
+ panEnd : { returns : interop . types . void , params : [ interop . types . id ] } ,
445
+ panBegin : { returns : interop . types . void , params : [ interop . types . id ] }
446
+ } ;
447
+
448
+ public addListener ( panState : UIGestureRecognizerState , listener : ( data ?: LatLng ) => void ) {
449
+ this . _listener . set ( panState , listener ) ;
447
450
}
448
451
449
452
public pan ( recognizer : UIPanGestureRecognizer ) : void {
450
- const panPoint = recognizer . locationInView ( this . _mapView ) ;
451
- const panCoordinate = this . _mapView . convertPointToCoordinateFromView ( panPoint , this . _mapView ) ;
453
+ const panCoordinate = this . getCoordinates ( recognizer ) ;
452
454
453
455
if ( Trace . isEnabled ( ) ) {
454
456
CLog ( CLogTypes . info , 'MapPanHandlerImpl::pan(): top with state:' , recognizer . state ) ;
455
457
}
456
458
457
- // if this is the beginning of the pan simulate the Android onMoveBegin
458
- //
459
- // See the objc platform declarations in objc!UIKit.d.ts. It doesn't quite match the apple documention
459
+ if ( recognizer . state === UIGestureRecognizerState . Changed ) {
460
+ this . notifyListener ( recognizer . state , panCoordinate . latitude , panCoordinate . longitude ) ;
461
+ }
462
+ }
460
463
461
- if ( this . onMoveBegin ) {
462
- if ( recognizer . state === UIGestureRecognizerState . Began ) {
463
- if ( Trace . isEnabled ( ) ) {
464
- CLog ( CLogTypes . info , 'MapPanHandlerImpl::pan(): calling onMoveBegin listener' ) ;
465
- }
464
+ public panEnd ( recognizer : UIPanGestureRecognizer ) : void {
465
+ const panCoordinate = this . getCoordinates ( recognizer ) ;
466
466
467
- this . _listener ( {
468
- lat : panCoordinate . latitude ,
469
- lng : panCoordinate . longitude
470
- } ) ;
471
- }
467
+ if ( Trace . isEnabled ( ) ) {
468
+ CLog ( CLogTypes . info , 'MapPanHandlerImpl::panEnd(): top with state:' , recognizer . state ) ;
469
+ }
472
470
473
- return ;
471
+ if ( recognizer . state === UIGestureRecognizerState . Ended ) {
472
+ this . notifyListener ( recognizer . state , panCoordinate . latitude , panCoordinate . longitude ) ;
474
473
}
474
+ }
475
475
476
- this . _listener ( {
477
- lat : panCoordinate . latitude ,
478
- lng : panCoordinate . longitude
479
- } ) ;
476
+ public panBegin ( recognizer : UIPanGestureRecognizer ) : void {
477
+ const panCoordinate = this . getCoordinates ( recognizer ) ;
478
+
479
+ if ( Trace . isEnabled ( ) ) {
480
+ CLog ( CLogTypes . info , 'MapPanHandlerImpl::panBegin(): top with state:' , recognizer . state ) ;
481
+ }
482
+
483
+ if ( recognizer . state === UIGestureRecognizerState . Began ) {
484
+ this . notifyListener ( recognizer . state , panCoordinate . latitude , panCoordinate . longitude ) ;
485
+ }
480
486
}
481
487
482
- public static ObjCExposedMethods = {
483
- pan : { returns : interop . types . void , params : [ interop . types . id ] }
484
- } ;
488
+ private getCoordinates ( recognizer : UIPanGestureRecognizer ) {
489
+ const panPoint = recognizer . locationInView ( this . _mapView ) ;
490
+ return this . _mapView . convertPointToCoordinateFromView ( panPoint , this . _mapView ) ;
491
+ }
492
+
493
+ private notifyListener ( panState : UIGestureRecognizerState , latitude : number , longitude : number ) {
494
+ if ( this . _listener . has ( panState ) ) {
495
+ this . _listener . get ( panState ) ( { lat : latitude , lng : longitude } ) ;
496
+ }
497
+ }
485
498
}
486
499
487
500
/**
@@ -850,6 +863,32 @@ export class MapboxView extends MapboxViewBase {
850
863
ios : this . nativeMapView
851
864
} ) ;
852
865
} , this . nativeMapView ) ;
866
+
867
+ this . mapbox . setOnMoveEndListener ( ( data ?: LatLng ) => {
868
+ if ( Trace . isEnabled ( ) ) {
869
+ CLog ( CLogTypes . info , 'initMap(): onMoveEnd listener' ) ;
870
+ }
871
+
872
+ this . notify ( {
873
+ eventName : MapboxViewBase . moveEndEvent ,
874
+ object : this ,
875
+ map : this ,
876
+ ios : this . nativeMapView
877
+ } ) ;
878
+ } , this . nativeMapView ) ;
879
+
880
+ this . mapbox . setOnScrollListener ( ( data ?: LatLng ) => {
881
+ if ( Trace . isEnabled ( ) ) {
882
+ CLog ( CLogTypes . info , 'initMap(): onScroll listener' ) ;
883
+ }
884
+
885
+ this . notify ( {
886
+ eventName : MapboxViewBase . scrollEvent ,
887
+ object : this ,
888
+ map : this ,
889
+ ios : this . nativeMapView
890
+ } ) ;
891
+ } , this . nativeMapView ) ;
853
892
} ;
854
893
855
894
// draw the map after a timeout
@@ -2247,7 +2286,11 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
2247
2286
}
2248
2287
2249
2288
// adding the pan handler to the map oject so it's not GC'd
2250
- theMap [ 'mapPanHandler' ] = MapPanHandlerImpl . initWithOwnerAndListenerForMap ( new WeakRef ( this ) , listener , theMap ) ;
2289
+ if ( theMap [ 'mapPanHandler' ] === undefined ) {
2290
+ theMap [ 'mapPanHandler' ] = MapPanHandlerImpl . initWithOwnerAndListenerForMap ( new WeakRef ( this ) , listener , UIGestureRecognizerState . Changed , theMap ) ;
2291
+ } else {
2292
+ ( theMap [ 'mapPanHandler' ] as MapPanHandlerImpl ) . addListener ( UIGestureRecognizerState . Changed , listener ) ;
2293
+ }
2251
2294
2252
2295
// there's already a pan recognizer, so find it and attach a target action
2253
2296
for ( let i = 0 ; i < theMap . gestureRecognizers . count ; i ++ ) {
@@ -2286,27 +2329,63 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
2286
2329
}
2287
2330
2288
2331
// adding the pan handler to the map oject so it's not GC'd
2289
- theMap [ 'mapOnMoveBeginHandler' ] = MapPanHandlerImpl . initWithOwnerAndListenerForMap ( new WeakRef ( this ) , listener , theMap ) ;
2332
+ if ( theMap [ 'mapPanHandler' ] === undefined ) {
2333
+ theMap [ 'mapPanHandler' ] = MapPanHandlerImpl . initWithOwnerAndListenerForMap ( new WeakRef ( this ) , listener , UIGestureRecognizerState . Began , theMap ) ;
2334
+ } else {
2335
+ ( theMap [ 'mapPanHandler' ] as MapPanHandlerImpl ) . addListener ( UIGestureRecognizerState . Began , listener ) ;
2336
+ }
2290
2337
2291
- // tell the panHandler that we're only interested in the first pan per pan gesture
2338
+ // there's already a pan recognizer, so find it and attach a target action
2292
2339
2293
- theMap [ 'mapOnMoveBeginHandler' ] . setOnMoveBegin ( ) ;
2340
+ for ( let i = 0 ; i < theMap . gestureRecognizers . count ; i ++ ) {
2341
+ const recognizer : UIGestureRecognizer = theMap . gestureRecognizers . objectAtIndex ( i ) ;
2294
2342
2295
- // there's already a pan recognizer, so find it and attach a target action
2343
+ if ( recognizer instanceof UIPanGestureRecognizer ) {
2344
+ recognizer . addTargetAction ( theMap [ 'mapPanHandler' ] , 'panBegin' ) ;
2345
+ break ;
2346
+ }
2347
+ }
2348
+
2349
+ resolve ( ) ;
2350
+ } catch ( ex ) {
2351
+ if ( Trace . isEnabled ( ) ) {
2352
+ CLog ( CLogTypes . info , 'Error in mapbox.setOnMoveBeginListener: ' + ex ) ;
2353
+ }
2354
+ reject ( ex ) ;
2355
+ }
2356
+ } ) ;
2357
+ }
2358
+
2359
+ setOnMoveEndListener ( listener : ( ) => void , nativeMap ?: any ) : Promise < void > {
2360
+ return new Promise ( ( resolve , reject ) => {
2361
+ try {
2362
+ const theMap : MGLMapView = nativeMap || this . _mapboxViewInstance ;
2296
2363
2364
+ if ( ! theMap ) {
2365
+ reject ( 'No map has been loaded' ) ;
2366
+ return ;
2367
+ }
2368
+
2369
+ if ( theMap [ 'mapPanHandler' ] === undefined ) {
2370
+ theMap [ 'mapPanHandler' ] = MapPanHandlerImpl . initWithOwnerAndListenerForMap ( new WeakRef ( this ) , listener , UIGestureRecognizerState . Ended , theMap ) ;
2371
+ } else {
2372
+ ( theMap [ 'mapPanHandler' ] as MapPanHandlerImpl ) . addListener ( UIGestureRecognizerState . Ended , listener ) ;
2373
+ }
2374
+
2375
+ // there's already a pan recognizer, so find it and attach a target action
2297
2376
for ( let i = 0 ; i < theMap . gestureRecognizers . count ; i ++ ) {
2298
2377
const recognizer : UIGestureRecognizer = theMap . gestureRecognizers . objectAtIndex ( i ) ;
2299
2378
2300
2379
if ( recognizer instanceof UIPanGestureRecognizer ) {
2301
- recognizer . addTargetAction ( theMap [ 'mapOnMoveBeginHandler ' ] , 'pan ' ) ;
2380
+ recognizer . addTargetAction ( theMap [ 'mapPanHandler ' ] , 'panEnd ' ) ;
2302
2381
break ;
2303
2382
}
2304
2383
}
2305
2384
2306
2385
resolve ( ) ;
2307
2386
} catch ( ex ) {
2308
2387
if ( Trace . isEnabled ( ) ) {
2309
- CLog ( CLogTypes . info , 'Error in mapbox.setOnScrollListener : ' + ex ) ;
2388
+ CLog ( CLogTypes . info , 'Error in mapbox.setOnMoveEndListener : ' + ex ) ;
2310
2389
}
2311
2390
reject ( ex ) ;
2312
2391
}
@@ -3113,4 +3192,4 @@ const _downloadMarkerImages = (markers: MapboxMarker[]) => {
3113
3192
} ) ;
3114
3193
3115
3194
return Promise . all ( iterations ) . then ( ( ) => result ) ;
3116
- } ;
3195
+ } ;
0 commit comments