通过netty把百度地图API获取的地理位置从Android端发送到Java服务器端

本篇记录我在实现时的思考过程,写给之后可能遇到困难的我自己也给到需要帮助的人 。写的比较浅显,见谅 。
在写项目代码的时候,需要把Android端的位置信息传输到服务器端,通过Netty达到连续传输的效果,如下:

通过netty把百度地图API获取的地理位置从Android端发送到Java服务器端

文章插图
我们可以先来看看百度地图官方给出的相关代码
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;@Overrideprotected 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); // 打开gpsoption.setCoorType("bd09ll"); // 设置坐标类型option.setScanSpan(1000);// 可选,设置地址信息option.setIsNeedAddress(true);//可选,设置是否需要地址描述option.setIsNeedLocationDescribe(true);//设置locationClientOptionmLocationClient.setLocOption(option);//注册LocationListener监听器MyLocationListene myLocationListener = new MyLocationListene();mLocationClient.registerLocationListener(myLocationListener);//开启地图定位图层mLocationClient.start();}public class MyLocationListene extends BDAbstractLocationListener {@Overridepublic 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 = https://www.huyubaike.com/biancheng/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纬度:"+ location.getLongitude());stringBuilder.append("\n状态码:"+ location.getLocType());stringBuilder.append("\n国家:" + location.getCountry());stringBuilder.append("\n城市:"+ location.getCity());stringBuilder.append("\n区:" + location.getDistrict());stringBuilder.append("\n街道:" + location.getStreet());stringBuilder.append("\n地址:" + location.getAddrStr());mtextView.setText(stringBuilder.toString());}}}

经验总结扩展阅读