androidfluttergoogle-mapsopenstreetmap

Cannot run (simulate) Open Street Map on Flutter


Why when I launch my Flutter Application via flutter run --debug using Google Map and Open Street Map (depending on the choice defined from the Backend of one of the two maps), I have the following feedback and the map is not displayed at all in the simulator:

PS C:\Users\useru\Downloads\Main-File-Dec-20\EDITED\flutter_user> flutter run --debug
Launching lib\main.dart on sdk gphone x86 64 in debug mode...
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Running Gradle task 'assembleDebug'...                            140,6s
āˆš Built build\app\outputs\flutter-apk\app-debug.apk
Installing build\app\outputs\flutter-apk\app-debug.apk...           7,3s
D/FlutterGeolocator( 6792): Attaching Geolocator to activity
D/FlutterGeolocator( 6792): Creating service.
D/FlutterGeolocator( 6792): Binding to location service.
D/FlutterLocationService( 6792): Creating service.
D/FlutterLocationService( 6792): Binding to location service.
I/Choreographer( 6792): Skipped 50 frames!  The application may be doing too much work on its main thread.
D/FlutterGeolocator( 6792): Geolocator foreground service connected
D/FlutterGeolocator( 6792): Initializing Geolocator services
D/FlutterGeolocator( 6792): Flutter engine connected. Connected engine count 1
D/LocationPlugin( 6792): Service connected: ComponentInfo{com.user.app/com.lyokone.location.FlutterLocationService}
I/com.user.app( 6792): Compiler allocated 4533KB to compile void android.view.ViewRootImpl.performTraversals()
Syncing files to device sdk gphone x86 64...                       351ms

Flutter run key commands.
r Hot reload.
R Hot restart.
h List all available interactive commands.
d Detach (terminate "flutter run" but leave application running).
c Clear the screen
q Quit (terminate the application on the device).

A Dart VM Service on sdk gphone x86 64 is available at: http://127.0.0.1:57469/x9iwOdhhZZo=/
The Flutter DevTools debugger and profiler on sdk gphone x86 64 is available at: http://127.0.0.1:9102?uri=http://127.0.0.1:57469/x9iwOdhhZZo=/
W/Parcel  ( 6792): Expecting binder but got null!
D/TrafficStats( 6792): tagSocket(134) with statsTag=0xffffffff, statsUid=-1
D/ProfileInstaller( 6792): Installing profile for com.user.app
D/EGL_emulation( 6792): app_time_stats: avg=69.87ms min=7.59ms max=570.00ms count=21
D/EGL_emulation( 6792): app_time_stats: avg=77.98ms min=12.24ms max=894.28ms count=22
D/EGL_emulation( 6792): app_time_stats: avg=56.78ms min=24.47ms max=246.50ms count=18
D/EGL_emulation( 6792): app_time_stats: avg=18.72ms min=4.07ms max=75.93ms count=47
D/EGL_emulation( 6792): app_time_stats: avg=11.31ms min=3.95ms max=25.39ms count=57
D/EGL_emulation( 6792): app_time_stats: avg=14.98ms min=3.99ms max=88.47ms count=47
D/EGL_emulation( 6792): app_time_stats: avg=11.09ms min=3.76ms max=50.86ms count=54
D/EGL_emulation( 6792): app_time_stats: avg=13.98ms min=4.17ms max=47.56ms count=50
D/EGL_emulation( 6792): app_time_stats: avg=14.09ms min=4.34ms max=37.21ms count=49
D/EGL_emulation( 6792): app_time_stats: avg=8.97ms min=3.30ms max=29.70ms count=57

???

I would like to point out that currently, Open Street Map has been defined as the default Map to use.

How to fix that ??? I'm using google_maps_flutter: ^2.5.0 and flutter_map: ^7.0.2.

Please, I need your help


Solution

  • Try this

    import 'package:flutter/material.dart';
    import 'package:flutter_map/flutter_map.dart';
    import 'package:latlong2/latlong2.dart';
    import 'package:permission_handler/permission_handler.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'OpenStreetMap Example',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: MapScreen(),
        );
      }
    }
    
    class MapScreen extends StatefulWidget {
      @override
      _MapScreenState createState() => _MapScreenState();
    }
    
    class _MapScreenState extends State<MapScreen> {
      bool _locationPermissionGranted = false;
      LatLng _currentLocation = LatLng(51.5, -0.09); //=> Default London
    
      @override
      void initState() {
        super.initState();
        _requestLocationPermission();
      }
    
      Future<void> _requestLocationPermission() async {
        final status = await Permission.location.request();
        if (status == PermissionStatus.granted) {
          print('Location permission granted');
          setState(() {
            _locationPermissionGranted = true;
          });
          _getCurrentLocation(); //=> Call after permission granted
        } else {
          print('Location permission denied');
        }
      }
    
      //Simulate Location data for now
      Future<void> _getCurrentLocation() async {
        await Future.delayed(Duration(seconds: 1));
        setState(() {
          //=> replace your LatLng
          _currentLocation = LatLng(48.8566, 2.3522); //=> Paris
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(title: Text('OpenStreetMap Example')),
          body: _locationPermissionGranted
              ? FlutterMap(
                  options: MapOptions(
                    center: _currentLocation,
                    zoom: 13.0,
                  ),
                  children: [
                    TileLayer(
                      urlTemplate: "https://tile.openstreetmap.org/{z}/{x}/{y}.png",
                      userAgentPackageName:
                          'com.example.xxxxx', //=> Replace your package name
                    ),
                    MarkerLayer(
                      markers: [
                        Marker(
                          width: 80.0,
                          height: 80.0,
                          point: _currentLocation,
                          builder: (ctx) => Container(
                            child: Icon(
                              Icons.location_pin,
                              color: Colors.red,
                            ),
                          ),
                        ),
                      ],
                    ),
                  ],
                )
              : Center(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Text('Location permission is required to show the map.'),
                      SizedBox(height: 20),
                      ElevatedButton(
                        onPressed: _requestLocationPermission,
                        child: Text('Request Location Permission'),
                      ),
                    ],
                  ),
                ),
        );
      }
    }
    

    AndroidManifest.xml

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    

    Dependencies

    flutter_map
    latlong2
    permission_handler