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
After we register to play the developer account, find 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
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(你自己的用户名)下会有一个
After entering, enter cmd directly on this page and enter in cmd
keytool -v -list -keystore debug.keystore (keystore的名字)
复制代码
The default keystore password in the above figure is: android
(Note: Entering the password is invisible, just enter Enter)
1.1 Get the package name
2. Configuration within the project
2.1 Configure libs
After configuring the above information, we remember to download the official lib package
Android map SDK related downloads (if you have other needs, find them here)
After downloading
Move directly to us
Download and decompress it and place it in the lis directory. Special attention needs to be added to the environment variable Add As Library
2.2 Configure AndroidManifest.xml
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>
复制代码
Your 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
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
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);
复制代码
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!!!)
4. E/AmapError: location Error, ErrCode: 4, errInfo: Network connection abnormal please go to
Brother, our phone is not connected to the Internet