masamune_location 3.1.6 copy "masamune_location: ^3.1.6" to clipboard
masamune_location: ^3.1.6 copied to clipboard

unlisted

Masamune plugin library for handling location information in apps.

Masamune logo

Masamune Location

Follow on GitHub Follow on X Follow on YouTube Maintained with Melos

GitHub Sponsor


[GitHub] | [YouTube] | [Packages] | [X] | [LinkedIn] | [mathru.net]


Masamune Location #

Usage #

These packages provide a unified location stack:

  • masamune_location – core abstractions, controllers, and runtime adapters
  • masamune_location_geocoding – geocoding/ reverse geocoding via Cloud Functions
  • masamune_location_google – Google Maps integration helpers

Install the packages you need.

flutter pub add masamune_location
flutter pub add masamune_location_geocoding
flutter pub add masamune_location_google

Run flutter pub get after editing pubspec.yaml manually.

Register Adapters #

Configure the adapters in your Masamune setup. The example below adds a mobile location adapter and geocoding adapter (which delegates to Cloud Functions).

// lib/adapter.dart

/// Masamune adapters used in the application.
final masamuneAdapters = <MasamuneAdapter>[
  const UniversalMasamuneAdapter(),
  const FunctionsMasamuneAdapter(),

  MobileLocationMasamuneAdapter(
    requestWhenInUsePermissionOnInit: true,
    locationAccuracy: LocationAccuracy.high,
  ),

  GeocodingMasamuneAdapter(
    functionsAdapter: const FunctionsMasamuneAdapter(),
  ),
];

MobileLocationMasamuneAdapter provides real device GPS access, while GeocodingMasamuneAdapter calls server-side geocoding endpoints.

Get Current Location #

Use the Location controller to fetch the current position or listen for updates.

class LocationPage extends PageScopedWidget {
  @override
  Widget build(BuildContext context, PageRef ref) {
    final location = ref.app.controller(Location.query());

    // Initialize on page load
    ref.page.on(
      initOrUpdate: () {
        location.initialize();
      },
    );

    return Scaffold(
      appBar: AppBar(title: const Text("Location")),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            if (location.value != null)
              Column(
                children: [
                  Text("Latitude: ${location.value!.latitude}"),
                  Text("Longitude: ${location.value!.longitude}"),
                  Text("Accuracy: ${location.value!.accuracy}m"),
                ],
              )
            else
              Text("No location data"),
            
            ElevatedButton(
              onPressed: () async {
                final position = await location.getCurrentPosition();
                print("Location: ${position.latitude}, ${position.longitude}");
              },
              child: const Text("Get Location"),
            ),
          ],
        ),
      ),
    );
  }
}

Continuous Location Updates #

Listen for location changes automatically:

// Start listening for updates
await location.startListening(
  distanceFilter: 50,              // Update when moved 50+ meters
  timeInterval: Duration(seconds: 30),  // Or every 30 seconds
);

// Listen to changes
location.addListener(() {
  final latest = location.value;
  if (latest != null) {
    print("Updated: ${latest.latitude}, ${latest.longitude}");
  }
});

// Stop listening when done
await location.stopListening();

Handle Permissions #

Use the adapter helpers to request permissions and open app settings when required.

final permissionStatus = await location.requestPermission();
if (!permissionStatus.granted) {
  await openAppSettings();
}

Geocoding #

GeocodingMasamuneAdapter exposes Functions actions to convert between coordinates and addresses.

final geocoding = GeocodingMasamuneAdapter.primary;

final address = await geocoding.fromLatLng(
  latitude: position.latitude,
  longitude: position.longitude,
);

final coordinates = await geocoding.fromAddress("Tokyo Station");

Implement the corresponding Firebase Function (or REST endpoint) to call external geocoding APIs such as Google Maps Geocoding.

Google Maps Helpers #

masamune_location_google provides utilities to integrate with Google Maps SDKs, such as map controllers, markers, and place lookups. Register adapters from that package if you need Google-specific features.

Mocking and Testing #

  • Use MockLocationMasamuneAdapter for widget tests or environments without GPS access.
  • Inject mock adapters during development to simulate location updates or geocoding responses.

Tips #

  • Handle platform permissions carefully: Android 12+ requires approximate/precise location toggles, iOS may need NSLocationAlwaysUsageDescription strings.
  • Cache last known locations to reduce sensor usage and provide instant UI feedback.
  • Respect user privacy by communicating why location data is collected and storing it securely.
  • Combine with masamune_scheduler to trigger location-based reminders or geofencing workflows.

GitHub Sponsors #

Sponsors are always welcome. Thank you for your support!

https://github.com/sponsors/mathrunet