最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android百度定位導航之基于百度地圖移動獲取位置和自動定位

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

一、問題描述

使用百度地圖實現如圖所示應用,首先自動定位當前我起始位置(小圓點位置),并跟隨移動不斷自動定位我的當前位置


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


二、編寫MyApplication類

public class MyApplication extends Application {
static MyApplication myApplication;
BMapManager mBMapManager = null;
String mStrKey = "7ZfuRcOx1G3oZ8TKuTNGm4sO";
boolean m_bKeyRight = true; // 授權Key正確,驗證通過
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 初始化錯誤!", 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)聽,用來處理通常的網絡錯誤,授權驗證錯誤等
static class MyGeneralListener implements MKGeneralListener {
public void onGetNetworkState(int iError) {
if (iError == MKEvent.ERROR_NETWORK_CONNECT) {
Toast.makeText(
MyApplication.getInstance().getApplicationContext(),
"您的網絡出錯啦!", 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) {
// 授權Key錯誤:
Toast.makeText(
MyApplication.getInstance().getApplicationContext(),
"請輸入正確的授權Key!",
Toast.LENGTH_LONG).show();
MyApplication.getInstance().m_bKeyRight = false;
}
}
}
} 

三、編寫主程序MainActivity,顯示當前所處位置

主程序MainActity:

public class MainActivity extends Activity {
public MyApplication app;
static MapView mMapView = null;
public MKMapViewListener mMapListener = null;
MyLocationOverlay myLocationOverlay = null;
// 定位相關
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);
//在屏幕中心點添加接我圖標
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);
}
});
//設置地圖相關
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)聽函數,又新位置的時候,格式化成字符串,輸出到屏幕中
*/
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="選擇地點"
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實現繪制中心點的圖標,MyIcon2繪制指示器圖標,如圖所示

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百度定位導航之基于百度地圖移動獲取位置和自動定位的相關知識,希望對大家有所幫助。

相關文章

  • Android文本框搜索和清空效果實現代碼及簡要概述

    Android文本框搜索和清空效果實現代碼及簡要概述

    在工作過程中可能會遇到這樣一個效果:文本框輸入為空時顯示輸入的圖標;不為空時顯示清空的圖標,此時點擊清空圖標能清空文本框內輸入文字,感興趣的你可以了解下哦,或許對你學習android有所幫助
    2013-02-02
  • 百度語音識別(Baidu Voice) Android studio版本詳解

    百度語音識別(Baidu Voice) Android studio版本詳解

    這篇文章主要介紹了百度語音識別(Baidu Voice) Android studio版本詳解的相關資料,需要的朋友可以參考下
    2016-09-09
  • Android組件Glide實現圖片平滑滾動效果

    Android組件Glide實現圖片平滑滾動效果

    這篇文章主要介紹了Android組件Glide實現圖片平滑滾動效果的相關資料,具有一定的參考價值,需要的朋友可以參考下
    2016-07-07
  • Android自定義View實現圓弧進度效果逐步完成過程

    Android自定義View實現圓弧進度效果逐步完成過程

    在Android開發(fā)中,通過自定義View實現自己想要的效果是作為android開發(fā)程序員的一項必備技能,自定義View對于android開發(fā)來說也是比較難的一項技術
    2023-04-04
  • Android Toast的用法總結(五種用法)

    Android Toast的用法總結(五種用法)

    本篇文章主要介紹了Android Toast的用法總結(五種用法),android toast幾種使用方法 toast經常會用到,今天做個總結,特別是自定義toast的布局,值得一看。
    2017-01-01
  • RxJava2 線程調度的方法

    RxJava2 線程調度的方法

    這篇文章主要介紹了RxJava2 線程調度的方法,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03
  • Android 6.0權限申請詳解及權限資料整理

    Android 6.0權限申請詳解及權限資料整理

    這篇文章主要介紹了Android 6.0權限申請詳解及權限資料整理的相關資料,需要的朋友可以參考下
    2016-10-10
  • Android三級緩存原理講解

    Android三級緩存原理講解

    今天小編就為大家分享一篇關于Android三級緩存原理講解,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • Android用Canvas繪制貝塞爾曲線

    Android用Canvas繪制貝塞爾曲線

    這篇文章主要為大家詳細介紹了Android用Canvas繪制貝塞爾曲線,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • Android listview多視圖嵌套多視圖

    Android listview多視圖嵌套多視圖

    這篇文章主要介紹了Android listview多視圖嵌套多視圖 的相關資料,需要的朋友可以參考下
    2016-02-02

最新評論

永平县| 仁怀市| 弥勒县| 邹平县| 罗田县| 甘孜| 泗水县| 方城县| 新源县| 临汾市| 鲁山县| 柏乡县| 抚州市| 彭州市| 华亭县| 莫力| 延边| 综艺| 九龙县| 北安市| 烟台市| 平潭县| 景德镇市| 莒南县| 恩施市| 石狮市| 肥东县| 合山市| 曲阳县| 南丹县| 永兴县| 萍乡市| 平江县| 石狮市| 郁南县| 巴林左旗| 财经| 天峨县| 德庆县| 台州市| 刚察县|