Android Studio2021.1.1 This article is enough for Gaode map api call

It's time for me to avoid all kinds of CSDs. I've been working on CSDs for four hours. I really hope it's time for me to avoid CSDs. I've been working on CSDs for four hours. It's time for me to write CSDs...

1, Gaode API Application

API high website
After we registered the developer account, we found this interface

When we add, we will encounter the following two options, which are also the key to the success of our project

1.1 SHA1 acquisition

Many of the teaching materials I'm looking for are asking us to create APK ourselves, and then what is re - creation jks, isn't it fragrant to get it directly???
C: There will be one under \ users \ twy (your own user name)

After entering, enter cmd directly on this page

Enter in cmd

keytool -v -list -keystore debug.keystore      (keystore (my name)

The keystore password in the figure above defaults to android
(Note: you can't see the password, just Enter after entering)

1.1 package name acquisition

2, In project configuration

2.1 configuring libs

After configuring the above information, we remember to download the official lib package
Just click here to download
Download Android map SDK (if you have other needs, please find it here)
After downloading

Move directly to us

Download and unzip the file and place it in the lis directory. In this case, you need to add it into the environment variable
Add As Library

2.2 configure androidmanifest xml

Official call document

It's directly on our Android manifest XML plus two sentences

First sentence:

   <meta-data android:name="com.amap.api.v2.apikey" android:value="Yours key"></meta-data>
        <service android:name="com.amap.api.location.APSService"></service>

Your key will be generated by Gaode. Remember to go back and copy it

Second sentence:

<uses-permission android:name="android.permission.INTERNET" />
    <!--Used for network positioning-->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
    <!--For access GPS location-->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
    <!--Obtain operator information to support the provision of interfaces related to operator information-->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
    <!--For access wifi Network information, wifi The information will be used for network positioning-->
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
    <!--This permission is used to get wifi Access to, wifi Information will be used for network positioning-->
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
    <!--It is used to access the network. Network positioning requires Internet access-->
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <!--Used to read the current status of the phone-->
    <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
    <!--Write to the expansion storage and write data to the expansion card for writing cache positioning data-->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
    <!--Used to apply for call A-GPS modular-->
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"></uses-permission>
    <!--It is used to apply for obtaining Bluetooth information for indoor positioning-->
    <uses-permission android:name="android.permission.BLUETOOTH"></uses-permission>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"></uses-permission>

Add effect

2.3 writing MainActivity

package com.example.myapplication;

import android.os.Bundle;

import android.os.Bundle;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;

public class MainActivity extends AppCompatActivity {
    //Declare AMapLocationClient class object
    public AMapLocationClient mLocationClient = null;
    //Declare location callback listener
    public AMapLocationListener mLocationListener = new MyAMapLocationListener();
    //Declare AMapLocationClientOption object
    public AMapLocationClientOption mLocationOption = null;

    protected void onCreate(Bundle savedInstanceState) {


    private void init() {
        //Initialize positioning
        try {
            AMapLocationClient.updatePrivacyShow(this, true, true);
            AMapLocationClient.updatePrivacyAgree(this, true);
            mLocationClient = new AMapLocationClient(getApplicationContext());
        } catch (Exception e) {
        //Set location callback listening
        //Initialize the AMapLocationClientOption object
        mLocationOption = new AMapLocationClientOption();
        //Set the location mode to amaplocationmode Hight_ Accuracy, high precision mode.
        //Get the location result once:
        //This method defaults to false.

        //Obtain the positioning result with the highest accuracy in the last 3s:
        //Set the setoncelocationlast (Boolean b) interface to true. When starting positioning, the SDK will return the positioning result with the highest accuracy in the last 3s. If it is set to true, the setOnceLocation(boolean b) interface will also be set to true. Otherwise, it will not. The default is false.
        //Set whether to return address information (default return address information)
        //Set whether to allow analog location. The default value is false. Analog location is not allowed
        //Turn off caching mechanism
        //Set positioning parameters for positioning client objects
        //Start positioning


    private class MyAMapLocationListener implements AMapLocationListener {

        public void onLocationChanged(AMapLocation aMapLocation) {
            if (aMapLocation != null) {
                if (aMapLocation.getErrorCode() == 0) {
                    Log.e("Location:", aMapLocation.getAddress());
                } else {
                    //When positioning fails, the reason for the failure can be determined through ErrCode information. errInfo is the error information. See the error code table for details.
                    Log.e("AmapError", "location Error, ErrCode:"
                            + aMapLocation.getErrorCode() + ", errInfo:"
                            + aMapLocation.getErrorInfo());

3, Realization effect

4, Summary of common errors and Solutions

1.import reported an error

as a result of. support.v7 doesn't support this method anymore. Change to the latest calling method


2. Errorcode: 555570 please set the privacy policy before using the loc SDK function. Whether to pop up and inform the user

This is the official document given to us. Unfortunately, there is an error. In the latest version, we not only need to use try to accept the return for this object, but also need to add it in front

AMapLocationClient.updatePrivacyShow(this, true, true);
AMapLocationClient.updatePrivacyAgree(this, true);

3.minSDK(API XX)>devicesdk(API XX) on device XXX

In other words, the minimum version we support is higher. We can directly find build gradle
(there are two files in build.gradle. We open build.gradle in the app directory!!!)

4. E/AmapError: location Error, ErrCode:4, errInfo: abnormal network connection

Brother, our mobile phones are not connected to the Internet

5. We don't have permission. We just open the settings on our mobile phone and give the generated files permission

If you encounter any problems, please feel free to come to Xiao Tang. For other problems, please add!!!

Tags: Android Android Studio

Posted by bytes on Thu, 14 Apr 2022 02:03:39 +0930