赞
踩
**Android Studio实现简单的百度地图定位**
项目前期准备可观看以下的视频回放:
代码实现:
DemoApplication.java
package com.example.lk.baidumapapplication; import android.app.Application; import com.baidu.mapapi.CoordType; import com.baidu.mapapi.SDKInitializer; public class DemoApplication extends Application { @Override public void onCreate() { super.onCreate(); SDKInitializer.initialize(this); SDKInitializer.setCoordType(CoordType.BD09LL); } }
MainActivity.java
package com.example.lk.baidumapapplication; import android.app.Activity; import android.os.Bundle; import com.baidu.location.BDAbstractLocationListener; import com.baidu.location.BDLocation; import com.baidu.location.LocationClient; import com.baidu.location.LocationClientOption; import com.baidu.mapapi.map.BaiduMap; import com.baidu.mapapi.map.MapView; import com.baidu.mapapi.map.MyLocationData; public class MainActivity extends Activity { private MapView mMapView = null; private BaiduMap mBaiduMap; private LocationClient mLocationClient; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mMapView = findViewById(R.id.bmapView); mBaiduMap = mMapView.getMap(); mBaiduMap.setMyLocationEnabled(true); mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL); mLocationClient = new LocationClient(getApplicationContext()); //通过LocationClientOption设置LocationClient相关参数 LocationClientOption option = new LocationClientOption(); option.setOpenGps(true); // 打开gps option.setCoorType("bd09ll"); // 设置坐标类型 option.setScanSpan(1000); //设置locationClientOption mLocationClient.setLocOption(option); //注册LocationListener监听器 MyLocationListener myLocationListener = new MyLocationListener(); mLocationClient.registerLocationListener(myLocationListener); //开启地图定位图层 mLocationClient.start(); } public class MyLocationListener extends BDAbstractLocationListener { @Override public void onReceiveLocation(BDLocation bdLocation) { //mapView 销毁后不在处理新接收的位置 if (bdLocation == null || mMapView == null){ return; } MyLocationData locData = new MyLocationData.Builder() .accuracy(bdLocation.getRadius()) // 此处设置开发者获取到的方向信息,顺时针0-360 .direction(bdLocation.getDirection()).latitude(bdLocation.getLatitude()) .longitude(bdLocation.getLongitude()).build(); mBaiduMap.setMyLocationData(locData); } } @Override protected void onResume() { super.onResume(); mMapView.onResume(); } @Override protected void onPause() { super.onPause(); mMapView.onPause(); } @Override protected void onDestroy() { mLocationClient.stop(); mBaiduMap.setMyLocationEnabled(false); mMapView.onDestroy(); mMapView = null; super.onDestroy(); } }
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.lk.baidumapapplication"> <application android:name=".DemoApplication" 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"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <meta-data android:name="com.baidu.lbsapi.API_KEY" android:value="ocub75Kt1EnqSqGwEtyToMxjsG4GW8fG" /> <service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote"/> </application> <!-- 访问网络,进行地图相关业务数据请求,包括地图数据,路线规划,POI检索等 --> <uses-permission android:name="android.permission.INTERNET" /> <!-- 获取网络状态,根据网络状态切换进行数据请求网络转换 --> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!-- 读取外置存储。如果开发者使用了so动态加载功能并且把so文件放在了外置存储区域,则需要申请该权限,否则不需要 --> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <!-- 写外置存储。如果开发者使用了离线地图,并且数据写在外置存储区域,则需要申请该权限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- 这个权限用于进行网络定位 --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <!-- 这个权限用于访问GPS定位 --> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> </manifest>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。