国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

簡單實現安卓里百度地圖持續定位

2019-12-12 04:07:03
字體:
來源:轉載
供稿:網友

這幾天自己研究了關于地手機上面開發安卓地圖的問題,發現百度官方示例demo講解百度持續定位方面還是講解的有些不清楚,本人研究了幾次之后將其弄得更詳細以便于讓各位方便學習,有不足之處請在評論區指出,官方示例的網址是:http://lbsyun.baidu.com/index.php?title=android-locsdk/guide/v5-0

上面的網址已經將安卓簡單配置百度地圖環境講解的很詳細了,再次不做贅述了,此外,可能會有人發現

package com.example.andoridloca;import java.util.List;import java.util.Timer;import java.util.TimerTask;import android.app.Activity;import android.content.ContentValues;import android.content.Intent;import android.database.Cursor;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.text.method.ScrollingMovementMethod;import android.util.Log;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;import com.baidu.location.BDLocation;import com.baidu.location.BDLocationListener;import com.baidu.location.LocationClient;import com.baidu.location.LocationClientOption;import com.baidu.location.BDNotifyListener;//假如用到位置提醒功能,需要import該類import com.baidu.location.LocationClientOption.LocationMode;import com.baidu.location.Poi;import com.baidu.mapapi.SDKInitializer;import com.baidu.mapapi.map.MapView;public class MainActivity extends Activity implements OnClickListener{ MapView mMapView = null;  public static final String TAG="mian"; StringBuffer sb = new StringBuffer(256); public StringBuilder builder=new StringBuilder(); private Button bt1; private TextView tv1; private DBtools DBhelper; boolean isOpenLocation=false; public LocationClient mLocationClient = null; public BDLocationListener myListener = new MyLocationListener(); @Override protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  SDKInitializer.initialize(getApplicationContext());   setContentView(R.layout.activity_main);  DBhelper = new DBtools(this);   tv1=(TextView) findViewById(R.id.textView1);  tv1.setMovementMethod(new ScrollingMovementMethod());  bt1=(Button) findViewById(R.id.button1);  bt1.setOnClickListener(this);  mMapView = (MapView) findViewById(R.id.bmapView);   mLocationClient = new LocationClient(getApplicationContext());  //聲明LocationClient類  mLocationClient.registerLocationListener( myListener ); //注冊監聽函數  initLocation(); } private void initLocation(){  LocationClientOption option = new LocationClientOption();  option.setLocationMode(LocationMode.Hight_Accuracy);//可選,默認高精度,設置定位模式,高精度,低功耗,僅設備  option.setCoorType("bd09ll");//可選,默認gcj02,設置返回的定位結果坐標系  int span=0;  option.setScanSpan(span);//可選,默認0,即僅定位一次,設置發起定位請求的間隔需要大于等于1000ms才是有效的  option.setIsNeedAddress(true);//可選,設置是否需要地址信息,默認不需要  option.setOpenGps(true);//可選,默認false,設置是否使用gps  option.setLocationNotify(true);//可選,默認false,設置是否當gps有效時按照1S1次頻率輸出GPS結果  option.setIsNeedLocationDescribe(true);//可選,默認false,設置是否需要位置語義化結果,可以在BDLocation.getLocationDescribe里得到,結果類似于“在北京天安門附近”  option.setIsNeedLocationPoiList(true);//可選,默認false,設置是否需要POI結果,可以在BDLocation.getPoiList里得到  option.setIgnoreKillProcess(false);//可選,默認true,定位SDK內部是一個SERVICE,并放到了獨立進程,設置是否在stop的時候殺死這個進程,默認不殺死   option.SetIgnoreCacheException(false);//可選,默認false,設置是否收集CRASH信息,默認收集  option.setEnableSimulateGps(false);//可選,默認false,設置是否需要過濾gps仿真結果,默認需要  mLocationClient.setLocOption(option); } @Override  protected void onDestroy() {   super.onDestroy();   //在activity執行onDestroy時執行mMapView.onDestroy(),實現地圖生命周期管理   mMapView.onDestroy();  }  @Override  protected void onResume() {   super.onResume();   //在activity執行onResume時執行mMapView. onResume (),實現地圖生命周期管理   mMapView.onResume();   }  @Override  protected void onPause() {   super.onPause();   //在activity執行onPause時執行mMapView. onPause (),實現地圖生命周期管理   mMapView.onPause();   }  public class MyLocationListener implements BDLocationListener {  @Override  public void onReceiveLocation(BDLocation location) {   //Receive Location   StringBuffer sb = new StringBuffer(256);   sb.append("time : ");   sb.append(location.getTime());   sb.append("/nerror code : ");   sb.append(location.getLocType());   sb.append("/nlatitude : ");   sb.append(location.getLatitude());   sb.append("/nlontitude : ");   sb.append(location.getLongitude());   sb.append("/nradius : ");   sb.append(location.getRadius());   if (location.getLocType() == BDLocation.TypeGpsLocation){// GPS定位結果    sb.append("/nspeed : ");    sb.append(location.getSpeed());// 單位:公里每小時    sb.append("/nsatellite : ");    sb.append(location.getSatelliteNumber());    sb.append("/nheight : ");    sb.append(location.getAltitude());// 單位:米    sb.append("/ndirection : ");    sb.append(location.getDirection());// 單位度    sb.append("/naddr : ");    sb.append(location.getAddrStr());    sb.append("/ndescribe : ");    sb.append("gps定位成功");   } else if (location.getLocType() == BDLocation.TypeNetWorkLocation){// 網絡定位結果    sb.append("/naddr : ");    sb.append(location.getAddrStr());    //運營商信息    sb.append("/noperationers : ");    sb.append(location.getOperators());    sb.append("/ndescribe : ");    sb.append("網絡定位成功");   } else if (location.getLocType() == BDLocation.TypeOffLineLocation) {// 離線定位結果    sb.append("/ndescribe : ");    sb.append("離線定位成功,離線定位結果也是有效的");   } else if (location.getLocType() == BDLocation.TypeServerError) {    sb.append("/ndescribe : ");    sb.append("服務端網絡定位失敗,可以反饋IMEI號和大體定位時間到loc-bugs@baidu.com,會有人追查原因");   } else if (location.getLocType() == BDLocation.TypeNetWorkException) {    sb.append("/ndescribe : ");    sb.append("網絡不同導致定位失敗,請檢查網絡是否通暢");   } else if (location.getLocType() == BDLocation.TypeCriteriaException) {    sb.append("/ndescribe : ");    sb.append("無法獲取有效定位依據導致定位失敗,一般是由于手機的原因,處于飛行模式下一般會造成這種結果,可以試著重啟手機");   }    sb.append("/nlocationdescribe : ");    sb.append(location.getLocationDescribe());// 位置語義化信息    List<Poi> list = location.getPoiList();// POI數據    if (list != null) {     sb.append("/npoilist size = : ");     sb.append(list.size());     for (Poi p : list) {     sb.append("/npoi= : ");     sb.append(p.getId() + " " + p.getName() + " " + p.getRank());     }    }   Log.i("BaiduLocationApiDem", sb.toString());   DBtools dbhelper=new DBtools(getApplicationContext());   ContentValues initialValues = new ContentValues();   initialValues.put("shijian",location.getTime());   initialValues.put("didian",location.getLatitude()+"--"+location.getLongitude());   dbhelper.open();   dbhelper.insert("path",initialValues);   dbhelper.close();   tv1.setText(sb.toString());   } } @Override public void onClick(View arg0) {  Thread mytime=new Thread(new ThreadShow());   if(isOpenLocation){    mLocationClient.stop();    isOpenLocation=false;    }   else{    Toast.makeText(getApplicationContext(), "開啟", Toast.LENGTH_SHORT).show();      isOpenLocation=true;    //mLocationClient.start();    mytime.start();    }  } // handler類接收數據  Handler handler = new Handler() {   public void handleMessage(Message msg) {    if (msg.what == 1) {     Log.i("BaiduLocationApiDem", "加以");    mLocationClient.start();    mLocationClient.requestLocation();    }   };  };   // 線程類  class ThreadShow implements Runnable {    @Override   public void run() {    // TODO Auto-generated method stub    while (isOpenLocation) {     try {      mLocationClient.stop();     Thread.sleep(2000);      Message msg = new Message();      msg.what = 1;      handler.sendMessage(msg);      // System.out.println("send...");     } catch (Exception e) {      // TODO Auto-generated catch block      e.printStackTrace();      System.out.println("thread error...");     }    }   }  }  }

