[英]How to get current latitude & longitude in google_maps_flutter?
How can I get the LatLng
of user's current location when using google_maps_flutter
plugin?使用
google_maps_flutter
插件时如何获取用户当前位置的LatLng
?
I wish there had been something like:我希望有类似的东西:
GoogleMap(
onLocationChanged: (latLng) {
// Something like this callback ...
}
)
PS: I don't wish to use any other plugin. PS:我不想使用任何其他插件。
Flutter is a framework which allows you to create UI easily. Flutter 是一个允许您轻松创建 UI 的框架。 But behind this technology actually we don't have any native functionality.
但在这项技术背后实际上我们没有任何原生功能。 To bring these functionalities to flutter we have "Method Channel" or "Dart:FFI".
为了将这些功能带到 flutter,我们有“方法通道”或“Dart:FFI”。
If you look at the source code of plugin which brings native functions to flutter they mostly use "Method Channel".如果您查看将本机功能带到 flutter 的插件的源代码,它们主要使用“方法通道”。
Google Maps plugin allows you to use google maps api, but however if you want to use or get client location, you must have some native code.谷歌地图插件允许你使用谷歌地图 api,但是如果你想使用或获取客户端位置,你必须有一些本地代码。
Some 3rd party apps already offer this functionality for you, but if you want to develop it for yourself, you have to write your own code for Android & iOS.一些 3rd 方应用程序已经为您提供了此功能,但如果您想自己开发,您必须为 Android 和 iOS 编写自己的代码。 Then you need to link it with the flutter.
然后您需要将其与 flutter 链接。 To do this you need to use method channel .
为此,您需要使用方法 channel 。
Maybe what you need is the location package from Flutter dev.也许您需要的是 Flutter dev 的 package位置。
With that you can just listen for location changes, and maybe update your current location variable.有了它,您可以只监听位置变化,并可能更新您当前的位置变量。
Something like:就像是:
location.onLocationChanged().listen((LocationData newLoc) {
currentLocation = newLoc;
});
There is no way to get current location from google_maps_flutter plugin, but you can get displaying location either by无法从 google_maps_flutter 插件获取当前位置,但您可以通过以下方式获取显示位置
GoogleMap(
onCameraMove: (LatLng displayingLocation){
// do operations here
}
)
or by using map controller或使用 map controller
mapController.getLatLng(ScreenCoordinate(
x: MediaQuery.of(context).size.width,
y: MediaQuery.of(context).size.height))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.