Android开发之Android定位与百度位置应用开发

Android开发之Android定位与百度位置应用开发,第1张

概述首先注册和获取密钥,开发者在使用SDK之前需要获取百度地图移动版开发密(AK),该AK与百度账户相关联,地图初始化时需要使用AK。AndroidStudio配置在百度地图官方网站中下载Android地图SDK,并导入项目中使用。配置AndroidManifest.xml文件<?xmlversion="1.0"encoding="utf-8"?><m

首先注册和获取密钥,开发者在使用SDK之前需要获取百度地图移动版开发密(AK),该AK与百度账户相关联,地图初始化时需要使用AK。AndroID Studio配置在百度地图官方网站中下载AndroID地图SDK,并导入项目中使用。配置AndroIDManifest.xml文件

<?xml version="1.0" enCoding="utf-8"?><manifest xmlns:androID="http://schemas.androID.com/apk/res/androID"    package="com.example.mybaIDulcoate">    <!--         The ACCESS_COARSE/FINE_LOCATION permissions are not required to use         Google Maps AndroID API v2, but you must specify either coarse or fine         location permissions for the 'MyLocation' functionality.    -->    <!-- 这个权限用于进行网络定位-->    <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卡读取权限,用户写入离线定位数据-->    <application             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">        <!--             The API key for Google Maps-based APIs is defined as a string resource.             (See the file "res/values/Google_maps_API.xml").             Note that the API key is linked to the encryption key used to sign the APK.             You need a different API key for each encryption key, including the release key that is used to             sign the APK for publishing.             You can define the keys for the deBUG and release targets in src/deBUG/ and src/release/.        -->        <Meta-data            androID:name="com.baIDu.lbsAPI.API_KEY"            androID:value="1e9jt4o5grtzivBwkUfcmePi59y6zehS">        </Meta-data>        <service            androID:name="com.baIDu.location.f"            androID:enabled="true"            androID:process=":remote"></service>        <activity            androID:name=".MainActivity"          >            <intent-filter>                <action androID:name="androID.intent.action.MAIN" />                <category androID:name="androID.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

使用SDK各组件之前初始化context信息,传入ApplicationContext创建地图Activity,管理MapVIEw生命周期判断应用是否具有定位权限。如果没有,则动态申请;如果有,则发起定位等等下面是MainActivity

MainActivity