這里面關于mLocationClient.stop();mLocationClient.start();  mLocationClient.requestLocation(); 這三個函數我有必要講解一下,因為持續定位時這三個函數的配合使用很重要,官方文檔里面解釋說mLocationClient.start()函數用于開啟定位,mLocationClient.requestLocation()函數用于主動觸發定位SDK內部定位邏輯,個人感覺差不多,兩個都會執行我的mLocationClient的所屬類里面的邏輯代碼,可能是我的項目就這樣吧,然后是mLocationClient.stop(),此函數用于停止定位,如果持續定位的話,是需要和mLocationClient.start()函數配合使用的,具體在上面的代碼里面有展示。

切記不要將mLocationClient.start()和mLocationClient.stop()一起使用,我在網上查詢時好像是說一部原因,具體舉一個例子吧:

//某段代碼如果是這樣的話按照邏輯韓式會將mLocationClient所屬類的里面邏輯代碼執行一遍,具體見MainAvtivity里面的MyLocationListener類內容,但是實際上是不會執行的mLocationClient.start();mLocationClient.stop();

所以在我的MainActivity里面我使用線程來一遍遍的執行start和stop函數,這樣就會消除剛剛說的這種效果,最后就能夠實現持續定位了。

在此給出我的布局文件配合看看

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.newloca.MainActivity" ><RelativeLayout  android:layout_width="match_parent" android:layout_height="300dp"> <com.baidu.mapapi.map.MapView  android:id="@+id/bmapView"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:clickable="true" /></RelativeLayout> <TextView  android:id="@+id/textView1"  android:layout_width="match_parent"  android:layout_height="150dp"  android:scrollbars="vertical"  android:background="#f00"  android:text="位置信息" /> <RelativeLayout  android:layout_width="match_parent"  android:layout_height="match_parent"   >  <Button   android:id="@+id/button1"   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:layout_alignParentRight="true"   android:layout_alignParentBottom="true"   android:text="獲取位置" /> </RelativeLayout></LinearLayout>

其他像權限什么的配置,用最開始給的官方地址里面的就行了

順便說一下,本人是使用的安卓4.2版本開發的,手機真機調試和虛擬機調試在定位的時間間隔上面會有點誤差,也不知道什么原因

以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持武林網!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新和县| 平阴县| 日喀则市| 佳木斯市| 大安市| 临澧县| 赤峰市| 墨竹工卡县| 湘潭市| 保康县| 册亨县| 三江| 同德县| 周至县| 南丰县| 安平县| 舟曲县| 竹北市| 蒲城县| 霍城县| 无为县| 景东| 宁津县| 博客| 大石桥市| 三门峡市| 专栏| 昔阳县| 南丰县| 莫力| 黔西| 若尔盖县| 沧州市| 介休市| 马尔康县| 秭归县| 社旗县| 鹿泉市| 微山县| 蒙山县| 福泉市|