Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - google map, does not show my location

It seems to have written everything correctly but I can not understand why it does not show my location on the map.

Why is it not showing my location?

import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

class MyMap extends StatefulWidget {
  @override
  MyMapState createState() => MyMapState();
}

class MyMapState extends State<MyMap> {
  GoogleMapController mapController;

  final LatLng _center = const LatLng(50.006406, 36.236484);

  void _onMapCreated(GoogleMapController controller) {
    mapController = controller;
    mapController.addMarker(
      MarkerOptions(
        position: LatLng(50.006406, 36.236484),
        infoWindowText: InfoWindowText('My Location', 'Office'),
        icon: BitmapDescriptor.defaultMarker,
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('Map'),
          backgroundColor: Colors.green[700],
        ),
        body: GoogleMap(
          onMapCreated: _onMapCreated,
          options: GoogleMapOptions(
            scrollGesturesEnabled: true,
            tiltGesturesEnabled: true,
            rotateGesturesEnabled: true,
            myLocationEnabled: true,
            compassEnabled: true,
            cameraPosition: CameraPosition(
              target: _center,
              zoom: 11.0,
            ),
          ),
        ),
      );
  }
}
like image 747
Max Avatar asked Jan 29 '19 17:01

Max


4 Answers

For Android: In debug mode and when your app is in development...you need to grant the location permissions manually in your phone applications settings and reloading your app in order to show the the "My location" button.

like image 68
Samir Ait abdelkoui Avatar answered Sep 22 '22 19:09

Samir Ait abdelkoui


If you want to see your current location on the map, and enable the lower right FAB functionality that takes you there, make sure you have the property myLocationEnabled set to true on your GoogleMap Widget.

      [...]
      body: GoogleMap(
          myLocationEnabled: true,
          [...]
      )
like image 32
Mengrolek Avatar answered Oct 25 '22 00:10

Mengrolek


You have to provide an access to your current location ios: Info.plist

    <key>NSLocationWhenInUseUsageDescription</key>
    <string>Application needs to access your current location</string>
    <key>NSLocationAlwaysUsageDescription</key>
    <string>Application needs to access your current location</string>

android: AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
like image 8
korgara Avatar answered Oct 25 '22 00:10

korgara


you need to use the location package of flutter in order to track the location of the user, and you need to add this function to your code

_getLocation() async {
    var location = new Location();
    try {
      currentLocation = await location.getLocation();

      print("locationLatitude: ${currentLocation.latitude}");
      print("locationLongitude: ${currentLocation.longitude}");
      setState(
          () {}); //rebuild the widget after getting the current location of the user
    } on Exception {
      currentLocation = null;
    }
  }

this function should be called in your initState() function as follows

@override
  void initState() {
    _getLocation();
    super.initState();
  }

and don't to forget to import the location package to your code,

import 'package:location/location.dart';

follow the installation guide of the location package as you need to add the location permission in AndroidManifest.xml for android and info.plist for iphone the location package will be responsible for asking for location permission from the user at runtime

Hope it helps and good luck

like image 8
Sami Kanafani Avatar answered Oct 25 '22 00:10

Sami Kanafani