Skip to content

Commit 539bbb1

Browse files
update pan mode when map is moved
1 parent 7c43b4e commit 539bbb1

File tree

4 files changed

+59
-27
lines changed

4 files changed

+59
-27
lines changed

arcgis_map_sdk_android/android/src/main/kotlin/dev/fluttercommunity/arcgis_map_sdk_android/ArcgisMapView.kt

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import com.esri.arcgisruntime.mapping.view.AnimationCurve
2727
import com.esri.arcgisruntime.mapping.view.Graphic
2828
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay
2929
import com.esri.arcgisruntime.mapping.view.LocationDisplay.AutoPanMode
30+
import com.esri.arcgisruntime.mapping.view.LocationDisplay.AutoPanMode.*
3031
import com.esri.arcgisruntime.mapping.view.MapView
3132
import com.esri.arcgisruntime.symbology.Symbol
3233
import com.google.gson.reflect.TypeToken
@@ -101,7 +102,7 @@ internal class ArcgisMapView(
101102
mapView.graphicsOverlays.add(defaultGraphicsOverlay)
102103

103104
mapView.addMapScaleChangedListener {
104-
if(mapView.mapScale.isNaN()) return@addMapScaleChangedListener
105+
if (mapView.mapScale.isNaN()) return@addMapScaleChangedListener
105106

106107
val zoomLevel = getZoomLevel(mapView)
107108
zoomStreamHandler.addZoom(zoomLevel)
@@ -114,9 +115,11 @@ internal class ArcgisMapView(
114115
val center = mapView.visibleArea?.extent?.center ?: return@addViewpointChangedListener
115116
val wgs84Center = GeometryEngine.project(center, SpatialReferences.getWgs84()) as? Point
116117

117-
val latLng = wgs84Center?.let {
118-
LatLng(longitude = it.x,latitude = it.y)
119-
} ?: return@addViewpointChangedListener
118+
if (wgs84Center == null || wgs84Center.x.isNaN() || wgs84Center.y.isNaN()) {
119+
return@addViewpointChangedListener
120+
}
121+
122+
val latLng = LatLng(longitude = wgs84Center.x, latitude = wgs84Center.y)
120123

121124
centerPositionStreamHandler.add(latLng)
122125
}
@@ -311,7 +314,14 @@ internal class ArcgisMapView(
311314
result: MethodChannel.Result
312315
) {
313316
try {
314-
return result.success(mapView.locationDisplay.autoPanMode.name)
317+
return result.success(
318+
when (mapView.locationDisplay.autoPanMode) {
319+
OFF -> "off"
320+
RECENTER -> "recenter"
321+
NAVIGATION -> "navigation"
322+
COMPASS_NAVIGATION -> "compassNavigation"
323+
}
324+
)
315325
} catch (e: Throwable) {
316326
result.finishWithError(e, "Getting AutoPanMode failed.")
317327
}
@@ -723,9 +733,9 @@ private fun LoadStatusChangedEvent.jsonValue() = when (newLoadStatus) {
723733
}
724734

725735
private fun String.autoPanModeFromString() = when (this) {
726-
"compassNavigation" -> AutoPanMode.COMPASS_NAVIGATION
727-
"navigation" -> AutoPanMode.NAVIGATION
728-
"recenter" -> AutoPanMode.RECENTER
729-
"off" -> AutoPanMode.OFF
736+
"compassNavigation" -> COMPASS_NAVIGATION
737+
"navigation" -> NAVIGATION
738+
"recenter" -> RECENTER
739+
"off" -> OFF
730740
else -> null
731741
}

example/lib/location_indicator_example_page.dart

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1+
import 'dart:async';
12
import 'dart:math';
23

34
import 'package:arcgis_example/main.dart';
45
import 'package:arcgis_map_sdk/arcgis_map_sdk.dart';
56
import 'package:flutter/material.dart';
67
import 'package:geolocator/geolocator.dart';
8+
import 'package:rxdart/rxdart.dart';
79

810
class LocationIndicatorExamplePage extends StatefulWidget {
911
const LocationIndicatorExamplePage({super.key});
1012

1113
@override
12-
State<LocationIndicatorExamplePage> createState() =>
13-
_LocationIndicatorExamplePageState();
14+
State<LocationIndicatorExamplePage> createState() => _LocationIndicatorExamplePageState();
1415
}
1516

16-
class _LocationIndicatorExamplePageState
17-
extends State<LocationIndicatorExamplePage> {
17+
class _LocationIndicatorExamplePageState extends State<LocationIndicatorExamplePage> {
1818
final _mockLocations = [
1919
LatLng(48.1234963, 11.5910182),
2020
LatLng(48.1239241, 11.45897063),
@@ -34,6 +34,8 @@ class _LocationIndicatorExamplePageState
3434
var _activeAutoPanMode = AutoPanMode.off;
3535
var _wanderExtentFactor = 0.5;
3636

37+
StreamSubscription? _panUpdateSubscription;
38+
3739
@override
3840
Widget build(BuildContext context) {
3941
return Scaffold(
@@ -48,8 +50,7 @@ class _LocationIndicatorExamplePageState
4850
: await _controller!.locationDisplay.startSource();
4951
} catch (e, stack) {
5052
if (!mounted) return;
51-
ScaffoldMessenger.of(_snackBarKey.currentContext!)
52-
.showSnackBar(SnackBar(content: Text("$e")));
53+
ScaffoldMessenger.of(_snackBarKey.currentContext!).showSnackBar(SnackBar(content: Text("$e")));
5354
debugPrint("$e");
5455
debugPrintStack(stackTrace: stack);
5556
}
@@ -71,16 +72,21 @@ class _LocationIndicatorExamplePageState
7172
_controller = controller;
7273
_requestLocationPermission();
7374
_configureLocationDisplay(Colors.blue);
75+
76+
_panUpdateSubscription?.cancel();
77+
_panUpdateSubscription = controller
78+
.centerPosition()
79+
.debounceTime(const Duration(milliseconds: 50))
80+
.listen((_) => _refreshAutoPanMode());
7481
},
7582
),
7683
),
7784
const SizedBox(height: 16),
85+
Text("Current auto pan mode: ${_activeAutoPanMode.name}"),
7886
ElevatedButton(
7987
onPressed: _switchLocationSource,
8088
child: Text(
81-
_isManualLocationSource
82-
? "Use auto location source"
83-
: "Use manual location source",
89+
_isManualLocationSource ? "Use auto location source" : "Use manual location source",
8490
),
8591
),
8692
if (_isManualLocationSource) ...[
@@ -101,15 +107,12 @@ class _LocationIndicatorExamplePageState
101107
ElevatedButton(
102108
onPressed: () {
103109
setState(
104-
() =>
105-
_useCourseSymbolForMovement = !_useCourseSymbolForMovement,
110+
() => _useCourseSymbolForMovement = !_useCourseSymbolForMovement,
106111
);
107112
_configureLocationDisplay(Colors.red);
108113
},
109114
child: Text(
110-
_useCourseSymbolForMovement
111-
? "Disable course indicator"
112-
: "Enable course indicator",
115+
_useCourseSymbolForMovement ? "Disable course indicator" : "Enable course indicator",
113116
),
114117
),
115118
ElevatedButton(
@@ -158,9 +161,7 @@ class _LocationIndicatorExamplePageState
158161
Future<void> _switchLocationSource() async {
159162
await _controller!.locationDisplay.stopSource();
160163
await _controller!.setLocationDisplay(
161-
_isManualLocationSource
162-
? ArcgisLocationDisplay()
163-
: ArcgisManualLocationDisplay(),
164+
_isManualLocationSource ? ArcgisLocationDisplay() : ArcgisManualLocationDisplay(),
164165
);
165166
setState(() => _isManualLocationSource = !_isManualLocationSource);
166167

@@ -246,4 +247,16 @@ class _LocationIndicatorExamplePageState
246247
_controller?.locationDisplay.setWanderExtentFactor(_wanderExtentFactor);
247248
}
248249
}
250+
251+
@override
252+
void dispose() {
253+
_panUpdateSubscription?.cancel();
254+
super.dispose();
255+
}
256+
257+
Future<void> _refreshAutoPanMode() async {
258+
final panMode = await _controller!.locationDisplay.getAutoPanMode();
259+
if (!mounted) return;
260+
setState(() => _activeAutoPanMode = panMode);
261+
}
249262
}

example/pubspec.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,14 @@ packages:
274274
url: "https://pub.dev"
275275
source: hosted
276276
version: "2.1.8"
277+
rxdart:
278+
dependency: "direct main"
279+
description:
280+
name: rxdart
281+
sha256: "5c3004a4a8dbb94bd4bf5412a4def4acdaa12e12f269737a5751369e12d1a962"
282+
url: "https://pub.dev"
283+
source: hosted
284+
version: "0.28.0"
277285
sky_engine:
278286
dependency: transitive
279287
description: flutter

example/pubspec.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ environment:
1111

1212
dependencies:
1313
arcgis_map_sdk: ^0.8.0
14+
rxdart: ^0.28.0
1415

1516
cupertino_icons: ^1.0.0
1617
geolocator: ^10.1.0
@@ -42,4 +43,4 @@ dependency_overrides:
4243
arcgis_map_sdk_platform_interface:
4344
path: ../arcgis_map_sdk_platform_interface
4445
arcgis_map_sdk_web:
45-
path: ../arcgis_map_sdk_web
46+
path: ../arcgis_map_sdk_web

0 commit comments

Comments
 (0)