好像都写对了,但是我不明白为什么它没有在地图上显示我的位置。
为什么不显示我的位置?
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,
),
),
),
);
}
}
如果你想在地图上看到你的当前位置,并启用右下角的FAB功能将你带到那里,请确保你的属性myLocationEnabled在你的GoogleMap Widget上设置为true。
[...]
body: GoogleMap(
myLocationEnabled: true,
[...]
)
您必须提供对您当前位置的访问权限 ios: 信息.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" />
你需要使用 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 添加位置权限
位置包将负责在运行时向用户请求位置许可
希望对你有帮助,祝你好运
对于 Android:在调试模式下以及当您的应用程序处于开发阶段时...您需要在手机应用程序设置中手动授予位置权限并重新加载您的应用程序以显示“我的位置”按钮。
我建议使用下面的方法。首先你设置 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;
}}
GoogleMap(
onMapCreated: _onMapCreated,
initialCameraPosition: CameraPosition(
target: _center,
zoom: 13.0,
),
myLocationEnabled: true,
onCameraMove: _onCameraMove,
myLocationButtonEnabled: true,
不仅仅是 myLocationButtonEnabled 就够了,你还需要
myLocationEnabled: true,
这对我有用,
如果您看到一切正常,只需卸载应用程序并重新安装即可。
谢谢。
就我而言,这是因为我忘记打开设备上的位置。