You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
3 months ago | |
|---|---|---|
| .. | ||
| android | 4 months ago | |
| example | 4 months ago | |
| ios | 4 months ago | |
| lib | 4 months ago | |
| test | 4 months ago | |
| .gitignore | 4 months ago | |
| .metadata | 4 months ago | |
| CHANGELOG.md | 4 months ago | |
| LICENSE | 4 months ago | |
| README.md | 4 months ago | |
| analysis_options.yaml | 4 months ago | |
| pubspec.yaml | 4 months ago | |
README.md
location_plugin
A Flutter plugin that exposes:
getCurrentLocation— backed by native Android (Google Play Services) and iOS (CoreLocation) implementations, returning{latitude, longitude}.getCityInfo— a Dart-side helper that calls Huawei Site reverse-geocode API to translate coordinates into city code and region names.
Usage
final plugin = LocationPlugin();
final location = await plugin.getCurrentLocation();
if (location != null) {
final lat = location['latitude'];
final lng = location['longitude'];
final cityInfo = await plugin.getCityInfo(latitude: lat!, longitude: lng!);
if (cityInfo != null) {
debugPrint('code=${cityInfo.code}, region=${cityInfo.region}');
}
}
The plugin requests runtime permissions when required and surfaces any failures through PlatformException.
Permissions and configuration
Android
- The plugin declares
ACCESS_FINE_LOCATIONandACCESS_COARSE_LOCATIONin its own manifest. - Ensure Google Play services are available on the target device.
iOS
- Add
NSLocationWhenInUseUsageDescriptionto your host appInfo.plist. - The bundled
PrivacyInfo.xcprivacyfile declares the required-reason API entry for location (L2). Update it if your use case differs.
Example app
The example/ app fetches the current location on launch and provides a refresh button so you can validate the full flow end-to-end.
Implementation details
- Android uses the fused location provider (
getCurrentLocationwith a fallback single update) when Google Play Services are available, and automatically falls back to the platformLocationManageron devices(如部分国内机型)缺少 GMS 的场景。 - iOS relies on
CLLocationManager.requestLocation()and listens for authorization changes before requesting a single fix. getCityInfoposts{lat, lng}tohttps://siteapi.cloud.huawei.com/mapApi/v1/siteService/reverseGeocode. A default demo key is bundled; supply your ownapiKeyparameter in production and adjust error handling as needed.
Feel free to extend the plugin with background tracking, geofencing, or reverse-geocoding as needed.