欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

通過(guò)netty把百度地圖API獲取的地理位置從Android端發(fā)送到Java服務(wù)器端的操作方法

 更新時(shí)間:2022年10月19日 10:02:44   作者:江浙滬漸凍人  
這篇文章主要介紹了通過(guò)netty把百度地圖API獲取的地理位置從Android端發(fā)送到Java服務(wù)器端,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

本篇記錄我在實(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)類型
    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 銷毀后不在處理新接收的位置
        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類型通過(guò)Netty傳輸至服務(wù)端,過(guò)程如下:

但是想法很美好,顯示很殘酷,BDAbstractLocationListener并不允許我們這么做/(ㄒoㄒ)/~~

我創(chuàng)建了MapUtil類,用于獲取位置信息

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;//拿到主線程中的netty對(duì)話管理器
}

public void init(){
    LocationClientOption option = new LocationClientOption();
    option.setOpenGps(true); // 打開(kāi)gps
    option.setCoorType("bd09ll"); // 設(shè)置坐標(biāo)類型
    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é)果信息類,通過(guò)它的各種get方法可獲取定位相關(guān)的全部結(jié)果
        //以下只列舉部分獲取經(jīng)緯度相關(guān)(常用)的結(jié)果信息
        //更多結(jié)果信息獲取說(shuō)明,請(qǐng)參照類參考中BDLocation類中的說(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類中的傳入的location改為了chatManger

public class MyLocationListener extends BDAbstractLocationListener {
    MyLocationListener(){

    }
    @Override
    public void onReceiveLocation(BDLocation location){
        //此處的BDLocation為定位結(jié)果信息類,通過(guò)它的各種get方法可獲取定位相關(guān)的全部結(jié)果
        //以下只列舉部分獲取經(jīng)緯度相關(guān)(常用)的結(jié)果信息
        //更多結(jié)果信息獲取說(shuō)明,請(qǐng)參照類參考中BDLocation類中的說(shuō)明


        CoderUtil coderUtil=new CoderUtil();//創(chuàng)建CoderUtil類用于處理文字
        MyAddress address=coderUtil.transform(location);//將百度地圖中的location類通過(guò)CoderUtil轉(zhuǎn)換為MyAddress類
        chatManager.sendData(address);//使用netty對(duì)話管理器發(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, "客戶端啟動(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連接信息的類。

最后終于成功?。。。。。。。。。。。。。。?/p>

在遇到怎么想都無(wú)法解決的問(wèn)題是,一定要有破釜沉舟的勇氣啊各位,從問(wèn)題的源頭開(kāi)始找,詢問(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)文章

最新評(píng)論