注意:我是用的定位+基础地图下载的BaiduLBS_Android.jar
清单文件相应配置
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.maptest">
<!--定位的相关权限-->
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<!-- 这个权限用于进行网络定位-->
<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:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<!--百度key-->
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="您的地图key"/>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!--注册定位服务-->
<service
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote">
</service>
</application>
</manifest>
MainActivity方法(只是一个MapView就不放xml了)
package com.maptest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MyLocationConfiguration.LocationMode;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
public class MainActivity extends AppCompatActivity {
private MapView mapView;
//初始化定位类
private LocationClient mLocationClient = null;
private BDLocationListener myListener = null;
private LocationMode mLocationMode;
private BaiduMap mBaidumap;
//是否第一次定位
private boolean isFirstIn = true;
//定位自己图标
private BitmapDescriptor mIconLocation;
//位置方向偏移量
private float mCurrentX;
//位置传感器
private MyOrientationListener myOrientationListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
mapView = (MapView) findViewById(R.id.bmapView);
mBaidumap = mapView.getMap();
initLocation();
}
private void initLocation() {
mLocationMode = LocationMode.NORMAL;
mLocationClient = new LocationClient(getApplicationContext());
myListener = new MyLocationListener();
mLocationClient.registerLocationListener(myListener);
LocationClientOption option = new LocationClientOption();
option.setCoorType("bd09ll"); //坐标类型
option.setIsNeedAddress(true); //返回当前位置
option.setOpenGps(true);
option.setScanSpan(5000); //多少秒一次请求
mLocationClient.setLocOption(option); //加载option
//初始化自己位置的图标
mIconLocation = BitmapDescriptorFactory.fromResource(R.drawable.navi_map_gps_locked);
//初始化方向传感器
myOrientationListener = new MyOrientationListener(this);
myOrientationListener.setOnOrientationListener(new MyOrientationListener.OnOrientationListener() {
@Override
public void onOrientationChanged(float x) {
//更新当前位置方向
mCurrentX = x;
}
});
}
@Override
protected void onStart() {
super.onStart();
mBaidumap.setMyLocationEnabled(true); //开启定位的允许
if (!mLocationClient.isStarted()) //判断是否启动
{
//开启定位
mLocationClient.start();
}
// 开启方向传感器
myOrientationListener.start();
}
public class MyLocationListener implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
if (location.getLocType() == BDLocation.TypeOffLineLocationFail
|| location.getLocType() == BDLocation.TypeOffLineLocationNetworkFail
|| location.getLocType() == BDLocation.TypeCriteriaException
|| location.getLocType() == BDLocation.TypeNetWorkException || location.getLocType() == BDLocation.TypeServerError) {
Toast.makeText(MainActivity.this, "获取定位失败", Toast.LENGTH_SHORT).show();
} else {
// 构造定位数据
MyLocationData locData = new MyLocationData.Builder()
.direction(mCurrentX)// 方向
.accuracy(location.getRadius())//精度
.latitude(location.getLatitude())//
.longitude(location.getLongitude())//
.build(); //build模式
// 设置定位数据
mBaidumap.setMyLocationData(locData);
// 设置定位图层的配置(定位模式,是否允许方向信息,用户自定义定位图标)
MyLocationConfiguration config = new MyLocationConfiguration(mLocationMode, true, mIconLocation);
mBaidumap.setMyLocationConfigeration(config);
//第一次定位时更新地图中心点
if (isFirstIn) {
//定位到自己所在的位置
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);
mBaidumap.setMapStatus(msu);
isFirstIn = false;
}
}
}
@Override
public void onConnectHotSpotMessage(String s, int i) {
}
}
@Override
protected void onResume() {
mapView.onResume();
super.onResume();
}
@Override
protected void onDestroy() {
mLocationClient.stop();
mapView.onDestroy();
super.onDestroy();
}
@Override
protected void onPause() {
mapView.onPause();
super.onPause();
}
}
方向传感器
package com.maptest;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
public class MyOrientationListener implements SensorEventListener {
private SensorManager mSensorManager;
private Context mContext;
private Sensor mSensor;
private float lastX;
public MyOrientationListener(Context context) {
this.mContext = context;
}
@SuppressWarnings("deprecation")
public void start() //开始监听
{
mSensorManager = (SensorManager) mContext
.getSystemService(Context.SENSOR_SERVICE);
if (mSensorManager != null) {
// 获得方向传感器
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
}
//注册方向传感器广播
if (mSensor != null) {
mSensorManager.registerListener(this, mSensor,
SensorManager.SENSOR_DELAY_UI);
}
}
public void stop() //结束监听
{
mSensorManager.unregisterListener(this);
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
@SuppressWarnings(
{"deprecation"})
//方向发生改变
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {
float x = event.values[SensorManager.DATA_X];
if (Math.abs(x - lastX) > 1.0) //角度大于1度
{
if (mOnOrientationListener != null) {
mOnOrientationListener.onOrientationChanged(x);
}
}
lastX = x;
}
}
private OnOrientationListener mOnOrientationListener;
public void setOnOrientationListener(
OnOrientationListener mOnOrientationListener) {
this.mOnOrientationListener = mOnOrientationListener;
}
//跑出接口
public interface OnOrientationListener {
void onOrientationChanged(float x);
}
}