通過netty把百度地圖API獲取的地理位置從Android端發(fā)送到Java服務器端的操作方法
本篇記錄我在實現(xiàn)時的思考過程,寫給之后可能遇到困難的我自己也給到需要幫助的人。
寫的比較淺顯,見諒。
在寫項目代碼的時候,需要把Android端的位置信息傳輸?shù)椒掌鞫?,通過Netty達到連續(xù)傳輸?shù)男Ч缦拢?/p>

我們可以先來看看百度地圖官方給出的相關代碼
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);衛(wèi)星地圖view顯示
//定位初始化
LocationClient mLocationClient = null;
try {
mLocationClient = new LocationClient(MainActivity.this);
} catch (Exception e) {
e.printStackTrace();
}
//通過LocationClientOption設置LocationClient相關參數(shù)
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監(jiān)聽器
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;
//給地圖設置狀態(tài)
mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newLatLng(ll));
}
MyLocationData locData = new MyLocationData.Builder()
.accuracy(location.getRadius())
// 此處設置開發(fā)者獲取到的方向信息,順時針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(普通態(tài)), FOLLOWING(跟隨態(tài)), COMPASS(羅盤態(tài))
locationMode = MyLocationConfiguration.LocationMode.NORMAL;
// 定位模式、是否開啟方向、設置自定義定位圖標、精度圈填充顏色以及精度圈邊框顏色5個屬性(此處只設置了前三個)。
MyLocationConfiguration mLocationConfiguration = new MyLocationConfiguration(locationMode,true,mCurrentMarker);
// 使自定義的配置生效
mBaiduMap.setMyLocationConfiguration(mLocationConfiguration);
// 顯示當前信息
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("\n經(jīng)度:" + location.getLatitude());
stringBuilder.append("\n緯度:"+ location.getLongitude());
stringBuilder.append("\n狀態(tài)碼:"+ location.getLocType());
stringBuilder.append("\n國家:" + location.getCountry());
stringBuilder.append("\n城市:"+ location.getCity());
stringBuilder.append("\n區(qū):" + location.getDistrict());
stringBuilder.append("\n街道:" + location.getStreet());
stringBuilder.append("\n地址:" + location.getAddrStr());
mtextView.setText(stringBuilder.toString());
}
}
}
使用者需要創(chuàng)建一個LocationClient對象,為LocationClient配置Option、注冊監(jiān)聽器(BDAbstractLocationListener)來獲取位置信息,監(jiān)聽器得到的BDLocation對象中含有需要的位置信息,我們需要把他取出。
在思考階段,我想直接把Listener中的BDLocation對象直接取出,把BDLocation對象變成String類型通過Netty傳輸至服務端,過程如下:

但是想法很美好,顯示很殘酷,BDAbstractLocationListener并不允許我們這么做/(ㄒoㄒ)/~~
我創(chuàng)建了MapUtil類,用于獲取位置信息
public class MapUtil {
public LocationClient mLocationClient = null;//百度地圖服務
private MyLocationListener myListener=new MyLocationListener();//創(chuàng)建監(jiān)聽器
public BDLocation location;
public MapUtil(LocationClient mLocationClient,BDLocation location)
this.mLocationClient=mLocationClient;//拿到百度地圖api中的服務
this.location=location;//拿到主線程中的netty對話管理器
}
public void init(){
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true); // 打開gps
option.setCoorType("bd09ll"); // 設置坐標類型
option.setScanSpan(1000);
// 可選,設置地址信息
option.setIsNeedAddress(true);
//可選,V7.2版本新增能力
//如果設置了該接口,首次啟動定位時,會先判斷當前Wi-Fi是否超出有效期,若超出有效期,會先重新掃描Wi-Fi,然后定位
option.setWifiCacheTimeOut(5*60*1000);
// option.setIgnoreKillProcess(true);
//可選,設置是否需要地址描述
option.setIsNeedLocationDescribe(true);
mLocationClient.setLocOption(option);//注入百度地圖定位相關配置
mLocationClient.registerLocationListener(myListener);//注冊監(jiān)聽器
mLocationClient.start();//啟動服務
mLocationClient.requestLocation();
}
public void stop(){
mLocationClient.stop();//停止服務
}
public class MyLocationListener extends BDAbstractLocationListener {
MyLocationListener(){
}
@Override
public void onReceiveLocation(BDLocation location1){
//此處的BDLocation為定位結果信息類,通過它的各種get方法可獲取定位相關的全部結果
//以下只列舉部分獲取經(jīng)緯度相關(常用)的結果信息
//更多結果信息獲取說明,請參照類參考中BDLocation類中的說明
loction=loction1;
}
}
}
我企圖直接在Listener中拿到參數(shù),結果是可以,但可以的不多

數(shù)據(jù)只能停留在Listener中,無法帶出Listener,因為Listener是在持續(xù)運行中的,相當于是一個while(true)的死循環(huán),MapUtil中的Location確實可以拿到位置,但數(shù)據(jù)也卡在了Listener中。
這個錯誤讓我思考了很久。
最后,我意識到,也許在最開始,我思考的方向就不是正確的,或許它的流程應該是這樣:

于是我把MapUtil類中的傳入的location改為了chatManger
public class MyLocationListener extends BDAbstractLocationListener {
MyLocationListener(){
}
@Override
public void onReceiveLocation(BDLocation location){
//此處的BDLocation為定位結果信息類,通過它的各種get方法可獲取定位相關的全部結果
//以下只列舉部分獲取經(jīng)緯度相關(常用)的結果信息
//更多結果信息獲取說明,請參照類參考中BDLocation類中的說明
CoderUtil coderUtil=new CoderUtil();//創(chuàng)建CoderUtil類用于處理文字
MyAddress address=coderUtil.transform(location);//將百度地圖中的location類通過CoderUtil轉換為MyAddress類
chatManager.sendData(address);//使用netty對話管理器發(fā)送處理完畢的地址
}
}
以下為chatManger代碼:
public class ChatManager implements ChatListener{
private String TAG = ChatManager.class.getSimpleName();
public static volatile ChatManager instance = null;
private ChatClient chatClient = null;
private Handler handler;
public ChatManager(){
chatClient=new ChatClient();
}
public static ChatManager getInstance(Handler handler) {
if (instance == null) {
synchronized (ChatManager.class) {
if (instance == null) {
instance = new ChatManager();
}
}
}
instance.setHandler(handler);
return instance;
}
public void setHandler(Handler handler){
this.handler = handler;
}
public void sendData(MyAddress address) {
System.out.println("ChatManger正在發(fā)送數(shù)據(jù)");
chatClient.sendMsgToServer(address, new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (future.isSuccess()) {
Log.e(TAG, "發(fā)送成功");
} else {
Log.e(TAG, "發(fā)送失敗");
}
}
});
}
public void connectNetty(IpPortInfo ipPortSetInfo) {
new Thread(new Runnable() {
@Override
public void run() {
Log.e(TAG, "客戶端啟動自動連接...");
if (!chatClient.getConnectStatus()) {
chatClient.setListener(ChatManager.this);
chatClient.connect(ipPortSetInfo);
} else {
chatClient.disconnect();
}
}
}).start();
}
@Override
public void onMessageResponse(ChannelHandlerContext ctx, String msg) {
}
@Override
public void onServiceStatusConnectChanged(int statusCode) {
}
}
總而言之,就是一個記錄Netty連接信息的類。

最后終于成功?。。。。。。。。。。。。。?!
在遇到怎么想都無法解決的問題是,一定要有破釜沉舟的勇氣啊各位,從問題的源頭開始找,詢問是不是自己一開始的方向就錯了?。?!
到此這篇關于通過netty把百度地圖API獲取的地理位置從Android端發(fā)送到Java服務器端的文章就介紹到這了,更多相關百度地圖API獲取地理位置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring中@Configuration注解和@Component注解的區(qū)別詳解
這篇文章主要介紹了Spring中@Configuration注解和@Component注解的區(qū)別詳解,@Configuration 和 @Component 到底有何區(qū)別呢?我先通過如下一個案例,在不分析源碼的情況下,小伙伴們先來直觀感受一下這兩個之間的區(qū)別,需要的朋友可以參考下2023-09-09
springboot application無法使用$獲取pom變量的問題及解決
這篇文章主要介紹了springboot application無法使用$獲取pom變量的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
Spring SpringMVC在啟動完成后執(zhí)行方法源碼解析
這篇文章主要介紹了SpringMVC在啟動完成后執(zhí)行方法源碼解析,還是非常不錯的,在這里分享給大家,需要的朋友可以參考下。2017-09-09
基于Pinpoint對SpringCloud微服務項目實現(xiàn)全鏈路監(jiān)控的問題
這篇文章主要介紹了基于Pinpoint對SpringCloud微服務項目實現(xiàn)全鏈路監(jiān)控的問題,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-02-02
MyBatis Plus 實現(xiàn)多表分頁查詢功能的示例代碼
這篇文章主要介紹了MyBatis Plus 實現(xiàn)多表分頁查詢功能,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08
SpringBoot實用小技巧之如何動態(tài)設置日志級別
這篇文章主要給大家介紹了關于SpringBoot實用小技巧之如何動態(tài)設置日志級別的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用SpringBoot具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-04-04
IDEA社區(qū)版創(chuàng)建spring boot項目的安裝插件的圖文教程
這篇文章主要介紹了IDEA社區(qū)版創(chuàng)建spring boot項目的安裝插件,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11
IntelliJ IDEA2020.2.2創(chuàng)建Servlet方法及404問題
這篇文章主要介紹了IntelliJ IDEA2020.2.2創(chuàng)建Servlet方法及404問題,這里小編使用的2020.2.2企業(yè)破解版本,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09

