public class MapActivity extends AppCompatActivity {
private MapView bmapView;
private BaiduMap mMap;
private LocationClient mLocationClient;
private boolean isFirstLoc = true;
public MyLocationListenner mListenner = new MyLocationListenner();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_map);
initView();
mMap = bmapView.getMap();
mMap.setMyLocationEnabled(true);
mLocationClient = new LocationClient(this);
mLocationClient.registerLocationListener(mListenner);
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true); // 打开gps
option.setCoorType("bd09ll"); // 设置坐标类型
option.setScanSpan(1000);
mLocationClient.setLocOption(option);
mLocationClient.start();
}
public class MyLocationListenner implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
// map view 销毁后不在处理新接收的位置
if (location == null || bmapView == null) {
return;
}
MyLocationData locData = new MyLocationData.Builder()
.accuracy(location.getRadius())
// 此处设置开发者获取到的方向信息,顺时针0-360
.direction(100).latitude(location.getLatitude())
.longitude(location.getLongitude()).build();
mMap.setMyLocationData(locData);
if (isFirstLoc) {
isFirstLoc = false;
LatLng ll = new LatLng(location.getLatitude(),
location.getLongitude());
MapStatus.Builder builder = new MapStatus.Builder();
builder.target(ll).zoom(18.0f);
mMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
}
}
public void onReceivePoi(BDLocation poiLocation) {
}
}
@Override
protected void onDestroy() {
super.onDestroy();
bmapView.onDestroy();
}
@Override
protected void onResume() {
super.onResume();
bmapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
bmapView.onPause();
}
private void initView() {
bmapView = findViewById(R.id.bmapView);
}
}
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
//在使用SDK各组件之前初始化context信息,传入ApplicationContext
SDKInitializer.initialize(this);
//自4.3.0起,百度地图SDK所有接口均支持百度坐标和国测局坐标,用此方法设置您使用的坐标类型.
//包括BD09LL和GCJ02两种坐标,默认是BD09LL坐标。
SDKInitializer.setCoordType(CoordType.BD09LL);
}
}
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
//获取设备网络状态,禁用后无法获取网络状态
<uses-permission android:name="android.permission.INTERNET" />
//网络权限,当禁用后,无法进行检索等相关业务
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
//读取设备硬件信息,统计数据
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
//读取系统信息,包含系统版本等信息,用作统计
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
//获取设备的网络状态,鉴权所需网络代理
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
//允许sd卡写权限,需写入地图数据,禁用后无法显示地图
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
//获取统计数据
<uses-permission android:name="android.permission.CAMERA" />
//使用步行AR导航,配置Camera权限
<!-- 这个权限用于进行网络定位-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<!-- 这个权限用于访问GPS定位-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<!-- 这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位-->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<!-- 写入扩展存储,向扩展卡写入数据,用于写入离线定位数据-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!-- SD卡读取权限,用户写入离线定位数据-->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="1y2YHb6XeaoiNgZGpl3VRVoiOKndRDXs" />
<service
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote">
</service>