Android集成百度定位SDK实现获取定位地址

应用场景:实现APP的网络定位功能

1.集成SDK

去百度创建应用,获取到AK,下载定位SDK,把里面的jar包添加到libs下。其他os文件就放在main下新建的jniLilbs里,如图:

Android集成百度定位SDK实现获取定位地址_第1张图片

2.代码贴出

package com.jin.baidudemo;

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

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity {

    public LocationClient mLocationClient = null;
    private MyLocationListener myListener = new MyLocationListener();

    TextView tv_location;

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

        tv_location = (TextView) findViewById(R.id.tv_location);

        //声明LocationClient类
        mLocationClient = new LocationClient(getApplicationContext());

        //注册监听函数
        mLocationClient.registerLocationListener(myListener);

        init();  // 初始化配置

        //存放权限的集合
        List permissionList=new ArrayList<>();
        //判断是否有权限
        if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED){
            permissionList.add(Manifest.permission.ACCESS_FINE_LOCATION);
        }
        if (ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.READ_PHONE_STATE)!=PackageManager.PERMISSION_GRANTED){
            permissionList.add(Manifest.permission.READ_PHONE_STATE);
        }
        if (ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.WRITE_EXTERNAL_STORAGE)!=PackageManager.PERMISSION_GRANTED){
            permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        }
        if (!permissionList.isEmpty()){
            String[] permission = permissionList.toArray(new String[permissionList.size()]);
            ActivityCompat.requestPermissions(MainActivity.this,permission,1);
        }else {
            request();
        }
    }

    /**
     * 初始化配置
     */
    private void init() {
        LocationClientOption option = new LocationClientOption();
        option.setIsNeedAddress(true);
        mLocationClient.setLocOption(option);
    }

    /**
     * 开始请求
     */
    public void request(){
        mLocationClient.start();
    }

    /**
     * 百度定位的响应接口
     */
    public class MyLocationListener extends BDAbstractLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {

            String addr = location.getAddrStr();    //获取详细地址信息
            String country = location.getCountry();    //获取国家
            String province = location.getProvince();    //获取省份
            String city = location.getCity();    //获取城市
            String district = location.getDistrict();    //获取区县
            String street = location.getStreet();    //获取街道信息
            tv_location.setText("address==" + addr + "       国家=" + country + "    省=" + province + "  市=" + city + "    区=" + district + "    街道=" + street);
        }
    }

    /**
     * 获取动态权限的回调
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode){
            case 1:
                if (grantResults.length>0){
                    for (int results : grantResults){
                        if (results!=PackageManager.PERMISSION_GRANTED){
                            Toast.makeText(MainActivity.this,"必须打开权限才能使用!",Toast.LENGTH_SHORT).show();
                            finish();
                            return;
                        }
                    }
                    request();
                }else {
                    Toast.makeText(MainActivity.this,"未知错误!!",Toast.LENGTH_SHORT).show();
                    finish();
                }
                break;
                default:break;
        }
    }
}

3.权限




    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    
        
        

        
            
                

                
            
        

         

    

4.效果图:

Android集成百度定位SDK实现获取定位地址_第2张图片

你可能感兴趣的:(Android)