简体   繁体   English

如何在 google_maps_flutter 中获取当前的纬度和经度?

[英]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.

相关问题 在 google_maps_flutter 中获取可见标记 - Get Visible Markers in google_maps_flutter 如何使用谷歌地图将初始相机 Position 设置为当前设备的纬度和经度 Flutter - How To Set Initial Camera Position To The Current Device's Latitude and Longitude Using Google Maps For Flutter 如何运行 google_maps_flutter 示例 - how to run google_maps_flutter example 如何使用 google_maps_flutter 添加标记? - How add marker with google_maps_flutter? 如何创建一个简单的谷歌地图地址搜索,并在颤动中自动完成并获取纬度和经度? - How to create a simple google maps address search with autocomplete in flutter and get latitude and longitude? 在 google_maps_flutter 中获取当前相机的 position 的问题 - Issue with getting current position of camera in google_maps_flutter 如何模拟“google_maps_flutter”包以进行颤振测试? - How to mock 'google_maps_flutter' package for flutter tests? 如何在 google_maps_flutter Flutter 插件中使用 SVG 标记? - How to use SVG markers in google_maps_flutter Flutter plugin? 如何在颤振中获得纬度和经度 - How to get latitude and longitude in flutter Flutter:如何在 google_maps_flutter 中为标记图标设置动画? - Flutter: How to animate marker icon in google_maps_flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM