BaiduMap百度地图使用攻略

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/woainijinying/article/details/73556311

百度地图开放平台攻略
今天做了做定位功能,大意失荆州呀,本来顺顺利利的因为一个不小心,少粘了一段话,定位不成功,好烦,不过也不能怪我,教程这种东西,官方教程写的那么啰嗦,又复杂,索性自己整理个简易版本哒。
第一步 申请AK
http://lbsyun.baidu.com/index.php?title=android-locsdk/guide/key
官方的写的比较清晰:不懂可以查看,以下是我的小攻略:
主要是两个 : 一个包名、一个SHA1。
包名:
文件build.gradle中查询 applictionId
SHA1
1. 打开cmd
2. 输入:cd .android
3. 输入:keytool -list -v -keystore debug.keystore
4. 输入密钥口令: android
就会看到SHA1
如果cmd不能复制
在cmd中点击红框的位置

在红框中的位置,点上钩就好
这里写图片描述
第二步 配置环境Android定位SDK
导入jia包:
app:lib中加入工具包
main:新增jniLibs文件夹,armeabi中加入os包

  1. build.gradle:
<!-- 这个权限用于进行网络定位-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<!-- 这个权限用于访问GPS定位-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<!-- 获取运营商信息,用于支持提供运营商信息相关的接口-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<!-- 这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位-->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<!-- 用于读取手机当前的状态-->
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<!-- 写入扩展存储,向扩展卡写入数据,用于写入离线定位数据-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<!-- 访问网络,网络定位需要上网-->
<uses-permission android:name="android.permission.INTERNET" />
<!-- SD卡读取权限,用户写入离线定位数据-->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>

<application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }
 </application>
  1. MyApplication中添加:
public LocationClient mLocationClient = null;
public BDLocationListener myListener = new MyLocationListener(this);

  @Override
    public void onCreate() {
        super.onCreate();
        exceptionLogOut();
        mLocationClient = new LocationClient(getApplicationContext());
        //声明LocationClient类
        mLocationClient.registerLocationListener( myListener );
        //注册监听函数
        initLocation();
        mLocationClient.start();

    }
    private void initLocation(){
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        //可选,默认高精度,设置定位模式,高精度,低功耗,仅设备

        option.setCoorType("bd09ll");
        //可选,默认gcj02,设置返回的定位结果坐标系

        int span=1000;
        option.setScanSpan(span);
        //可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的

        option.setIsNeedAddress(true);
        //可选,设置是否需要地址信息,默认不需要

        option.setOpenGps(true);
        //可选,默认false,设置是否使用gps

        option.setLocationNotify(true);
        //可选,默认false,设置是否当GPS有效时按照1S/1次频率输出GPS结果

        option.setIsNeedLocationDescribe(true);
        //可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近”

        option.setIsNeedLocationPoiList(true);
        //可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到

        option.setIgnoreKillProcess(false);
        //可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死

        option.SetIgnoreCacheException(false);
        //可选,默认false,设置是否收集CRASH信息,默认收集

        option.setEnableSimulateGps(false);
        //可选,默认false,设置是否需要过滤GPS仿真结果,默认需要

        mLocationClient.setLocOption(option);
    }

书写MyLocationListener .class 百度有现成的代码,
为了开发方便我在现成的基础上进行了一些修改。

public class MyLocationListener implements BDLocationListener {

    private Context context;

    public MyLocationListener(Context context) {
        this.context = context;
    }

    public static double latitude;//经度
    public static double longitude;//纬度
    public static String addr;//常量

    public static String city;//城市

    public static String county;//县街道
    public static String street;//县街道
    public static String LocationDescribe;
    public static BDLocation location;

    @Override
    public void onReceiveLocation(BDLocation location) {
        this.location=location;
        MyLocationListener.latitude=location.getLatitude();
        MyLocationListener.longitude=location.getLongitude();
        MyLocationListener.LocationDescribe=location.getLocationDescribe();

        MyLocationListener.addr=location.getCity()+location.getDistrict()+location.getStreet()+location.getStreetNumber();
        if(MyLocationListener.addr.contains("null")){
            MyLocationListener.addr = "";
        }
        MyLocationListener.city=location.getProvince()+location.getCity();
        MyLocationListener.county=location.getDistrict();
        MyLocationListener.street=location.getStreet()+location.getStreetNumber();
        //Receive Location
        StringBuffer sb = new StringBuffer(256);
        sb.append("time : ");
        sb.append(location.getTime());
        sb.append("\nerror code : ");
        sb.append(location.getLocType());
        sb.append("\nlatitude : ");
        sb.append(location.getLatitude());

        sb.append("\nlontitude : ");
        sb.append(location.getLongitude());


        sb.append("\nradius : ");
        sb.append(location.getRadius());
        if (location.getLocType() == BDLocation.TypeGpsLocation){// GPS定位结果
            sb.append("\nspeed : ");
            sb.append(location.getSpeed());// 单位:公里每小时
            sb.append("\nsatellite : ");
            sb.append(location.getSatelliteNumber());
            sb.append("\nheight : ");
            sb.append(location.getAltitude());// 单位:米
            sb.append("\ndirection : ");
            sb.append(location.getDirection());// 单位度
            sb.append("\naddr : ");
            sb.append(location.getAddrStr());
            sb.append("\ndescribe : ");
            sb.append("gps定位成功");

        } else if (location.getLocType() == BDLocation.TypeNetWorkLocation){// 网络定位结果
            sb.append("\naddr : ");
            sb.append(location.getAddrStr());
            //运营商信息
            sb.append("\noperationers : ");
            sb.append(location.getOperators());
            sb.append("\ndescribe : ");
            sb.append("网络定位成功");
        } else if (location.getLocType() == BDLocation.TypeOffLineLocation) {// 离线定位结果
            sb.append("\ndescribe : ");
            sb.append("离线定位成功,离线定位结果也是有效的");
        } else if (location.getLocType() == BDLocation.TypeServerError) {
            sb.append("\ndescribe : ");
            sb.append("服务端网络定位失败,可以反馈IMEI号和大体定位时间到[email protected],会有人追查原因");
        } else if (location.getLocType() == BDLocation.TypeNetWorkException) {
            sb.append("\ndescribe : ");
            sb.append("网络不同导致定位失败,请检查网络是否通畅");
        } else if (location.getLocType() == BDLocation.TypeCriteriaException) {
            sb.append("\ndescribe : ");
            sb.append("无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机");
        }
        sb.append("\nlocationdescribe : ");
        sb.append(location.getLocationDescribe());// 位置语义化信息
        List<Poi> list = location.getPoiList();// POI数据
        if (list != null) {
            sb.append("\npoilist size = : ");
            sb.append(list.size());
            for (Poi p : list) {
                sb.append("\npoi= : ");
                sb.append(p.getId() + " " + p.getName() + " " + p.getRank());
            }
        }
        if(loacationCall!=null){
            loacationCall.success();
        }
        ((MyApplication)MyApplication.getContext()).mLocationClient.stop();
    }


    public static interface LoacationCall{
        public void success();
    }
    private LoacationCall loacationCall;

    /**
     * 开始定位
     * @param loacationCall
     */
    public  void startLoacation(LoacationCall loacationCall){
        this.loacationCall =loacationCall;
        ((MyApplication)MyApplication.getContext()).mLocationClient.start();
    }
}

直接在Activity中引用就好:例子:

MyLocationListener.addr//地址全址

猜你喜欢

转载自blog.csdn.net/woainijinying/article/details/73556311