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

Android百度定位導(dǎo)航之基于百度地圖移動(dòng)獲取位置和自動(dòng)定位

 更新時(shí)間:2016年01月29日 15:05:24   作者:杰瑞教育  
項(xiàng)目需求是這樣的,首先定位我當(dāng)前的起始位置,并跟隨移動(dòng)不斷自動(dòng)定位我的當(dāng)前位置,下面通過(guò)本文給大家介紹android百度定位導(dǎo)航之基于百度地圖移動(dòng)獲取位置和自動(dòng)定位,需要的朋友參考下

一、問(wèn)題描述

使用百度地圖實(shí)現(xiàn)如圖所示應(yīng)用,首先自動(dòng)定位當(dāng)前我起始位置(小圓點(diǎn)位置),并跟隨移動(dòng)不斷自動(dòng)定位我的當(dāng)前位置


百度Api不同版本使用會(huì)有些差異,本例中加入lib如下:


二、編寫(xiě)MyApplication類(lèi)

public class MyApplication extends Application {
static MyApplication myApplication;
BMapManager mBMapManager = null;
String mStrKey = "7ZfuRcOx1G3oZ8TKuTNGm4sO";
boolean m_bKeyRight = true; // 授權(quán)Key正確,驗(yàn)證通過(guò)
private List<Activity> activityList = new LinkedList<Activity>();
@Override
public void onCreate() {
myApplication = this;
initEngineManager(this);
super.onCreate();
}
public void initEngineManager(Context context) {
if (mBMapManager == null) {
mBMapManager = new BMapManager(context);
Toast.makeText(MyApplication.getInstance().getApplicationContext(),
"BMapManager 初始化SUCSUC!", Toast.LENGTH_LONG).show();
}
if (!mBMapManager.init(mStrKey, new MyGeneralListener())) {
Toast.makeText(MyApplication.getInstance().getApplicationContext(),
"BMapManager 初始化錯(cuò)誤!", Toast.LENGTH_LONG).show();
}
}

public static MyApplication getInstance() {
if (myApplication == null) {
myApplication = new MyApplication();
}
return myApplication;
}
public void addActivity(Activity activity) {
activityList.add(activity);
}
public void exit() {
for (Activity activity : activityList) {
activity.finish();
}
System.exit(0);
}
// 常用事件監(jiān)聽(tīng),用來(lái)處理通常的網(wǎng)絡(luò)錯(cuò)誤,授權(quán)驗(yàn)證錯(cuò)誤等
static class MyGeneralListener implements MKGeneralListener {
public void onGetNetworkState(int iError) {
if (iError == MKEvent.ERROR_NETWORK_CONNECT) {
Toast.makeText(
MyApplication.getInstance().getApplicationContext(),
"您的網(wǎng)絡(luò)出錯(cuò)啦!", Toast.LENGTH_LONG).show();
} else if (iError == MKEvent.ERROR_NETWORK_DATA) {
Toast.makeText(
MyApplication.getInstance().getApplicationContext(),
"輸入正確的檢索條件!", Toast.LENGTH_LONG).show();
}
// ...
}
public void onGetPermissionState(int iError) {
if (iError == MKEvent.ERROR_PERMISSION_DENIED) {
// 授權(quán)Key錯(cuò)誤:
Toast.makeText(
MyApplication.getInstance().getApplicationContext(),
"請(qǐng)輸入正確的授權(quán)Key!",
Toast.LENGTH_LONG).show();
MyApplication.getInstance().m_bKeyRight = false;
}
}
}
} 

三、編寫(xiě)主程序MainActivity,顯示當(dāng)前所處位置

主程序MainActity:

public class MainActivity extends Activity {
public MyApplication app;
static MapView mMapView = null;
public MKMapViewListener mMapListener = null;
MyLocationOverlay myLocationOverlay = null;
// 定位相關(guān)
LocationClient mLocClient;
public NotifyLister mNotifyer = null;
public MyLocationListenner myListener = new MyLocationListenner();
LocationData locData = null;
private MapController mMapController = null;
static MKSearch mkSerach;
Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
Toast.makeText(MainActivity.this, "msg:" + msg.what,
Toast.LENGTH_SHORT).show();
};
};
static TextView showAddr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showAddr = (TextView) findViewById(R.id.showAddr);
MyIcon mi = new MyIcon(this);
//在屏幕中心點(diǎn)添加接我圖標(biāo)
getWindow().addContentView(
mi,
new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
MyIcon2 m2 = new MyIcon2(this);
getWindow().addContentView(
m2,
new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
mMapView = (MapView) findViewById(R.id.bmapsView);
mMapController = mMapView.getController();
initMapView();
app = MyApplication.getInstance();
mLocClient = new LocationClient(this);
mLocClient.registerLocationListener(myListener);
//搜索初始化
mkSerach = new MKSearch();
mkSerach.init(app.mBMapManager, new MKSearchListener() {
@Override
public void onGetWalkingRouteResult(MKWalkingRouteResult arg0,
int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetTransitRouteResult(MKTransitRouteResult arg0,
int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onGetPoiDetailSearchResult(int arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetDrivingRouteResult(MKDrivingRouteResult arg0,
int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetAddrResult(MKAddrInfo info, int arg1) {
showAddr.setText(info.strAddr);
}
});
//設(shè)置地圖相關(guān)
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);
option.setCoorType("bd09ll");
option.setScanSpan(300000);
mLocClient.setLocOption(option);
mLocClient.start();
mMapView.getController().setZoom(16);
mMapView.getController().enableClick(true);
mMapView.displayZoomControls(true);
mMapListener = new MKMapViewListener() {
public void onMapMoveFinish() {
}
public void onClickMapPoi(MapPoi mapPoiInfo) {
// TODO Auto-generated method stub
String title = "";
if (mapPoiInfo != null) {
title = mapPoiInfo.strText;
Toast.makeText(MainActivity.this, title, Toast.LENGTH_SHORT)
.show();
}
}
};
mMapView.regMapViewListener(MyApplication.getInstance().mBMapManager,
mMapListener);
myLocationOverlay = new MyLocationOverlay(mMapView);
locData = new LocationData();
myLocationOverlay.setData(locData);
mMapView.getOverlays().add(myLocationOverlay);
myLocationOverlay.enableCompass();
mMapView.refresh();
}
private void initMapView() {
mMapView.setLongClickable(true);
}
/**
* 監(jiān)聽(tīng)函數(shù),又新位置的時(shí)候,格式化成字符串,輸出到屏幕中
*/
public class MyLocationListenner implements BDLocationListener {
public void onReceiveLocation(BDLocation location) {
if (location == null)
return;
locData.latitude = location.getLatitude();
locData.longitude = location.getLongitude();
locData.direction = 2.0f;
locData.accuracy = location.getRadius();
locData.direction = location.getDerect();
Log.d("loctest",
String.format("before: lat: %f lon: %f",
location.getLatitude(), location.getLongitude()));
myLocationOverlay.setData(locData);
mMapView.refresh();
mMapController
.animateTo(new GeoPoint((int) (locData.latitude * 1e6),
(int) (locData.longitude * 1e6)), mHandler
.obtainMessage(1));
}
public void onReceivePoi(BDLocation poiLocation) {
if (poiLocation == null) {
return;
}
}
}
public class NotifyLister extends BDNotifyListener {
public void onNotify(BDLocation mlocation, float distance) {
}
}
@Override
protected void onPause() {
mMapView.onPause();
super.onPause();
}
@Override
protected void onResume() {
mMapView.onResume();
super.onResume();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mMapView.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mMapView.onRestoreInstanceState(savedInstanceState);
}
public static void getPosition(GeoPoint g) {
mkSerach.reverseGeocode(g);
showAddr.setText("獲取位置中...");
}
}

Xml布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView 
android:id="@+id/showAddr"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="選擇地點(diǎn)"
android:textSize="20dp"
/> 
<com.baidu.mapapi.map.MapView
android:id="@+id/bmapsView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" /> 
</LinearLayout>

三、繪制圖形

MyIcon 的onDraw實(shí)現(xiàn)繪制中心點(diǎn)的圖標(biāo),MyIcon2繪制指示器圖標(biāo),如圖所示

public class MyIcon extends View {
public static int w;
public static int h;
public static Bitmap mBitmap;
public MyIcon(Context context) {
super(context);
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.me);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
w = this.getWidth() / 2 - mBitmap.getWidth() / 2;
h = this.getHeight() / 2 - mBitmap.getHeight() / 2;
canvas.drawBitmap(mBitmap, w, h, null);
}
}
public class MyIcon2 extends View{
public static int w;
public static int h;
private Bitmap mBitmap;
public MyIcon2(Context context) {
super(context);
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.jiewo);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
w = this.getWidth() / 2 - mBitmap.getWidth() / 2;
h = (this.getHeight() / 2 - mBitmap.getHeight() / 2) - ( MyIcon.mBitmap.getHeight()/2);
canvas.drawBitmap(mBitmap, w, h, null);
}
}

以上所述是小編給大家分享Android百度定位導(dǎo)航之基于百度地圖移動(dòng)獲取位置和自動(dòng)定位的相關(guān)知識(shí),希望對(duì)大家有所幫助。

相關(guān)文章

  • Android自定義View九宮格手勢(shì)密碼解鎖

    Android自定義View九宮格手勢(shì)密碼解鎖

    這篇文章主要為大家詳細(xì)介紹了Android自定義View九宮格手勢(shì)密碼解鎖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • Android獲取驗(yàn)證碼倒計(jì)時(shí)實(shí)現(xiàn)代碼

    Android獲取驗(yàn)證碼倒計(jì)時(shí)實(shí)現(xiàn)代碼

    這篇文章主要為大家詳細(xì)介紹了Android獲取驗(yàn)證碼倒計(jì)時(shí)的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • Android中兩個(gè)類(lèi)讓你再也不用實(shí)現(xiàn)onActivityResult()

    Android中兩個(gè)類(lèi)讓你再也不用實(shí)現(xiàn)onActivityResult()

    這篇文章主要給大家介紹了關(guān)于Android中兩個(gè)類(lèi)讓你再也不用實(shí)現(xiàn)onActivityResult()的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位Android開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧
    2018-08-08
  • Android中l(wèi)ayout屬性大全

    Android中l(wèi)ayout屬性大全

    這篇文章主要介紹了Android中l(wèi)ayout屬性含義及用法,較為詳細(xì)的總結(jié)分析了layout屬性相關(guān)用法,需要的朋友可以參考下
    2015-05-05
  • 詳解Android中Handler的使用方法

    詳解Android中Handler的使用方法

    這篇文章主要介紹了Android中Handler的使用方法,對(duì)Android中Handler的作用于如何使用進(jìn)行了初步介紹,需要的朋友可以參考下
    2015-12-12
  • Android系統(tǒng)添加自己寫(xiě)的工具

    Android系統(tǒng)添加自己寫(xiě)的工具

    今天小編就為大家分享一篇關(guān)于Android系統(tǒng)添加自己寫(xiě)的工具的文章,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2018-10-10
  • Android仿微信朋友圈添加圖片的實(shí)例代碼

    Android仿微信朋友圈添加圖片的實(shí)例代碼

    本篇文章主要介紹了Android仿微信朋友圈添加圖片的實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • Android圖片壓縮上傳之基礎(chǔ)篇

    Android圖片壓縮上傳之基礎(chǔ)篇

    這篇文章主要介紹了Android圖片壓縮上傳之基礎(chǔ)篇的相關(guān)內(nèi)容,本文介紹的非常詳解,具有參考借鑒價(jià)值,感興趣的朋友一起看下吧
    2016-05-05
  • 深入Android Handler,MessageQueue與Looper關(guān)系

    深入Android Handler,MessageQueue與Looper關(guān)系

    這篇文章主要介紹了深入Android Handler,MessageQueue與Looper關(guān)系,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-08-08
  • Android開(kāi)發(fā)graphics?bufferqueue整體流程

    Android開(kāi)發(fā)graphics?bufferqueue整體流程

    這篇文章主要為大家介紹了Android開(kāi)發(fā)graphics?bufferqueue整體流程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07

最新評(píng)論