Android百度地图开发
前几天看着慕课学了一下百度地图,在这里做一下总结
加载包
申请秘钥(这个就不多说了,网上一大把);
注意:引入.so库时有两种方法
- 建立与java同级的JniLibs目录然后讲所有so库文件全部放进去就ok,不需要做其他工作(破坏目录结构不推荐)
- 将so库文件全部放入libs目录在gradle文件中加入
sourceSets{
main{
java.srcDirs=['src/main/java','src/main/java-gen']
jniLibs.srcDirs=['libs']
}
}
加载地图
mMapView = (MapView) findViewById(R.id.baimapView);
baiduMap = mMapView.getMap();
注意:mapView的生命周期要与Activity同步
定位相关
相关类
MapStatusUpdate :地图状态更新
- MapStatusUpdateFactory.zoomIn(float f):地图缩放
- MapStatusUpdateFactoryn.newLatLng(Lanlng lanlng):定位新坐标,lanlng包含经纬度
MyLocationClient:定位对象
- MyLocationOption:定位设置
- 生命周期与Map相关OnStart()调用client.start() , OnStop()调用client.stop()
- 注册监听器
- mLocationClient.registerLocationListener(mLocationListener); //注册监听器
mLocationListener = new MyLocationListener();
mLocationClient.registerLocationListener(mLocationListener); //注册监听器
//Location设置
mLocationClientOption = new LocationClientOption();
mLocationClientOption.setCoorType("bd09ll"); //设置坐标格式
mLocationClientOption.setIsNeedAddress(true); //设置是否需要获取地址
mLocationClientOption.setOpenGps(true); //设置打开GPS
mLocationClientOption.setScanSpan(1000); //设置请求时间间隔
mLocationClient.setLocOption(mLocationClientOption);
MyLocationListener代码
private class MyLocationListener implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
//通过回调获取定位信息
MyLocationData locationData = new MyLocationData.Builder()
.direction(mCurrentX) //此处设置开发者获取到的方向信息,顺时针0-360
.accuracy(location.getRadius()) //设置准确性,半径
.latitude(location.getLatitude()) //获取纬度
.longitude(location.getLongitude()) //获取经度
.build();
//添加方向覆盖物
/**
*LocationMode: 地图模式
*mIconLocation: 定位图标
*/
MyLocationConfiguration myLocationConfig = new MyLocationConfiguration(MyLocationConfiguration.LocationMode.NORMAL, true, mIconLocation);
baiduMap.setMyLocationConfigeration(myLocationConfig);
//向地图添加定位信息
baiduMap.setMyLocationData(locationData);
//获取经纬度
mlatitude = location.getLatitude();
mlongitude = location.getLongitude();
if (isFirstLocation) {
centerToMyLocation();
isFirstLocation = false;
Toast.makeText(context, "我的位置:" + location.getAddrStr(), Toast.LENGTH_SHORT).show();
}
}
}
centerToMyLocation()
private void centerToMyLocation() {
LatLng latLng = new LatLng(mlatitude, mlongitude); //获取当前经纬度并封装在latLng对象中
Log.d(TAG, "onReceiveLocation: latLng = " + latLng);
MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newLatLng(latLng); //地图状态更新
baiduMap.animateMapStatus(mapStatusUpdate); //跟踪到当前位置
}
这样就可以实现第一次登陆地图定位到当前的位置,并显示定位图标,但是没有方向传感器
方向传感器
mIconLocation = BitmapDescriptorFactory.fromResource(R.drawable.icon_openmap_focuse_mark);
myDirectionSensorListener = new MyDirectionSensorListener(this);
myDirectionSensorListener.setOrientation(new MyDirectionSensorListener.OnOrientationListener() {
@Override
public void onOrientationChanged(float x) {
mCurrentX = x;
}
});
MyDirectionSensorLister.java代码
package com.lqy.funnyday.model.map;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
/**
* Created by mrliu on 16-9-14.
*/
public class MyDirectionSensorListener implements SensorEventListener {
private Context context;
/**
* 传感器相关
*/
private SensorManager sensorManager;
private Sensor sensor;
/**
* 坐标相关
*/
private float lastX; //x轴数据
private OnOrientationListener orientation; //通过接口对象回调坐标轴数据
public MyDirectionSensorListener(Context context) {
this.context = context;
}
public void setOrientation(OnOrientationListener orientation) {
this.orientation = orientation;
}
/**
* 开启方向传感器
*/
public void start() {
sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
if (sensorManager != null) {
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
}
if (sensor != null) {
sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_UI);
}
}
/**
* 关闭方向传感器
*/
public void stop() {
sensorManager.unregisterListener(this);
}
@Override
public void onSensorChanged(SensorEvent event) {
//只获取X轴坐标
if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {
float x = event.values[SensorManager.DATA_X];
if (Math.abs(x - lastX) > 1.0) {
if (orientation != null) {
orientation.onOrientationChanged(x);
}
}
lastX = x;
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
//精度改变
}
/**
* 设置回调的接口返回x轴数据
*/
public interface OnOrientationListener {
void onOrientationChanged(float x);
}
}
注意:方向传感器声明周期和Activity相同
权限问题
在Android6.0定位和权限需要动态申请了,如过定位图标出现,但是周边地图没有出现,那很有可能你没有是设置权限