Max*_*bko 9 javascript google-maps-markers flutter
它似乎已经正确地写了所有内容,但我不明白为什么它没有在地图上显示我的位置。
为什么不显示我的位置?
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,
),
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
Men*_*lek 10
如果您想在地图上查看您的当前位置,并启用将您带到那里的右下方FAB功能,请确保您在 GoogleMap Widget上将属性myLocationEnabled设置为true。
[...]
body: GoogleMap(
myLocationEnabled: true,
[...]
)
Run Code Online (Sandbox Code Playgroud)
你需要使用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;
}
}
Run Code Online (Sandbox Code Playgroud)
这个函数应该在你的initState()函数中调用如下
@override
void initState() {
_getLocation();
super.initState();
}
Run Code Online (Sandbox Code Playgroud)
并且不要忘记将位置包导入到您的代码中,
import 'package:location/location.dart';
Run Code Online (Sandbox Code Playgroud)
按照位置包的安装指南,因为您需要AndroidManifest.xml为android和info.plistiphone添加位置权限,位置包将负责在运行时向用户请求位置权限
希望它有帮助并祝你好运
您必须提供对当前位置 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>
Run Code Online (Sandbox Code Playgroud)
安卓:AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Run Code Online (Sandbox Code Playgroud)