簡單使用百度地圖,顯示頭像
阿新 • • 發佈:2019-01-29
使用百度定位,修改的百度API demo。
在你的當前位置顯示你的頭像。
/** * 重寫application,驗證異常和網路 */ public class BaseApplication extends Application { private static BaseApplication mInstance = null; public boolean m_bKeyRight = true; BMapManager mBMapManager = null; // 輸入授權的key public static final String strKey = "A6e775781071f702061cb5f783f3f4ba"; @Override public 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( BaseApplication.getInstance().getApplicationContext(), "BMapManager 初始化錯誤!", Toast.LENGTH_LONG).show(); } } public static BaseApplication getInstance() { return mInstance; } // 常用事件監聽,用來處理通常的網路錯誤,授權驗證錯誤等 static class MyGeneralListener implements MKGeneralListener { @Override public void onGetNetworkState(int iError) { if (iError == MKEvent.ERROR_NETWORK_CONNECT) { Toast.makeText( BaseApplication.getInstance().getApplicationContext(), "您的網路出錯啦!", Toast.LENGTH_LONG).show(); } else if (iError == MKEvent.ERROR_NETWORK_DATA) { Toast.makeText( BaseApplication.getInstance().getApplicationContext(), "輸入正確的檢索條件!", Toast.LENGTH_LONG).show(); } // ... } @Override public void onGetPermissionState(int iError) { if (iError == MKEvent.ERROR_PERMISSION_DENIED) { // 授權Key錯誤: Toast.makeText( BaseApplication.getInstance().getApplicationContext(), "請在 DemoApplication.java檔案輸入正確的授權Key!", Toast.LENGTH_LONG).show(); BaseApplication.getInstance().m_bKeyRight = false; } } } }
圖片工具類:
/** * 得到bitmap 影象 */ public class BMapUtil { /** * 從view得到圖片 */ public static Bitmap getBitmapFromView(View view) { view.destroyDrawingCache(); view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec .makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); view.setDrawingCacheEnabled(true); Bitmap bitmap = view.getDrawingCache(true); return bitmap; } }
定位功能:
/** * 實現定位功能,定位到自己的當前的位置 */ public class MainActivity extends Activity implements LocationListener { private enum E_BUTTON_TYPE { LOC, COMPASS, FOLLOW } private E_BUTTON_TYPE mCurBtnType; // 定位相關 LocationClient mLocClient; LocationData locData = null; public MyLocationListenner myListener = new MyLocationListenner(); // 定點陣圖層 locationOverlay myLocationOverlay = null; // 彈出泡泡圖層 private PopupOverlay pop = null;// 彈出泡泡圖層,瀏覽節點時使用 private TextView popupText = null;// 泡泡view private View viewCache = null; // 地圖相關,使用繼承MapView的MyLocationMapView目的是重寫touch事件實現泡泡處理 // 如果不處理touch事件,則無需繼承,直接使用MapView即可 MyLocationMapView mMapView = null; // 地圖View private MapController mMapController = null; // UI相關 OnCheckedChangeListener radioButtonListener = null; Button requestLocButton = null; boolean isRequest = false;// 是否手動觸發請求定位 boolean isFirstLoc = true;// 是否首次定位 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); CharSequence titleLable = "定位功能"; setTitle(titleLable); requestLocButton = (Button) findViewById(R.id.button1); mCurBtnType = E_BUTTON_TYPE.LOC; OnClickListener btnClickListener = new OnClickListener() { public void onClick(View v) { switch (mCurBtnType) { case LOC: // 手動定位請求 requestLocClick(); break; case COMPASS: myLocationOverlay.setLocationMode(LocationMode.NORMAL); requestLocButton.setText("定位"); mCurBtnType = E_BUTTON_TYPE.LOC; break; case FOLLOW: myLocationOverlay.setLocationMode(LocationMode.COMPASS); requestLocButton.setText("羅盤"); mCurBtnType = E_BUTTON_TYPE.COMPASS; break; } } }; requestLocButton.setOnClickListener(btnClickListener); RadioGroup group = (RadioGroup) this.findViewById(R.id.radioGroup); radioButtonListener = new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { if (checkedId == R.id.defaulticon) { // 傳入null則,恢復預設圖示 modifyLocationOverlayIcon(null); } if (checkedId == R.id.customicon) { // 修改為自定義marker modifyLocationOverlayIcon(getResources().getDrawable( R.drawable.icon_geo)); } } }; group.setOnCheckedChangeListener(radioButtonListener); // 地圖初始化 mMapView = (MyLocationMapView) findViewById(R.id.bmapView); mMapController = mMapView.getController(); mMapView.getController().setZoom(14); mMapView.getController().enableClick(true); mMapView.setBuiltInZoomControls(true); // 建立 彈出泡泡圖層 createPaopao(); // 定位初始化 mLocClient = new LocationClient(this); locData = new LocationData(); mLocClient.registerLocationListener(myListener); LocationClientOption option = new LocationClientOption(); option.setOpenGps(true);// 開啟gps 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(); } /** * 修改位置圖示 * * @param marker */ public void modifyLocationOverlayIcon(Drawable marker) { // 當傳入marker為null時,使用預設圖示繪製 myLocationOverlay.setMarker(marker); // 修改圖層,需要重新整理MapView生效 mMapView.refresh(); } /** * 建立彈出泡泡圖層 */ public void createPaopao() { viewCache = getLayoutInflater() .inflate(R.layout.custom_text_view, null); popupText = (TextView) viewCache.findViewById(R.id.textcache); // 泡泡點選響應回撥 PopupClickListener popListener = new PopupClickListener() { @Override public void onClickedPopup(int index) { Log.v("click", "clickapoapo"); } }; pop = new PopupOverlay(mMapView, popListener); MyLocationMapView.pop = pop; } /** * 定位SDK監聽函式 */ public class MyLocationListenner implements BDLocationListener { private double OLati; private double OLong; private double Length; @Override public 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); requestLocButton.setText("跟隨"); mCurBtnType = E_BUTTON_TYPE.FOLLOW; popupText.setBackgroundResource(R.drawable.nanshengduanfa); popupText.setText("蒲錦"); pop.showPopup(BMapUtil.getBitmapFromView(popupText), new GeoPoint((int) (locData.latitude * 1e6), (int) (locData.longitude * 1e6)), 8); } // 首次定位完成 isFirstLoc = false; } public void onReceivePoi(BDLocation poiLocation) { if (poiLocation == null) { return; } } } // 繼承MyLocationOverlay重寫dispatchTap實現點選處理 public class locationOverlay extends MyLocationOverlay { public locationOverlay(MapView mapView) { super(mapView); // TODO Auto-generated constructor stub } @Override protected boolean dispatchTap() { // TODO Auto-generated method stub // 處理點選事件,彈出泡泡 // popupText.setBackgroundResource(R.drawable.popup); // popupText.setText("我的位置"); // pop.showPopup(BMapUtil.getBitmapFromView(popupText), new GeoPoint( // (int) (locData.latitude * 1e6), // (int) (locData.longitude * 1e6)), 8); return false; } } @Override protected void onPause() { mMapView.onPause(); super.onPause(); } @Override protected void onResume() { mMapView.onResume(); super.onResume(); } @Override protected void onDestroy() { // 退出時銷燬定位 if (mLocClient != null) mLocClient.stop(); mMapView.destroy(); super.onDestroy(); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); mMapView.onSaveInstanceState(outState); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); mMapView.onRestoreInstanceState(savedInstanceState); } @Override public boolean onCreateOptionsMenu(Menu menu) { // getMenuInflater().inflate(R.menu.activity_main, menu); return true; } /** * 當位置發生變化的時候,觸發該方法 */ @Override public void onLocationChanged(Location location) { // TODO Auto-generated method stub } @Override public void onProviderDisabled(String provider) { // TODO Auto-generated method stub } @Override public void onProviderEnabled(String provider) { // TODO Auto-generated method stub } @Override public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } } /** * 繼承MapView重寫onTouchEvent實現泡泡處理操作 * * @author hejin * */ class MyLocationMapView extends MapView { static PopupOverlay pop = null;// 彈出泡泡圖層,點選圖示使用 public MyLocationMapView(Context context) { super(context); // TODO Auto-generated constructor stub } public MyLocationMapView(Context context, AttributeSet attrs) { super(context, attrs); } public MyLocationMapView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public boolean onTouchEvent(MotionEvent event) { if (!super.onTouchEvent(event)) { // 消隱泡泡 if (pop != null && event.getAction() == MotionEvent.ACTION_UP) { // pop.hidePop(); } } return true; } }
效果:
注意:
1.mapview 寫全路徑,才能顯示地圖圖層。因為mylocationmapview是在locationoverlaydemo中定義的內部類。
2.
這個錯誤是沒有配置好清單中的name:
<application
android:name="com.example.testmap.BaseApplication"
android:allowBackup="true"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
name 為重寫的application的路徑。
3.百度地圖的亂七八糟的許可權太多了。就直接copy上去吧,我改了幾個認為不需要的許可權都報錯,傷不起啊。
4.要定位的話,還得新增定位服務,這個是封裝在.jar裡面的。你在清單檔案中新增起,就可以了。
<service
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote" >
</service>
5.申請授權key的時候,應用名稱是:
string.xml中的"app_name"對應的字串。
6.已存在服務異常:
原來是跳轉的時候,寫錯了跳轉到了定位服務,沒跳轉到活動造成的異常。