Apply for amap API
Website address: lbs.amap.com/
- The console creates android applications
- Gets the local test SHA1 security code
- Into the C: \ Users \ admin android
- perform
keytool -list -v -keystore debug.keystore
- The default key is:
android
- Open the Flutter project android -> app -> SRC -> main -> Androidmanifest.xml file
- The second line of code package corresponds to the name packageName
The keystore secret key is generated
- Create a folder on the destination disk to store the keystore key file (in this case, store it in drive D).
- Command window execution
keytool -genkey -v -keystore D:\keystore\key.jks -keyalg RSA -keysize 2048 -validity 10000 -alias key
- In the project android -> app -> build.gradle add, corresponding address and password, test formal can use the same
- Used separately can visit: www.cnblogs.com/zhireshini/.
- Execute in the keystore folder
keytool -list -v -keystore key.jks
Get the release sha1 code - Amap API key configuration is complete here.
Use the Flutter Aude map location function
- Android -> app -> SRC -> main -> Androidmanifest.xml configuration
<! -- Access the network -->
<uses-permission android:name="android.permission.INTERNET" />
<! -- Rough location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<! -- Precise positioning -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<! -- Request to call a-GPS module -->
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<! -- Used to obtain carrier information, used to support the interface related to providing carrier information -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<! -- To access wifi network information, wifi information will be used for network location -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<! Wifi information will be used to locate the network -->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<! -- To read the current state of the phone -->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<! -- Used to write cache data to an extended memory card -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Copy the code
- In application
<! -- Configure ApiKey -->
<meta-data
android:name="com.amap.api.v2.apikey"
android:value=Amap API Key />
<! -- configure location Service -->
<service android:name="com.amap.api.location.APSService"/>
Copy the code
- Override this code over main.dart
import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:amap_flutter_location/amap_flutter_location.dart';
import 'package:amap_flutter_location/amap_location_option.dart';
import 'package:permission_handler/permission_handler.dart';
void main() {
runApp(new MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
Map<String.Object>? _locationResult;
StreamSubscription<Map<String.Object> >? _locationListener; AMapFlutterLocation _locationPlugin =new AMapFlutterLocation();
@override
void initState() {
super.initState();
///Dynamically apply for location permission
requestPermission();
///Set the apiKey for Android and iOS<br>
///Please refer to the official website of Autonavi open Platform for application of key<br>
///Android: https://lbs.amap.com/api/android-location-sdk/guide/create-project/get-key
///iOS: https://lbs.amap.com/api/ios-location-sdk/guide/create-project/get-key
AMapFlutterLocation.setApiKey("android api key"."IOS Api Key");
///IOS obtains the native precision type
if (Platform.isIOS) {
requestAccuracyAuthorization();
}
///Register location result listening
_locationListener = _locationPlugin.onLocationChanged().listen((Map<String.Object> result) {
print(result);
setState(() {
_locationResult = result;
});
});
}
@override
void dispose() {
super.dispose();
///Remove location listen
if (null! = _locationListener) { _locationListener? .cancel(); }///Destruction of positioning
_locationPlugin.destroy();
}
///Setting Location Parameters
void _setLocationOption() {
AMapLocationOption locationOption = new AMapLocationOption();
///Whether to locate a single time
locationOption.onceLocation = false;
///Whether to return the inverse geographic information
locationOption.needAddress = true;
///Language types of inverse geographic information
locationOption.geoLanguage = GeoLanguage.DEFAULT;
locationOption.desiredLocationAccuracyAuthorizationMode = AMapLocationAccuracyAuthorizationMode.ReduceAccuracy;
locationOption.fullAccuracyPurposeKey = "AMapLocationScene";
///Set the interval for continuous positioning on the Android terminal
locationOption.locationInterval = 2000;
///Set the location mode on the Android terminal<br>
///Optional value:<br>
///<li>[AMapLocationMode.Battery_Saving]</li>
///<li>[AMapLocationMode.Device_Sensors]</li>
///<li>[AMapLocationMode.Hight_Accuracy]</li>
locationOption.locationMode = AMapLocationMode.Hight_Accuracy;
///Set the minimum positioning update distance on the iOS<br>
locationOption.distanceFilter = - 1;
///Set the desired positioning accuracy of the iOS terminal
///Optional value:<br>
/// <li>[DesiredAccuracy.Best] Maximum accuracy</li>
/// <li>[DesiredAccuracy BestForNavigation] is suitable for high precision navigation scenarios</li>
/// <li>[DesiredAccuracy NearestTenMeters] 10 meters</li>
/// <li>[DesiredAccuracy. Kilometer] 1000 meters</li>
/// <li>[DesiredAccuracy ThreeKilometers] 3000 meters</li>
locationOption.desiredAccuracy = DesiredAccuracy.Best;
///Set whether the iOS allows the system to pause the location
locationOption.pausesLocationUpdatesAutomatically = false;
///Set the location parameters to the location plug-in
_locationPlugin.setLocationOption(locationOption);
}
///To locate
void _startLocation() {
///Set location parameters before starting location
_setLocationOption();
_locationPlugin.startLocation();
}
///Stop positioning
void _stopLocation() {
_locationPlugin.stopLocation();
}
Container _createButtonContainer() {
return new Container(
alignment: Alignment.center,
child: new Row(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
new ElevatedButton(
onPressed: _startLocation,
child: new Text('Start positioning'),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.blue),
foregroundColor: MaterialStateProperty.all(Colors.white),
),
),
new Container(width: 20.0),
new ElevatedButton(
onPressed: _stopLocation,
child: new Text('Stop positioning'),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.blue),
foregroundColor: MaterialStateProperty.all(Colors.white),
),
)
],
));
}
Widget _resultWidget(key, value) {
return new Container(
child: new Row(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
new Container(
alignment: Alignment.centerRight,
width: 100.0,
child: new Text('$key: '),),new Container(width: 5.0),
new Flexible(child: new Text('$value', softWrap: true)),,),); }@override
Widget build(BuildContext context) {
List<Widget> widgets = <Widget>[];
widgets.add(_createButtonContainer());
if(_locationResult ! =null) { _locationResult? .forEach((key, value) { widgets.add(_resultWidget(key, value)); }); }return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: new Text('AMap Location plugin example app'),
),
body: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: widgets,
),
));
}
///Obtain the iOS Native accuracyAuthorization type
void requestAccuracyAuthorization() async {
AMapAccuracyAuthorization currentAccuracyAuthorization = await _locationPlugin.getSystemAccuracyAuthorization();
if (currentAccuracyAuthorization == AMapAccuracyAuthorization.AMapAccuracyAuthorizationFullAccuracy) {
print("Precise positioning type");
} else if (currentAccuracyAuthorization == AMapAccuracyAuthorization.AMapAccuracyAuthorizationReducedAccuracy) {
print("Fuzzy location type");
} else {
print("Unknown location type"); }}///Dynamically apply for location permission
void requestPermission() async {
// Apply for permission
bool hasLocationPermission = await requestLocationPermission();
if (hasLocationPermission) {
print("Location permission application approved");
} else {
print("Location permission application failed"); }}///Applying for Location Permission
///Return true if location permission is granted, false otherwise
Future<bool> requestLocationPermission() async {
// Obtain the current permissions
var status = await Permission.location.status;
if (status == PermissionStatus.granted) {
// Already authorized
return true;
} else {
// If no authorization is granted, an application is initiated
status = await Permission.location.request();
if (status == PermissionStatus.granted) {
return true;
} else {
return false; }}}}Copy the code
- The last execution
flutter run