Skip to content

Commit

Permalink
fix trackLocation needs to be called on ui thread
Browse files Browse the repository at this point in the history
  • Loading branch information
josxha committed Oct 6, 2024
1 parent 88632cb commit 732e555
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
7 changes: 6 additions & 1 deletion example/lib/user_location_page.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:maplibre/maplibre.dart';
import 'package:maplibre_example/styled_map_page.dart';
import 'package:permission_handler/permission_handler.dart';

@immutable
Expand Down Expand Up @@ -60,7 +61,11 @@ class _UserLocationPageState extends State<UserLocationPage> {
),
Expanded(
child: MapLibreMap(
options: MapOptions(zoom: 1, center: Position(0, 0)),
options: MapOptions(
zoom: 1,
center: Position(0, 0),
style: StyledMapPage.styleUrl,
),
onMapCreated: (controller) => _controller = controller,
),
),
Expand Down
12 changes: 7 additions & 5 deletions lib/src/native/widget_state_jni.dart
Original file line number Diff line number Diff line change
Expand Up @@ -470,10 +470,9 @@ final class MapLibreMapStateJni extends State<MapLibreMap>
bool pulse = true,
}) async {
// https://maplibre.org/maplibre-native/docs/book/android/location-component-guide.html
final jniMapLibreMap = _jniMapLibreMap;
final locationComponent = _locationComponent;
final jniStyle = _jniStyle;
await runOnPlatformThread(() {
final locationComponent = jniMapLibreMap.getLocationComponent();
final jniContext = jni.MapLibreRegistry.INSTANCE.getContext();
final locationComponentOptionsBuilder =
jni.LocationComponentOptions.builder(jniContext)
Expand All @@ -497,7 +496,6 @@ final class MapLibreMapStateJni extends State<MapLibreMap>
locationComponent.activateLocationComponent(activationOptions);
locationComponent.setLocationComponentEnabled(true);

locationComponent.release();
locationComponentOptionsBuilder.release();
locationComponentOptions.release();
locationEngineRequestBuilder.release();
Expand All @@ -508,6 +506,10 @@ final class MapLibreMapStateJni extends State<MapLibreMap>
}

@override
Future<void> trackLocation() async =>
_locationComponent.setCameraMode(jni.CameraMode.TRACKING_GPS);
Future<void> trackLocation() async {
final locationComponent = _locationComponent;
await runOnPlatformThread(() {
locationComponent.setCameraMode(jni.CameraMode.TRACKING_GPS);
});
}
}

0 comments on commit 732e555

Please sign in to comment.