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

我們可以先來(lái)看看百度地圖官方給出的相關(guān)代碼
public class MainActivity extends AppCompatActivity {
private MapView mMapView = null;
private BaiduMap mBaiduMap = null;
private LocationClient mLocationClient = null;
private TextView mtextView;
// 是否是第一次定位
private boolean isFirstLocate = true;
// 當(dāng)前定位模式
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);
//開(kāi)啟交通圖
mBaiduMap = mMapView.getMap();
mBaiduMap.setTrafficEnabled(true);
//開(kāi)啟地圖的定位圖層
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();
}
//通過(guò)LocationClientOption設(shè)置LocationClient相關(guān)參數(shù)
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true); // 打開(kāi)gps
option.setCoorType("bd09ll"); // 設(shè)置坐標(biāo)類(lèi)型
option.setScanSpan(1000);
// 可選,設(shè)置地址信息
option.setIsNeedAddress(true);
//可選,設(shè)置是否需要地址描述
option.setIsNeedLocationDescribe(true);
//設(shè)置locationClientOption
mLocationClient.setLocOption(option);
//注冊(cè)LocationListener監(jiān)聽(tīng)器
MyLocationListene myLocationListener = new MyLocationListene();
mLocationClient.registerLocationListener(myLocationListener);
//開(kāi)啟地圖定位圖層
mLocationClient.start();
}
public class MyLocationListene extends BDAbstractLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
//mapView 銷(xiāo)毀后不在處理新接收的位置
if (location == null || mMapView == null) {
return;
}
LatLng ll = new LatLng(location.getLatitude(), location.getLongitude());
if (isFirstLocate) {
isFirstLocate = false;
//給地圖設(shè)置狀態(tài)
mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newLatLng(ll));
}
MyLocationData locData = new MyLocationData.Builder()
.accuracy(location.getRadius())
// 此處設(shè)置開(kāi)發(fā)者獲取到的方向信息,順時(shí)針0-360
.direction(location.getDirection()).latitude(location.getLatitude())
.longitude(location.getLongitude()).build();
mBaiduMap.setMyLocationData(locData);
// 更換定位圖標(biāo),這里的圖片是放在 drawble 文件下的
BitmapDescriptor mCurrentMarker = BitmapDescriptorFactory.fromResource(R.drawable.icon_gcoding);
// 定位模式 地圖SDK支持三種定位模式:NORMAL(普通態(tài)), FOLLOWING(跟隨態(tài)), COMPASS(羅盤(pán)態(tài))
locationMode = MyLocationConfiguration.LocationMode.NORMAL;
// 定位模式、是否開(kāi)啟方向、設(shè)置自定義定位圖標(biāo)、精度圈填充顏色以及精度圈邊框顏色5個(gè)屬性(此處只設(shè)置了前三個(gè))。
MyLocationConfiguration mLocationConfiguration = new MyLocationConfiguration(locationMode,true,mCurrentMarker);
// 使自定義的配置生效
mBaiduMap.setMyLocationConfiguration(mLocationConfiguration);
// 顯示當(dāng)前信息
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國(guó)家:" + 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());
}
}
}
使用者需要?jiǎng)?chuàng)建一個(gè)LocationClient對(duì)象,為L(zhǎng)ocationClient配置Option、注冊(cè)監(jiān)聽(tīng)器(BDAbstractLocationListener)來(lái)獲取位置信息,監(jiān)聽(tīng)器得到的BDLocation對(duì)象中含有需要的位置信息,我們需要把他取出。
在思考階段,我想直接把Listener中的BDLocation對(duì)象直接取出,把BDLocation對(duì)象變成String類(lèi)型通過(guò)Netty傳輸至服務(wù)端,過(guò)程如下:

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

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

于是我把MapUtil類(lèi)中的傳入的location改為了chatManger
public class MyLocationListener extends BDAbstractLocationListener {
MyLocationListener(){
}
@Override
public void onReceiveLocation(BDLocation location){
//此處的BDLocation為定位結(jié)果信息類(lèi),通過(guò)它的各種get方法可獲取定位相關(guān)的全部結(jié)果
//以下只列舉部分獲取經(jīng)緯度相關(guān)(常用)的結(jié)果信息
//更多結(jié)果信息獲取說(shuō)明,請(qǐng)參照類(lèi)參考中BDLocation類(lèi)中的說(shuō)明
CoderUtil coderUtil=new CoderUtil();//創(chuàng)建CoderUtil類(lèi)用于處理文字
MyAddress address=coderUtil.transform(location);//將百度地圖中的location類(lèi)通過(guò)CoderUtil轉(zhuǎn)換為MyAddress類(lèi)
chatManager.sendData(address);//使用netty對(duì)話(huà)管理器發(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, "客戶(hù)端啟動(dòng)自動(dòng)連接...");
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) {
}
}
總而言之,就是一個(gè)記錄Netty連接信息的類(lèi)。

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