public class MainActivity extends AppCompatActivity implements VIEw.OnClickListener {    private MapVIEw mMapVIEw;    private BaIDuMap mBaIDuMap;    public LocationClIEnt mLocationClIEnt;    public BDLocationListener myListener = new MyLocationListener();    private button bt;    private button button;    private button buttons;    private LatLng latLng;    private boolean isFirstLoc = true; // 是否首次定位    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        SDKInitializer.initialize(getApplicationContext());        setContentVIEw(R.layout.activity_main);        initVIEw();        initMap();    }    private voID initMap() {        //获取地图控件引用        mBaIDuMap = mMapVIEw.getMap();        //普通地图        mBaIDuMap.setMapType(BaIDuMap.MAP_TYPE_norMAL);        mBaIDuMap.setMyLocationEnabled(true);        //默认显示普通地图        mBaIDuMap.setMapType(BaIDuMap.MAP_TYPE_norMAL);        // 开启定位图层        mBaIDuMap.setMyLocationEnabled(true);        mLocationClIEnt = new LocationClIEnt(getApplicationContext());     //声明LocationClIEnt类        //配置定位SDK参数        initLocation();        mLocationClIEnt.registerLocationListener(myListener);    //注册监听函数        //开启定位        mLocationClIEnt.start();        //图片点击事件,回到定位点        mLocationClIEnt.requestLocation();    }    //配置定位SDK参数    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);        option.setopenGps(true); // 打开gps        //可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死        option.SetIgnoreCacheException(false);//可选,默认false,设置是否收集CRASH信息,默认收集        option.setEnableSimulateGps(false);//可选,默认false,设置是否需要过滤GPS仿真结果,默认需要        mLocationClIEnt.setLocoption(option);    }    //实现BDLocationListener接口,BDLocationListener为结果监听接口,异步获取定位结果    public class MyLocationListener implements BDLocationListener {        @OverrIDe        public voID onReceiveLocation(BDLocation location) {            latLng = new LatLng(location.getLatitude(),location.getLongitude());            // 构造定位数据            MyLocationData locdata = new MyLocationData.Builder()                    .accuracy(location.geTradius())                    // 此处设置开发者获取到的方向信息,顺时针0-360                    .direction(100).latitude(location.getLatitude())                    .longitude(location.getLongitude()).build();            // 设置定位数据            mBaIDuMap.setMyLocationData(locdata);            // 当不需要定位图层时关闭定位图层            //mBaIDuMap.setMyLocationEnabled(false);            if (isFirstLoc) {                isFirstLoc = false;                LatLng ll = new LatLng(location.getLatitude(),                        location.getLongitude());                MapStatus.Builder builder = new MapStatus.Builder();                builder.target(ll).zoom(18.0f);                mBaIDuMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));                if (location.getLocType() == BDLocation.TypeGpsLocation) {                    // GPS定位结果                    Toast.makeText(MainActivity.this, location.getAddrstr(), Toast.LENGTH_SHORT).show();                } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {                    // 网络定位结果                    Toast.makeText(MainActivity.this, location.getAddrstr(), Toast.LENGTH_SHORT).show();                } else if (location.getLocType() == BDLocation.TypeOfflineLocation) {                    // 离线定位结果                    Toast.makeText(MainActivity.this, location.getAddrstr(), Toast.LENGTH_SHORT).show();                } else if (location.getLocType() == BDLocation.TypeServerError) {                    Toast.makeText(MainActivity.this, "服务器错误,请检查", Toast.LENGTH_SHORT).show();                } else if (location.getLocType() == BDLocation.TypeNetWorkException) {                    Toast.makeText(MainActivity.this, "网络错误,请检查", Toast.LENGTH_SHORT).show();                } else if (location.getLocType() == BDLocation.TypeCriteriaException) {                    Toast.makeText(MainActivity.this, "手机模式错误,请检查是否飞行", Toast.LENGTH_SHORT).show();                }            }        }    }    private voID initVIEw() {        mMapVIEw = (MapVIEw) findVIEwByID(R.ID.bmapVIEw);        bt = (button) findVIEwByID(R.ID.bt);        bt.setonClickListener(this);        button = (button) findVIEwByID(R.ID.button);        button.setonClickListener(this);        buttons = (button) findVIEwByID(R.ID.buttons);        buttons.setonClickListener(this);    }    @OverrIDe    protected voID onDestroy() {        super.onDestroy();        //在activity执行onDestroy时执行mMapVIEw.onDestroy(),实现地图生命周期管理        mMapVIEw.onDestroy();    }    @OverrIDe    protected voID onResume() {        super.onResume();        //在activity执行onResume时执行mMapVIEw. onResume (),实现地图生命周期管理        mMapVIEw.onResume();    }    @OverrIDe    protected voID onPause() {        super.onPause();        //在activity执行onPause时执行mMapVIEw. onPause (),实现地图生命周期管理        mMapVIEw.onPause();    }    @OverrIDe    public voID onClick(VIEw v) {        switch (v.getID()) {            case R.ID.bt:                //把定位点再次显现出来                MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newLatLng(latLng);                mBaIDuMap.animateMapStatus(mapStatusUpdate);                break;            case R.ID.button:                //卫星地图                mBaIDuMap.setMapType(BaIDuMap.MAP_TYPE_SATELliTE);                break;            case R.ID.buttons:                //普通地图                mBaIDuMap.setMapType(BaIDuMap.MAP_TYPE_norMAL);                break;        }    }}

运行界面展示:


码云:https://gitee.com/yan-xinruikexin/MybaiduLcoate

总结

以上是内存溢出为你收集整理的Android开发之Android定位与百度位置应用开发全部内容,希望文章能够帮你解决Android开发之Android定位与百度位置应用开发所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1055435.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存