“This article has participated in the call for good writing activities, click to view: the back end, the big front end double track submission, 20,000 yuan prize pool waiting for you to challenge!”

Positioning integration

Configure the Gaud location dependency library

Create a Flutter project and open the Android project in the project using Android Studio, or right-click the Android folder -> Flutter -> Open Android Module in Android Studio.

In the Android project, switch to Android View:

Open the build.gradle file and add the location dependency package:

Implementation (' com. Amap. API: location: 5.2.0 ')Copy the code

Click Sync Now or the 🐘 icon on the toolbar to synchronize dependencies:

Open the manifest file androidmanifest.xml and configure permissions and services:

<! - access to the network - > < USES - permission android: name = "android. Permission. INTERNET" / > <! - a rough positioning - > < USES - permission android: name = "android. Permission. ACCESS_COARSE_LOCATION" / > <! - precise positioning - > < USES - permission android: name = "android. Permission. ACCESS_FINE_LOCATION" / > <! - application calls A - GPS module - > < USES - permission android: name = "android. Permission. ACCESS_LOCATION_EXTRA_COMMANDS" / > <! - information for operators, used to support to provide operators information interface - > < USES - permission android: name = ". Android. Permission ACCESS_NETWORK_STATE "/ > <! - used to access the wifi network information, wifi positioning information will be used for network - > < USES - permission android: name = "android. Permission. ACCESS_WIFI_STATE" / > <! - to get wifi access permissions, wifi positioning information will be used to carry out network - > < USES - permission android: name = "android. Permission. CHANGE_WIFI_STATE" / > <! --> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <! <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />Copy the code
<! - configuration positioning Service - > < Service android: name = "com. Amap. API. Location. APSService" / >Copy the code

Configuring signature Files

The Autonavi platform requires a signed SHA1, so you need to configure a signature file, a debug mode and a formal package, and you can also use the same keystore. Build -> Generate Signed Bundle/APK -> Next -> Create new

Key Store Path Select the root directory of the current project or any directory you like, and name it debug.keystore or whatever you like. The Password of the two places can be the same, and the Alias needs to be filled in.

Click OK -> Next, select Debug, or select both debug and Release if they use the same signature file, and check V2 signature:

When you’re done, you can see the debug.keystore file in your project:

Repeat the above method to create a release.keystore file. Click on the Project Structure:

Select Modules -> app -> Signing Configs -> + -> Debug by default, add a release and select the corresponding keystore:

Click OK and open the build.gradle file again. You can see that the signature configuration corresponds to the signature file you just configured:

signingConfigs {
        debug {
            storeFile file('/Users/apple/AndroidStudioProjects/flutter/map_demo/android/debug.keystore')
            storePassword '123456'
            keyAlias 'amap'
            keyPassword '123456'
        }
        release {
            storeFile file('/Users/apple/AndroidStudioProjects/flutter/map_demo/android/release.keytore')
            storePassword '123456'
            keyAlias 'amap'
            keyPassword '123456'
        }
    }
Copy the code

Autonavi Platform Key application

Open the gold open platform | Scott map API (amap.com), register as a user, gold open platform to open the console to create an application, fill in the corresponding information, there’s a mistake, SHA1 is MD5, don’t fill in SHA1.

To obtain MD5: in the previous project, open Terminal and type the following command. The path is debug.keystore and release.keystore:

keytool -list -v -keystore  ./debug.keystore
Copy the code

Input the Password of the secret key library, that is, the Password set, copy MD5, go to Autonavi open platform paste.

After both release and debug versions are set, copy the key.

In the manifest file, configure the Apikey:

<! - configuration positioning Service - > < Service android: name = "com. Amap. API. Location. APSService" / > < meta - data android:name="com.amap.api.v2.apikey" android:value="f6c46787c43cb7df5510d9f4c530fd1e"/>Copy the code

Flutter file Configuration

Go back to the Flutter project, add the Autonavi location library and permission request dependencies, and run pub get:

Amap_flutter_location: ^ 2.0.0 permission_handler:Copy the code

Configure the permission request in the get Location entry, register listening:

// requestPermission(); _locationListener = _locationplugin. onLocationChanged().listen((Map<String, Object> result) { setState(() { _locationResult = result; }); });Copy the code

Whenever I get a location, I’m going to call back, and result contains a lot of location information. If INVALID_USER_KEY is displayed, the MD5 of your keystore is not correct. The platform may change SHA1 to the real SHA1, so try filling in SHA1.

Map integration

Android project added map dependency

Back to the Android project, add map dependencies:

Implementation 'com. Amap. API: 3 dmap: 5.0.0'Copy the code

Go back to the Flutter project and add plugin dependencies to pubspec.yaml:

Amap_flutter_map: ^ 2.0.1Copy the code

Use map in layout:

Final AMapWidget map = AMapWidget(onMapCreated: onMapCreated, // Locate the small blue dot to configure myLocationStyleOptions: MyLocationStyleOptions(true), // Whether compass // compassEnabled: true,);Copy the code

Source code portal