Flutter-Google地图,不显示我的位置

时间:2019-01-29 17:17:37

标签: javascript flutter google-maps-markers

似乎一切都写得正确,但我不明白为什么它不能在地图上显示我的位置。

为什么不显示我的位置?

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,
            ),
          ),
        ),
      );
  }
}

6 个答案:

答案 0 :(得分:2)

您需要使用Flutter的location包来跟踪用户的位置,并且需要将此功能添加到代码中

_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;
    }
  }

应在您的initState()函数中按如下所示调用此函数

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

并且不要忘记将位置信息包导入到您的代码中,

import 'package:location/location.dart';

请按照位置包的安装指南进行操作,因为您需要在AndroidManifest.xml(对于Android)和info.plist(对于iphone)中添加位置许可 位置包将负责在运行时向用户请求位置权限

希望有帮助,祝你好运

答案 1 :(得分:1)

您必须提供对当前位置的访问权限 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" />

答案 2 :(得分:1)

如果您想在地图上查看您的当前位置,并启用右下角的 FAB 功能,请确保您拥有该属性在您的GoogleMap小部件上将myLocationEnabled 设置为 true

      [...]
      body: GoogleMap(
          myLocationEnabled: true,
          [...]
      )

答案 3 :(得分:0)

我建议使用以下方法。首先,将LocationData设置为currentLocation;      var location = new Location();在global中创建第二个方法,然后创建myCurrentLocation()方法,最后使用它initState()或单击按钮。不要忘记在AndroidMainfest.xml和此类路径'com.google.gms:google-services:4.3.2'android中导入/build.gradle。

 LocationData currentLocation;
 var location = new Location();

 myCurrentLocation() async{
   try {
      currentLocation = await location.getLocation();
      print("locationLatitude: ${currentLocation.latitude.toString()}");
      print("locationLongitude: ${currentLocation.longitude.toString()}");
   } on PlatformException catch (e) {
   if (e.code == 'PERMISSION_DENIED') {
      String error = 'Permission denied';
      print(error);
   }
   currentLocation = null;
}}

答案 4 :(得分:0)

    GoogleMap(
onMapCreated: _onMapCreated,
initialCameraPosition: CameraPosition(
target: _center,
zoom: 13.0,
),
myLocationEnabled: true,
onCameraMove: _onCameraMove,
myLocationButtonEnabled: true,

答案 5 :(得分:0)

不仅 myLocationButtonEnabled 足够你还需要

myLocationEnabled: true,
相关问题