【问题标题】:Flutter - google map, does not show my locationFlutter - 谷歌地图,不显示我的位置
【发布时间】:2019-06-22 21:27:44
【问题描述】:

似乎一切都写对了,但我不明白为什么它没有在地图上显示我的位置。

为什么不显示我的位置?

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

【问题讨论】:

  • 您是否请求了位置权限?
  • 但是怎么做呢?
  • &lt;uses-permission android:name= "android.permission.ACCESS_COARSE_LOCATION"/&gt; &lt;uses-permission android:name= "android.permission.ACCESS_FINE_LOCATION"/&gt; 将这两行粘贴到您的 Android 清单文件中。如果您在模拟器中运行,则切换到物理设备。因为模拟器无法显示您当前的位置。

标签: javascript flutter google-maps-markers


【解决方案1】:

如果您想在地图上查看您的当前位置,并启用将您带到那里的右下方FAB功能,请确保您拥有属性myLocationEnabled 在您的 GoogleMap Widget 上设置为 true

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

【讨论】:

    【解决方案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 for android 和info.plist for iphone 中添加定位权限 位置包将负责在运行时向用户请求位置权限

    希望对你有帮助,祝你好运

    【讨论】:

    • 一般位置的有用答案,但不是 GoogleMap Flutter 小部件的正确答案
    【解决方案3】:

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

    安卓:AndroidManifest.xml

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

    【讨论】:

      【解决方案4】:

      我建议使用下面的方法。首先你设置 LocationData currentLocation; 变量位置 = 新位置();在 global.Second 你创建 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;
      }}
      

      【讨论】:

      • 嗨,欢迎来到 SO,请提供一些关于您的代码的作用的解释,它可以帮助其他可能正在寻找解决方案的用户理解:)
      • 首先,对不起我的英语不好。你设置 LocationData currentLocation;变量位置 = 新位置();这是全局的,并且在您创建 myCurrentLoacation 方法之后。最后您使用方法 initState 或单击任何按钮来获取当前用户位置。
      • 没什么可抱歉的,感谢您添加更多详细信息,现在只需编辑您的答案并在此处添加此信息。
      【解决方案5】:
          GoogleMap(
      onMapCreated: _onMapCreated,
      initialCameraPosition: CameraPosition(
      target: _center,
      zoom: 13.0,
      ),
      myLocationEnabled: true,
      onCameraMove: _onCameraMove,
      myLocationButtonEnabled: true,
      

      【讨论】:

      • @devpato 这不是仅链接的答案。它有代码,甚至没有链接。如果您认为此代码错误或无用,正确的做法是投反对票。
      • @RobertColumbia 我点击了错误的选项,所以给了我。我的意思是这个答案需要更多解释,而不仅仅是答案
      【解决方案6】:

      不只是 myLocationButtonEnabled,您还需要这样做

      myLocationEnabled: true,
      

      【讨论】:

        【解决方案7】:

        对于 Android:在调试模式下以及当您的应用正在开发中时...您需要在手机应用设置中手动授予位置权限并重新加载您的应用才能显示“我的位置”按钮。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-10-06
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多