首先去高德注册开发者账号
下面这两个选项,也是我们项目成功的关键
你的用户文件下面会有一个.android文件夹,进入文件夹,在这个路径下打开cmd
如果.android下面没有签名文件参考创建文章
输入: keytool -v -list -keystore debug.keystore(debug.keystore是你签名文件的名称)
下载完成后
复制jar到项目的libs下面,右键Add As Library
- <meta-data
- android:name="com.amap.api.v2.apikey"
- android:value="你的key">
- meta-data>
- <service android:name="com.amap.api.location.APSService" />
-
- <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
-
- <uses-permission android:name="android.permission.INTERNET" />
-
- <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
-
- <uses-permission android:name="android.permission.VIBRATE" />
-
- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
-
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
-
- <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
-
- <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" />
-
- <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
-
- <uses-permission android:name="android.permission.BLUETOOTH" />
- <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
位置演示:
- "1.0" encoding="utf-8"?>
- <LinearLayout
- android:orientation="vertical"
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".GDActivity">
-
- <Button
- android:layout_width="match_parent"
- android:text="高德定位"
- android:layout_height="wrap_content"
- android:id="@+id/btn_01"/>
-
- <TextView
- android:id="@+id/tv_site"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textColor="@color/black"
- android:layout_marginTop="10dp"
- android:textSize="15sp"/>
-
- LinearLayout>
- package com.example.civilclass;
-
- import android.Manifest;
- import android.content.pm.PackageManager;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
-
- import androidx.annotation.NonNull;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.core.app.ActivityCompat;
- import androidx.core.content.ContextCompat;
-
- 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 GDActivity extends AppCompatActivity implements View.OnClickListener {
-
- //声明AMapLocationClient类对象
- public AMapLocationClient mLocationClient = null;
- //声明定位回调监听器
- public AMapLocationListener mLocationListener = new MyAMapLocationListener();
- //声明AMapLocationClientOption对象
- public AMapLocationClientOption mLocationOption = null;
- private Button btn_01;
- private TextView tv_site;
-
- private static final int PERMISSION_REQUEST_LOCATION = 1;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_gdactivity);
- initView();
-
- // 检查定位权限
- if(ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION)
- != PackageManager.PERMISSION_GRANTED) {
- // 请求权限
- ActivityCompat.requestPermissions(this,
- new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
- PERMISSION_REQUEST_LOCATION);
- } else {
- // 权限已被授予,可以直接使用定位服务
- useLocationServices();
- }
- }
-
- @Override
- public void onRequestPermissionsResult(int requestCode, @NonNull @org.jetbrains.annotations.NotNull String[] permissions, @NonNull @org.jetbrains.annotations.NotNull int[] grantResults) {
- super.onRequestPermissionsResult(requestCode, permissions, grantResults);
- if (requestCode == PERMISSION_REQUEST_LOCATION) {
- if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
- // 权限被授予,可以使用定位服务
- useLocationServices();
- } else {
- // 权限被拒绝,显示提示信息
- showPermissionDeniedMessage();
- }
- }
- }
-
- private void useLocationServices() {
- // 使用定位服务的代码
- // init();
- }
-
- private void showPermissionDeniedMessage() {
- // 提示用户权限被拒绝的信息
- }
-
- 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 void initView() {
- btn_01 = (Button) findViewById(R.id.btn_01);
- tv_site = (TextView) findViewById(R.id.tv_site);
-
- btn_01.setOnClickListener(this);
- }
-
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.btn_01:
- init();
- break;
- }
- }
-
- private class MyAMapLocationListener implements AMapLocationListener {
-
- @Override
- public void onLocationChanged(AMapLocation aMapLocation) {
- if (aMapLocation != null) {
- if (aMapLocation.getErrorCode() == 0) {
- Log.e("位置:", aMapLocation.getAddress());
- tv_site.setText(aMapLocation.getAddress());
- } else {
- //定位失败时,可通过ErrCode(错误码)信息来确定失败的原因,errInfo是错误信息,详见错误码表。
- Log.e("AmapError", "location Error, ErrCode:"
- + aMapLocation.getErrorCode() + ", errInfo:"
- + aMapLocation.getErrorInfo());
- }
- }
- }
- }
- }