Android Studio2021.1.1 AutoNavi map api call this one is enough

This article has participated in the "Newcomer Creation Ceremony" activity, and started the road of Nuggets creation together

Xiao Tang is developing Android mobile applications recently, which is really outrageous. The various teaching materials on csdn are really outrageous. Children will be vomited. What should be encountered, and what should not be encountered, are basically by me. I met it, and I worked for three or four hours. I wrote it in a shallow way, hoping that children in the future can avoid the pit. . .

1. AutoNavi API application

AutoNavi API URL

After we register to play the developer account, find this interfaceinsert image description here

When we add, we will encounter the following two options, which are also the key to the success of our projectinsert image description here

1.1 SHA1 acquisition

A lot of teaching materials I've been looking for are asking us to create APK ourselves, and then what is re-creating .jks, isn't it good to get it directly? ? ?

C:\Users\twy(你自己的用户名)下会有一个 insert image description hereAfter entering, enter cmd directly on this page and enter insert image description herein cmd

keytool -v -list -keystore debug.keystore      (keystore的名字)
复制代码

insert image description hereThe default keystore password in the above figure is: android

(Note: Entering the password is invisible, just enter Enter)

1.1 Get the package name

insert image description here

2. Configuration within the project

2.1 Configure libs

After configuring the above information, we remember to download the official lib package

Just click here to download

Android map SDK related downloads (if you have other needs, find them here)

After downloading

insert image description here

insert image description here

Move directly to us

insert image description here

Download and decompress it and place it in the lis directory. Special attention needs to be added to the environment variable Add As Library

insert image description here

2.2 Configure AndroidManifest.xml

Official call documentation

Just add two sentences directly to our AndroidManifest.xml

first sentence:

   <meta-data android:name="com.amap.api.v2.apikey" android:value="你的key"></meta-data>
        <service android:name="com.amap.api.location.APSService"></service>
复制代码

insert image description hereYour key will be generated by Gaode, remember to go back and look at it, and then copy it up

Second sentence:

<uses-permission android:name="android.permission.INTERNET" />
    <!--用于进行网络定位-->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
    <!--用于访问GPS定位-->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
    <!--获取运营商信息,用于支持提供运营商信息相关的接口-->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
    <!--用于访问wifi网络信息,wifi信息会用于进行网络定位-->
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
    <!--这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位-->
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
    <!--用于访问网络,网络定位需要上网-->
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <!--用于读取手机当前的状态-->
    <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
    <!--写入扩展存储,向扩展卡写入数据,用于写入缓存定位数据-->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
    <!--用于申请调用A-GPS模块-->
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"></uses-permission>
    <!--用于申请获取蓝牙信息进行室内定位-->
    <uses-permission android:name="android.permission.BLUETOOTH"></uses-permission>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"></uses-permission>
复制代码

add effect here is the quote

2.3 Writing MainActivity

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
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 {
    //声明AMapLocationClient类对象
    public AMapLocationClient mLocationClient = null;
    //声明定位回调监听器
    public AMapLocationListener mLocationListener = new MyAMapLocationListener();
    //声明AMapLocationClientOption对象
    public AMapLocationClientOption mLocationOption = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
    }

    private void init() {
        //初始化定位
        try {
            AMapLocationClient.updatePrivacyShow(this, true, true);
            AMapLocationClient.updatePrivacyAgree(this, true);
            mLocationClient = new AMapLocationClient(getApplicationContext());
        } catch (Exception e) {
            e.printStackTrace();
        }
        //设置定位回调监听
        mLocationClient.setLocationListener(mLocationListener);
        //初始化AMapLocationClientOption对象
        mLocationOption = new AMapLocationClientOption();
        //设置定位模式为AMapLocationMode.Hight_Accuracy,高精度模式。
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //获取一次定位结果:
        //该方法默认为false。
        mLocationOption.setOnceLocation(false);

        //获取最近3s内精度最高的一次定位结果:
        //设置setOnceLocationLatest(boolean b)接口为true,启动定位时SDK会返回最近3s内精度最高的一次定位结果。如果设置其为true,setOnceLocation(boolean b)接口也会被设置为true,反之不会,默认为false。
        mLocationOption.setOnceLocationLatest(true);
        //设置是否返回地址信息(默认返回地址信息)
        mLocationOption.setNeedAddress(true);
        //设置是否允许模拟位置,默认为false,不允许模拟位置
        mLocationOption.setMockEnable(false);
        //关闭缓存机制
        mLocationOption.setLocationCacheEnable(false);
        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
        //启动定位
        mLocationClient.startLocation();

    }

    private class MyAMapLocationListener implements AMapLocationListener {

        @Override
        public void onLocationChanged(AMapLocation aMapLocation) {
            if (aMapLocation != null) {
                if (aMapLocation.getErrorCode() == 0) {
                    Log.e("位置:", aMapLocation.getAddress());
                } else {
                    //定位失败时,可通过ErrCode(错误码)信息来确定失败的原因,errInfo是错误信息,详见错误码表。
                    Log.e("AmapError", "location Error, ErrCode:"
                            + aMapLocation.getErrorCode() + ", errInfo:"
                            + aMapLocation.getErrorInfo());
                }
            }
        }
    }
}
复制代码

3. Realize the effect

insert image description here

4. Summary of common errors and solutions

1.import android.support.v7.app.AppCompatActivity报错

The reason is that this method of .support.v7 is no longer supported, and replaced with the latest calling method

import androidx.appcompat.app.AppCompatActivity;
复制代码

2.errorCode : 555570 Before using the loc SDK function, please set the privacy policy to inform the user in a pop-up window

This is the official document given to us. Unfortunately, something went wrong. In the latest version, we not only use try to accept the return for this object, but also need to add

AMapLocationClient.updatePrivacyShow(this, true, true);
AMapLocationClient.updatePrivacyAgree(this, true);
复制代码

insert image description here insert image description here

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

To put it bluntly, the minimum version we support is higher, we directly find build.gradle (build.gradle has two files in total, we open the build.gradle in the app directory!!!) insert image description here

4. E/AmapError: location Error, ErrCode: 4, errInfo: Network connection abnormal please go to

Brother, our phone is not connected to the Internet

5. Or what to say, we don't have permission, we can directly open the settings on our mobile phone, and open the permission for the file we generate.

If you encounter any problems, please feel free to come to Xiaotang at any time. If you have any other questions, please feel free to add them! ! !

Guess you like

Origin juejin.im/post/7086141399304765448