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

首頁 > 系統 > Android > 正文

Android百度地圖實現搜索和定位及自定義圖標繪制并點擊時彈出泡泡

2020-04-11 10:56:22
字體:
來源:轉載
供稿:網友

一、問題描述

  上一次我們使用百度地圖實現基本的定位功能,接下來我們繼續實現搜索和定位,并使用LocationOverlay繪制定位位置,同時展示如何使用自定義圖標繪制并點擊時彈出泡泡

  如圖所示:

二、編寫MyApplication類

public class MyApplication extends Application {private static MyApplication mInstance = null;public boolean m_bKeyRight = true;public BMapManager mBMapManager = null;public static final String strKey = "申請的應用key";@Overridepublic void onCreate() {super.onCreate();mInstance = this;initEngineManager(this);}public void initEngineManager(Context context) {if (mBMapManager == null) {mBMapManager = new BMapManager(context);}if (!mBMapManager.init(strKey,new MyGeneralListener())) {Toast.makeText(MyApplication.getInstance().getApplicationContext(), "BMapManager 初始化錯誤!", Toast.LENGTH_LONG).show();}}public static MyApplication getInstance() {return mInstance;}// 常用事件監聽,用來處理通常的網絡錯誤,授權驗證錯誤等public static class MyGeneralListener implements MKGeneralListener {@Overridepublic 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();}}@Overridepublic void onGetPermissionState(int iError) {//非零值表示key驗證未通過if (iError != 0) {//授權Key錯誤:Toast.makeText(MyApplication.getInstance().getApplicationContext(), "請在 DemoApplication.java文件輸入正確的授權Key,并檢查您的網絡連接是否正常!error: "+iError, Toast.LENGTH_LONG).show();MyApplication.getInstance().m_bKeyRight = true;}else{MyApplication.getInstance().m_bKeyRight = true;//Toast.makeText(DemoApplication.getInstance().getApplicationContext(), "key認證成功", Toast.LENGTH_LONG).show();}}}}

三、編寫MyLocationMapView,繼承MapView重寫onTouchEvent實現泡泡處理操作

public class MyLocationMapView extends MapView {public static PopupOverlay pop = null;// 彈出泡泡圖層,點擊圖標使用public MyLocationMapView(Context context) {super(context);}public MyLocationMapView(Context context, AttributeSet attrs) {super(context, attrs);}public MyLocationMapView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}@Overridepublic boolean onTouchEvent(MotionEvent event) {if (!super.onTouchEvent(event)) {// 消隱泡泡if (pop != null && event.getAction() == MotionEvent.ACTION_UP)pop.hidePop();}return true;}}

三、編寫主程序MainActivity

