我的网摘
win11系统,当我使用v2rayN开启网络代理之后,如何在powershell中挂上代理?
Gitee API 文档
天地图API
location install | Flutter package
location example | Flutter package
location | Flutter package
本站点使用 MrDoc 构建
-
+
location | Flutter package
[](https://pub.dev/packages/location) [](https://pub.dev/packages/leancode_lint) [](https://docs.page/) [](https://codecov.io/gh/Lyokone/flutterlocation) This plugin for [Flutter](https://flutter.dev/) handles getting a location on Android and iOS. It also provides callbacks when the location is changed. [](https://www.youtube.com/watch?feature=player_embedded&v=65qbtJMltVk) [Web demo](https://lyokone.github.io/flutterlocation) (more features available on Android/iOS) Add this to your package's `pubspec.yaml` file: ``` dependencies: location: ^5.0.0 ``` copied to clipboard ### Android [#](#android) To use location background mode on Android, you have to use the enableBackgroundMode({bool enable}) API before accessing location in the background and adding necessary permissions. You should place the required permissions in your applications ``` <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/> ``` copied to clipboard Remember that the user has to accept the location permission to `always allow` to use the background location. The Android 11 option to `always allow` is not presented on the location permission dialog prompt. The user has to enable it manually from the app settings. This should be explained to the user on a separate UI that redirects the user to the app's location settings managed by the operating system. More on that topic can be found on [Android developer](https://developer.android.com/training/location/permissions#request-background-location) pages. And to use it in iOS, you have to add this permission in Info.plist : ``` // This is probably the only one you need. Background location is supported // by this -- the caveat is that a blue badge is shown in the status bar // when the app is using location service while in the background. NSLocationWhenInUseUsageDescription // Deprecated, use NSLocationAlwaysAndWhenInUseUsageDescription instead. NSLocationAlwaysUsageDescription // Use this very carefully. This key is required only if your iOS app // uses APIs that access the user’s location information at all times, // even if the app isn't running. NSLocationAlwaysAndWhenInUseUsageDescription ``` copied to clipboard To receive location when application is in background, to Info.plist you have to add property list key : ``` UIBackgroundModes ``` copied to clipboard with string value: ``` location ``` copied to clipboard Nothing to do, the plugin works directly out of box. Ensure that the application is properly "sandboxed" and that the location is enabled. You can do this in Xcode with the following steps: 1. In the project navigator, click on your application's target. This should bring up a view with tabs such as "General", "Capabilities", "Resource Tags", etc. 2. Click on the "Capabilities" tab. This will give you a list of items such as "App Groups", "App Sandbox", and so on. Each item will have an "On/Off" button. 3. Turn on the "App Sandbox" item and press the ">" button on the left to show the sandbox stuff. 4. In the "App Data" section, select "Location". Add this permission in Info.plist : ``` NSLocationWhenInUseUsageDescription NSLocationAlwaysUsageDescription ``` copied to clipboard Then you just have to import the package with ``` import 'package:location/location.dart'; ``` copied to clipboard In order to request location, you should always check Location Service status and Permission status manually ``` Location location = Location(); bool serviceEnabled; PermissionStatus permissionGranted; LocationData locationData; serviceEnabled = await location.serviceEnabled(); if (!serviceEnabled) { serviceEnabled = await location.requestService(); if (!serviceEnabled) { return; } } permissionGranted = await location.hasPermission(); if (permissionGranted == PermissionStatus.denied) { permissionGranted = await location.requestPermission(); if (permissionGranted != PermissionStatus.granted) { return; } } locationData = await location.getLocation(); ``` copied to clipboard You can also get continuous callbacks when your position is changing: ``` location.onLocationChanged.listen((LocationData currentLocation) { // Use current location }); ``` copied to clipboard To receive location when application is in background you have to enable it: ``` location.enableBackgroundMode(enable: true) ``` copied to clipboard Be sure to check the example project to get other code samples. On Android, a foreground notification is displayed with information that location service is running in the background. On iOS, while the app is in the background and gets the location, the blue system bar notifies users about updates. Tapping on this bar moves the User back to the app.   | Return | Description | | --- | --- | | Future<PermissionStatus> | **requestPermission()** Request the Location permission. Return a PermissionStatus to know if the permission has been granted. | | Future<PermissionStatus> | **hasPermission()** Return a PermissionStatus to know the state of the location permission. | | Future<bool> | **serviceEnabled()** Return a boolean to know if the Location Service is enabled or if the user manually deactivated it. | | Future<bool> | **requestService()** Show an alert dialog to request the user to activate the Location Service. On iOS, will only display an alert due to Apple Guidelines, the user having to manually go to Settings. Return a boolean to know if the Location Service has been activated (always `false` on iOS). | | Future<bool> | **changeSettings(LocationAccuracy accuracy = LocationAccuracy.HIGH, int interval = 1000, double distanceFilter = 0)** Will change the settings of future requests. `accuracy`will describe the accuracy of the request (see the LocationAccuracy object). `interval` will set the desired interval for active location updates, in milliseconds (only affects Android). `distanceFilter` set the minimum displacement between location updates in meters. | | Future<LocationData> | **getLocation()** Allow to get a one time position of the user. It will try to request permission if not granted yet and will throw a `PERMISSION_DENIED` error code if permission still not granted. | | Stream<LocationData> | **onLocationChanged** Get the stream of the user's location. It will try to request permission if not granted yet and will throw a `PERMISSION_DENIED` error code if permission still not granted. | | Future<bool> | **enableBackgroundMode({bool enable})** Allow or disallow to retrieve location events in the background. Return a boolean to know if background mode was successfully enabled. | You should try to manage permission manually with `requestPermission()` to avoid error, but plugin will try handle some cases for you. ``` class LocationData { final double latitude; // Latitude, in degrees final double longitude; // Longitude, in degrees final double accuracy; // Estimated horizontal accuracy of this location, radial, in meters final double altitude; // In meters above the WGS 84 reference ellipsoid final double speed; // In meters/second final double speedAccuracy; // In meters/second, always 0 on iOS and web final double heading; // Heading is the horizontal direction of travel of this device, in degrees final double time; // timestamp of the LocationData final bool isMock; // Is the location currently mocked } enum LocationAccuracy { powerSave, // To request best accuracy possible with zero additional power consumption, low, // To request "city" level accuracy balanced, // To request "block" level accuracy high, // To request the most accurate locations available navigation // To request location for navigation usage (affect only iOS) } // Status of a permission request to use location services. enum PermissionStatus { /// The permission to use location services has been granted. granted, // The permission to use location services has been denied by the user. May have been denied forever on iOS. denied, // The permission to use location services has been denied forever by the user. No dialog will be displayed on permission request. deniedForever } ``` copied to clipboard Note: you can convert the timestamp into a `DateTime` with: `DateTime.fromMillisecondsSinceEpoch(locationData.time.toInt())` Please feel free to [give me any feedback](https://github.com/Lyokone/flutterlocation/issues) helping support this plugin !
lx271896700
2026年4月7日 15:25
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
Markdown文件
PDF文档(打印)
分享
链接
类型
密码
更新密码