高德地图实现简单的定位(内含动态权限)

  1. 新建应用:https://lbs.amap.com/dev/key/app
  2. 如下图,双击signingReport,查看控制台SHA1值

高德地图实现简单的定位(内含动态权限)_第1张图片

  1. 引入定位依赖库:compile 'com.amap.api:location:4.4.0

  2. 集成代码,点开网址,找到第五步进行拷贝:https://lbs.amap.com/api/android-location-sdk/gettingstarted

  3. MainActvity

package com.qh.***.gaodemap;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

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

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private AMapLocationClient mapLocationClient;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取显示位置控件
        final TextView textViewa = findViewById(R.id.txt_map);
        //获取点击获取位置控件
        Button buttona = findViewById(R.id.btn_map);
        //点击获取位置控件——事件监听
        buttona.setOnClickListener(this);
        //声明AMapLocationClient类对象
        //初始化定位
        mapLocationClient = new AMapLocationClient(getApplicationContext());

        //异步获取定位结果
        AMapLocationListener mAMapLocationListener = new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation amapLocation) {
                if (amapLocation != null) {
                    if (amapLocation.getErrorCode() == 0) {
                        //解析定位结果
                        textViewa.setText(amapLocation.getAddress());
                        mapLocationClient.stopLocation();
                    }
                }
            }
        };

        //设置定位回调监听
        mapLocationClient.setLocationListener(mAMapLocationListener);

        //动态权限
        String[] p = new String[]{Manifest.permission.ACCESS_COARSE_LOCATION};
        ActivityCompat.requestPermissions(MainActivity.this, p, 1);


    }

    //点击获取位置控件
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_map) {
            //启动定位
            mapLocationClient.startLocation();
        }
    }

    //请求允许的结果
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (grantResults.length > 0) {
            int result = grantResults[0];
            if (result == PackageManager.PERMISSION_GRANTED) {
                //权限同意
                mapLocationClient.startLocation();
            } else {
                //权限拒绝
                Toast.makeText(MainActivity.this, "请同意定位权限", Toast.LENGTH_SHORT).show();
            }
        }
    }


}


你可能感兴趣的:(android)