本篇记录我在实现时的思考过程,写给之后可能遇到困难的我自己也给到需要帮助的人。
写的比较浅显,见谅。
在写项目代码的时候,需要把Android端的位置信息传输到服务器端,通过Netty达到连续传输的效果,如下:
我们可以先来看看百度地图官方给出的相关代码
public class MainActivity extends AppCompatActivity {
private MapView mMapView = null;
private BaiduMap mBaiduMap = null;
private LocationClient mLocationClient = null;
private TextView mtextView;
// 是否是第一次定位
private boolean isFirstLocate = true;
// 当前定位模式
private MyLocationConfiguration.LocationMode locationMode;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LocationClient.setAgreePrivacy(true);
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
mMapView = findViewById(R.id.bmapView);
mtextView = findViewById(R.id.text_tishi);
//开启交通图
mBaiduMap = mMapView.getMap();
mBaiduMap.setTrafficEnabled(true);
//开启地图的定位图层
mBaiduMap.setMyLocationEnabled(true);
// BaiduMapOptions options = new BaiduMapOptions();
// options.mapType(BaiduMap.MAP_TYPE_SATELLITE);
// MapView mapView = new MapView(this, options);
// setContentView(mapView);卫星地图view显示
//定位初始化
LocationClient mLocationClient = null;
try {
mLocationClient = new LocationClient(MainActivity.this);
} catch (Exception e) {
e.printStackTrace();
}
//通过LocationClientOption设置LocationClient相关参数
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true); // 打开gps
option.setCoorType("bd09ll"); // 设置坐标类型
option.setScanSpan(1000);
// 可选,设置地址信息
option.setIsNeedAddress(true);
//可选,设置是否需要地址描述
option.setIsNeedLocationDescribe(true);
//设置locationClientOption
mLocationClient.setLocOption(option);
//注册LocationListener监听器
MyLocationListene myLocationListener = new MyLocationListene();
mLocationClient.registerLocationListener(myLocationListener);
//开启地图定位图层
mLocationClient.start();
}
public class MyLocationListene extends BDAbstractLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
//mapView 销毁后不在处理新接收的位置
if (location == null || mMapView == null) {
return;
}
LatLng ll = new LatLng(location.getLatitude(), location.getLongitude());
if (isFirstLocate) {
isFirstLocate = false;
//给地图设置状态
mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newLatLng(ll));
}
MyLocationData locData = new MyLocationData.Builder()
.accuracy(location.getRadius())
// 此处设置开发者获取到的方向信息,顺时针0-360
.direction(location.getDirection()).latitude(location.getLatitude())
.longitude(location.getLongitude()).build();
mBaiduMap.setMyLocationData(locData);
// 更换定位图标,这里的图片是放在 drawble 文件下的
BitmapDescriptor mCurrentMarker = BitmapDescriptorFactory.fromResource(R.drawable.icon_gcoding);
// 定位模式 地图SDK支持三种定位模式:NORMAL(普通态), FOLLOWING(跟随态), COMPASS(罗盘态)
locationMode = MyLocationConfiguration.LocationMode.NORMAL;
// 定位模式、是否开启方向、设置自定义定位图标、精度圈填充颜色以及精度圈边框颜色5个属性(此处只设置了前三个)。
MyLocationConfiguration mLocationConfiguration = new MyLocationConfiguration(locationMode,true,mCurrentMarker);
// 使自定义的配置生效
mBaiduMap.setMyLocationConfiguration(mLocationConfiguration);
// 显示当前信息
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("\n经度:" + location.getLatitude());
stringBuilder.append("\n纬度:&q