Skip to content

Commit

Permalink
feat: add user location (#68)
Browse files Browse the repository at this point in the history
- [x] release jni builder objects (e.g. LocationEngineRequest_Builder)
- [x] solution for web
- [x] working android integration
- [x] example app
  • Loading branch information
josxha authored Oct 6, 2024
1 parent 9745aba commit 4d0c6db
Show file tree
Hide file tree
Showing 21 changed files with 2,289 additions and 338 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class MapLibrePlugin : FlutterPlugin, ActivityAware {
private lateinit var flutterAssets: FlutterPlugin.FlutterAssets

override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) {
MapLibreRegistry.context = binding.applicationContext;
flutterAssets = binding.flutterAssets
binding
.platformViewRegistry
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.github.josxha.maplibre

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import androidx.annotation.Keep
import org.maplibre.android.maps.MapLibreMap

@Keep
@SuppressLint("StaticFieldLeak")
object MapLibreRegistry {
private val mapRegistry = HashMap<Int, MapLibreMap>()

Expand All @@ -14,4 +18,10 @@ object MapLibreRegistry {
fun addMap(viewId: Int, map: MapLibreMap) {
mapRegistry[viewId] = map
}

// TODO: Storing the Activity in a static field is a potential memory leak.
public var activity: Activity? = null;

// TODO: Storing the Context in a static field is a potential memory leak.
public var context: Context? = null;
}
2 changes: 2 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import 'package:maplibre_example/menu_page.dart';
import 'package:maplibre_example/parameters_page.dart';
import 'package:maplibre_example/styled_map_page.dart';
import 'package:maplibre_example/two_maps_page.dart';
import 'package:maplibre_example/user_location_page.dart';
import 'package:maplibre_example/web_controls_page.dart';

void main() {
Expand All @@ -47,6 +48,7 @@ class MyApp extends StatelessWidget {
GesturesPage.location: (context) => const GesturesPage(),
EventsPage.location: (context) => const EventsPage(),
StyledMapPage.location: (context) => const StyledMapPage(),
UserLocationPage.location: (context) => const UserLocationPage(),
LayersSymbolPage.location: (context) => const LayersSymbolPage(),
LayersCirclePage.location: (context) => const LayersCirclePage(),
LayersHeatmapPage.location: (context) => const LayersHeatmapPage(),
Expand Down
7 changes: 7 additions & 0 deletions example/lib/menu_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import 'package:maplibre_example/layers_symbol_page.dart';
import 'package:maplibre_example/parameters_page.dart';
import 'package:maplibre_example/styled_map_page.dart';
import 'package:maplibre_example/two_maps_page.dart';
import 'package:maplibre_example/user_location_page.dart';
import 'package:maplibre_example/web_controls_page.dart';

class MenuPage extends StatelessWidget {
Expand Down Expand Up @@ -79,6 +80,12 @@ class MenuPage extends StatelessWidget {
iconData: Icons.animation,
location: AnimationPage.location,
),
if (!kIsWeb)
ItemCard(
label: 'User Location',
iconData: Icons.gps_fixed,
location: UserLocationPage.location,
),
],
),
const SliverToBoxAdapter(child: SectionTitle('Annotations')),
Expand Down
76 changes: 76 additions & 0 deletions example/lib/user_location_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
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
class UserLocationPage extends StatefulWidget {
const UserLocationPage({super.key});

static const location = '/user-location';

@override
State<UserLocationPage> createState() => _UserLocationPageState();
}

class _UserLocationPageState extends State<UserLocationPage> {
late final MapController _controller;

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('User Location')),
body: Column(
children: [
Padding(
padding: const EdgeInsets.all(8),
child: Wrap(
spacing: 8,
alignment: WrapAlignment.center,
children: [
OutlinedButton(
onPressed: () async {
final status = await Permission.locationWhenInUse.request();
debugPrint(status.toString());
},
child: const Text(
'Get permission',
textAlign: TextAlign.center,
),
),
OutlinedButton(
onPressed: () async {
await _controller.enableLocation();
},
child: const Text(
'Enable location',
textAlign: TextAlign.center,
),
),
OutlinedButton(
onPressed: () async {
await _controller.trackLocation();
},
child: const Text(
'Track location',
textAlign: TextAlign.center,
),
),
],
),
),
Expanded(
child: MapLibreMap(
options: MapOptions(
zoom: 1,
center: Position(0, 0),
style: StyledMapPage.styleUrl,
),
onMapCreated: (controller) => _controller = controller,
),
),
],
),
);
}
}
1 change: 1 addition & 0 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dependencies:
http: ^1.2.2
maplibre:
path: ../
permission_handler: ^11.3.1

dev_dependencies:
flutter_test:
Expand Down
3 changes: 3 additions & 0 deletions example/windows/flutter/generated_plugin_registrant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
#include "generated_plugin_registrant.h"

#include <maplibre/maplibre_plugin_c_api.h>
#include <permission_handler_windows/permission_handler_windows_plugin.h>

void RegisterPlugins(flutter::PluginRegistry* registry) {
MaplibrePluginCApiRegisterWithRegistrar(
registry->GetRegistrarForPlugin("MaplibrePluginCApi"));
PermissionHandlerWindowsPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("PermissionHandlerWindowsPlugin"));
}
1 change: 1 addition & 0 deletions example/windows/flutter/generated_plugins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

list(APPEND FLUTTER_PLUGIN_LIST
maplibre
permission_handler_windows
)

list(APPEND FLUTTER_FFI_PLUGIN_LIST
Expand Down
2 changes: 1 addition & 1 deletion jnigen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ source_path:
- 'android/src/main'
classes:
# https://github.com/maplibre/maplibre-native/tree/main/platform/android/MapLibreAndroid/src/main/java/org/maplibre/android
- 'android.app.Activity'
- 'android.graphics.PointF'
- 'android.location.Location'
- 'com.github.josxha.maplibre.MapLibreRegistry'
- 'org.maplibre.android.attribution'
- 'org.maplibre.android.camera'
Expand Down
Loading

0 comments on commit 4d0c6db

Please sign in to comment.