  編寫主程序MainActivity,用來展示如何結合定位SDK實現定位,并使用MyLocation Overlay繪制定位位置 同時展示如何使用自定義圖標繪制并點擊時彈出泡泡。

public class MainActivity extends Activity {private EditText txtAddr;// 定位相關LocationClient mLocClient;LocationData locData = null;public MyLocationListenner myListener = new MyLocationListenner();public MyApplication app;//定位圖層locationOverlay myLocationOverlay = null;//彈出泡泡圖層private PopupOverlay pop = null;//彈出泡泡圖層,瀏覽節點時使用private TextView popupText = null;//泡泡viewprivate View viewCache = null;//地圖相關,使用繼承MapView的MyLocationMapView目的是重寫touch事件實現泡泡處理//如果不處理touch事件,則無需繼承,直接使用MapView即可public MyLocationMapView mMapView = null; // 地圖Viewprivate MapController mMapController = null;private MKSearch mMKSearch = null;//用于信息檢索服務 //UI相關OnCheckedChangeListener radioButtonListener = null;TextView requestLocButton ,btSerach;boolean isRequest = false;//是否手動觸發請求定位boolean isFirstLoc = true;//是否首次定位@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);/*** 使用地圖sdk前需先初始化BMapManager.* BMapManager是全局的,可為多個MapView共用,它需要地圖模塊創建前創建,* 并在地圖地圖模塊銷毀后銷毀,只要還有地圖模塊在使用,BMapManager就不應該銷毀*/app = (MyApplication)this.getApplication();if (app.mBMapManager == null) {app.mBMapManager = new BMapManager(getApplicationContext());/*** 如果BMapManager沒有初始化則初始化BMapManager*/app.mBMapManager.init(MyApplication.strKey,new MyApplication.MyGeneralListener());}setContentView(R.layout.activity_main);txtAddr=(EditText)findViewById(R.id.txtAddr);//關鍵字輸入框//監聽搜索單擊事件btSerach= (TextView)findViewById(R.id.btOk);btSerach.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {mMKSearch.poiSearchInCity("", txtAddr.getText().toString()); }});//定位按鈕requestLocButton = (TextView)findViewById(R.id.btget);requestLocButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {requestLocClick();}});//地圖初始化mMapView = (MyLocationMapView)findViewById(R.id.bmapView);mMapView.setTraffic(true);//設置地圖模式為交通視圖(也可為衛星視圖) mMapController = mMapView.getController();mMapView.getController().setZoom(15);mMapView.getController().enableClick(true);mMapView.setBuiltInZoomControls(true);//信息檢索初始化mMKSearch = new MKSearch(); mMKSearch.init(app.mBMapManager, new MKSearchListener() {@Overridepublic void onGetAddrResult(MKAddrInfo arg0, int arg1) {}@Overridepublic void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {}@Overridepublic void onGetDrivingRouteResult(MKDrivingRouteResult arg0,int arg1) {}@Overridepublic void onGetPoiDetailSearchResult(int arg0, int arg1) {}@Overridepublic void onGetPoiResult(MKPoiResult res, int type, int error) {if (error == MKEvent.ERROR_RESULT_NOT_FOUND) {Toast.makeText(MainActivity.this, "抱歉,未找到結果",Toast.LENGTH_LONG).show();return;} else if (error != 0 || res == null) {Toast.makeText(MainActivity.this, "搜索出錯啦.."+error,Toast.LENGTH_LONG).show();return;}PoiOverlay poiOverlay = new PoiOverlay(MainActivity.this,mMapView);poiOverlay.setData(res.getAllPoi());mMapView.getOverlays().clear();mMapView.getOverlays().add(poiOverlay);mMapView.refresh();for (MKPoiInfo info : res.getAllPoi()) {if (info.pt != null) {mMapView.getController().animateTo(info.pt);break;}}}@Overridepublic void onGetShareUrlResult(MKShareUrlResult arg0, int arg1,int arg2) {}@Overridepublic void onGetSuggestionResult(MKSuggestionResult arg0, int arg1){}@Overridepublic void onGetTransitRouteResult(MKTransitRouteResult arg0,int arg1) {}@Overridepublic void onGetWalkingRouteResult(MKWalkingRouteResult arg0,int arg1) {}});createPaopao();//定位初始化mLocClient = new LocationClient( this );locData = new LocationData();mLocClient.registerLocationListener( myListener );LocationClientOption option = new LocationClientOption();option.setOpenGps(true);//打開gpsoption.setAddrType("all");//返回的定位結果包含地址信息option.disableCache(false);//禁止啟用緩存定位option.setCoorType("bd09ll"); //設置坐標類型option.setScanSpan(1000);mLocClient.setLocOption(option);mLocClient.start();//定位圖層初始化myLocationOverlay = new locationOverlay(mMapView);//設置定位數據myLocationOverlay.setData(locData);//添加定位圖層mMapView.getOverlays().add(myLocationOverlay);myLocationOverlay.enableCompass();//修改定位數據后刷新圖層生效mMapView.refresh();}/*** 手動觸發一次定位請求*/public void requestLocClick(){isRequest = true;mLocClient.requestLocation();Toast.makeText(MainActivity.this, "正在定位……", Toast.LENGTH_SHORT).show();}/*** 創建彈出泡泡圖層*/public void createPaopao(){viewCache = getLayoutInflater().inflate(R.layout.custom_text_view, null);popupText =(TextView) viewCache.findViewById(R.id.textcache);//泡泡點擊響應回調PopupClickListener popListener = new PopupClickListener(){@Overridepublic void onClickedPopup(int index) {}};pop = new PopupOverlay(mMapView,popListener);MyLocationMapView.pop = pop;}/*** 定位SDK監聽函數*/public class MyLocationListenner implements BDLocationListener {@Overridepublic void onReceiveLocation(BDLocation location) {if (location == null)return ;locData.latitude = location.getLatitude();locData.longitude = location.getLongitude();//如果不顯示定位精度圈,將accuracy賦值為0即可locData.accuracy = location.getRadius();// 此處可以設置 locData的方向信息, 如果定位 SDK 未返回方向信息,用戶可以自己實現羅盤功能添加方向信息。locData.direction = location.getDerect();//更新定位數據myLocationOverlay.setData(locData);//更新圖層數據執行刷新后生效mMapView.refresh();//是手動觸發請求或首次定位時,移動到定位點if (isRequest || isFirstLoc){//移動地圖到定位點//Log.d("LocationOverlay", "receive location, animate to it");mMapController.animateTo(new GeoPoint((int)(locData.latitude* 1e6), (int)(locData.longitude * 1e6)));isRequest = false;myLocationOverlay.setLocationMode(LocationMode.FOLLOWING);}//首次定位完成isFirstLoc = false;}public void onReceivePoi(BDLocation poiLocation) {if (poiLocation == null){return ;}}}//繼承MyLocationOverlay重寫dispatchTap實現點擊處理public class locationOverlay extends MyLocationOverlay{public locationOverlay(MapView mapView) {super(mapView);}@Overrideprotected boolean dispatchTap() {//處理點擊事件,彈出泡泡popupText.setBackgroundResource(R.drawable.popup);popupText.setText(mLocClient.getLastKnownLocation().getAddrStr());pop.showPopup(BMapUtil.getBitmapFromView(popupText),new GeoPoint((int)(locData.latitude*1e6), (int)(locData.longitude*1e6)),8);return true;}}@Overrideprotected void onPause() {mMapView.onPause();if(app.mBMapManager!=null){ app.mBMapManager.stop(); } super.onPause();}@Overrideprotected void onResume() {mMapView.onResume();if(app.mBMapManager!=null){ app.mBMapManager.start(); }super.onResume();}@Overrideprotected void onDestroy() {//退出時銷毀定位if (mLocClient != null)mLocClient.stop();mMapView.destroy();if(app.mBMapManager!=null){ app.mBMapManager.destroy(); app.mBMapManager=null; } super.onDestroy();}@Overrideprotected void onSaveInstanceState(Bundle outState) {super.onSaveInstanceState(outState);mMapView.onSaveInstanceState(outState); }@Overrideprotected void onRestoreInstanceState(Bundle savedInstanceState) {super.onRestoreInstanceState(savedInstanceState);mMapView.onRestoreInstanceState(savedInstanceState);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {return true;}}

MainActivity的布局文件如下:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" ><FrameLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent" ><com.jerehedu.ljb.MyLocationMapViewandroid:id="@+id/bmapView"android:layout_width="fill_parent"android:layout_height="fill_parent"android:clickable="true" /><RelativeLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_marginRight="10dp"android:layout_marginTop="10dp"android:background="@drawable/edit_bg_all" ><EditTextandroid:id="@+id/txtAddr"android:layout_width="fill_parent"android:layout_height="40dp"android:layout_centerVertical="true"android:layout_marginRight="2dp"android:layout_toLeftOf="@+id/btOk"android:background="@drawable/edit_bg_all"android:completionThreshold="2"android:drawableLeft="@drawable/qz_icon_seabar_search"android:hint="請輸入搜索關鍵字" /><TextViewandroid:id="@+id/btOk"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:layout_marginRight="2dp"android:layout_toLeftOf="@+id/s2"android:text="搜索"android:textSize="18sp" /><ImageViewandroid:id="@+id/s2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:layout_marginRight="2dp"android:layout_toLeftOf="@+id/btget"android:src="@drawable/slide_center" /><TextViewandroid:id="@+id/btget"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_centerVertical="true"android:text="定位"android:textSize="18sp" /></RelativeLayout></FrameLayout></RelativeLayout>

通過以上內容給大家分享了Android百度地圖實現搜索和定位及自定義圖標繪制并點擊時彈出泡泡的相關知識,希望對大家有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 明溪县| 密山市| 达拉特旗| 宜宾县| 宁安市| 福贡县| 延安市| 民勤县| 阆中市| 漳浦县| 车致| 镇平县| 东明县| 保靖县| 施甸县| 班玛县| 桃园县| 略阳县| 华宁县| 万安县| 公主岭市| 肇源县| 九江市| 郓城县| 宽城| 鹤山市| 丹东市| 兴安县| 光山县| 华池县| 平和县| 江津市| 寿阳县| 富裕县| 惠州市| 嘉兴市| 阿尔山市| 河北区| 泗水县| 沽源县| 东平县|