演示Android百度地圖操作功能

jopen 12年前發布 | 44K 次閱讀 百度地圖 Android開發 移動開發

在本文中將演示百度地圖的操作功能,包括縮放,旋轉,視角切換,點擊,雙擊,長按事件觸發的操作以及截圖等。百度地圖本來就內置有縮放,旋轉功能,那么在這里,截圖(其實很多手機也自帶截圖功能)以及點擊事件的監聽算是比較有實際意義的功能。代碼原型來源百度demo:

Activity:

package com.home;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.map.MKMapTouchListener;
import com.baidu.mapapi.map.MKMapViewListener;
import com.baidu.mapapi.map.MapController;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapView;
import com.baidu.platform.comapi.basestruct.GeoPoint;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class ControlBMapActivity extends Activity {
    /**
     * MapView 是地圖主控件
     */
    private MapView mMapView = null;
    /**
     * 用MapController完成地圖控制
     */
    private MapController mMapController = null;
    /**
     * MKMapViewListener 用于處理地圖事件回調
     */
    MKMapViewListener mMapListener = null;
    /**
     * 用于截獲屏坐標
     */
    MKMapTouchListener mapTouchListener = null;
    /**
     * 當前地點擊點
     */
    private GeoPoint currentPt = null;
    /**
     * 控制按鈕
     */
    private Button zoomButton = null;
    private Button rotateButton = null;
    private Button overlookButton = null;
    private Button saveScreenButton = null;
    private String touchType = null;
    /**
     * 用于顯示地圖狀態的面板
     */
    private TextView mStateTextView = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DemoApplication app = (DemoApplication) this.getApplication();
        if (app.mBMapManager == null) {
            app.mBMapManager = new BMapManager(this);
            /**
             * 如果BMapManager沒有初始化則初始化BMapManager
             */
            app.mBMapManager.init(DemoApplication.strKey,
                    new DemoApplication.MyGeneralListener());
        }
        setContentView(R.layout.control_main);
        mMapView = (MapView) findViewById(R.id.bmapView);
        /**
         * 獲取地圖控制器
         */
        mMapController = mMapView.getController();
        /**
         * 設置地圖是否響應點擊事件 .
         */
        mMapController.enableClick(true);
        /**
         * 設置地圖縮放級別
         */
        mMapController.setZoom(12);

        mStateTextView = (TextView) findViewById(R.id.state);
        /**
         * 初始化地圖事件監聽
         */
        initListener();
        /**
         * 將地圖默認移動至天安門
         */
        double cLat = 39.945;
        double cLon = 116.404;
        GeoPoint p = new GeoPoint((int) (cLat * 1E6), (int) (cLon * 1E6));
        mMapController.setCenter(p);
    }

    private void initListener() {
        /**
         * 設置地圖點擊事件監聽
         */
        mapTouchListener = new MKMapTouchListener() {
            @Override
            public void onMapClick(GeoPoint point) {
                touchType = "單擊";
                currentPt = point;
                updateMapState();

            }

            @Override
            public void onMapDoubleClick(GeoPoint point) {
                touchType = "雙擊";
                currentPt = point;
                updateMapState();
            }

            @Override
            public void onMapLongClick(GeoPoint point) {
                touchType = "長按";
                currentPt = point;
                updateMapState();
            }
        };
        mMapView.regMapTouchListner(mapTouchListener);
        /**
         * 設置地圖事件監聽
         */
        mMapListener = new MKMapViewListener() {
            @Override
            public void onMapMoveFinish() {
                /**
                 * 在此處理地圖移動完成回調 縮放,平移等操作完成后,此回調被觸發
                 */
                updateMapState();
            }

            @Override
            public void onClickMapPoi(MapPoi mapPoiInfo) {
                /**
                 * 在此處理底圖poi點擊事件 顯示底圖poi名稱并移動至該點 設置過:
                 * mMapController.enableClick(true); 時,此回調才能被觸發
                 * 
                 */

            }

            @Override
            public void onGetCurrentMap(Bitmap b) {
                /**
                 * 當調用過 mMapView.getCurrentMap()后,此回調會被觸發 可在此保存截圖至存儲設備
                 * 
                 */
                if (!Environment.MEDIA_MOUNTED.equals(Environment
                        .getExternalStorageState())) {
                    Toast.makeText(ControlBMapActivity.this, "請插入SD卡",
                            Toast.LENGTH_SHORT).show();
                    return;
                }
                File file = new File(Environment.getExternalStorageDirectory(),
                        System.currentTimeMillis() + ".png");
                FileOutputStream out;
                try {
                    out = new FileOutputStream(file);
                    if (b.compress(Bitmap.CompressFormat.PNG, 70, out)) {
                        out.flush();
                        out.close();
                    }
                    Toast.makeText(ControlBMapActivity.this,
                            "屏幕截圖成功,圖片存在: " + file.toString(),
                            Toast.LENGTH_SHORT).show();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onMapAnimationFinish() {
                /**
                 * 地圖完成帶動畫的操作(如: animationTo())后,此回調被觸發
                 */
                updateMapState();
            }

            @Override
            public void onMapLoadFinish() {
            }
        };
        mMapView.regMapViewListener(DemoApplication.getInstance().mBMapManager,
                mMapListener);
        /**
         * 設置按鍵監聽
         */
        zoomButton = (Button) findViewById(R.id.zoombutton);
        rotateButton = (Button) findViewById(R.id.rotatebutton);
        overlookButton = (Button) findViewById(R.id.overlookbutton);
        saveScreenButton = (Button) findViewById(R.id.savescreen);
        OnClickListener onClickListener = new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (view.equals(zoomButton)) {
                    perfomZoom();
                } else if (view.equals(rotateButton)) {
                    perfomRotate();
                } else if (view.equals(overlookButton)) {
                    perfomOverlook();
                } else if (view.equals(saveScreenButton)) {
                    // 截圖,在MKMapViewListener中保存圖片
                    mMapView.getCurrentMap();
                    Toast.makeText(ControlBMapActivity.this, "正在截取屏幕圖片...",
                            Toast.LENGTH_SHORT).show();

                }
                updateMapState();
            }

        };
        zoomButton.setOnClickListener(onClickListener);
        rotateButton.setOnClickListener(onClickListener);
        overlookButton.setOnClickListener(onClickListener);
        saveScreenButton.setOnClickListener(onClickListener);
    }

    /**
     * 處理縮放,sdk縮放級別范圍:[3.0,19.0],小于3.0將按3.0處理;大于19.0將按19.0處理
     */
    private void perfomZoom() {
        EditText t = (EditText) findViewById(R.id.zoomlevel);
        try {
            float zoomLevel = Float.parseFloat(t.getText().toString());
            mMapController.setZoom(zoomLevel);
        } catch (NumberFormatException e) {
            Toast.makeText(this, "請輸入正確的縮放級別,范圍: [3.0,19.0],只能輸入浮點型或整型",
                    Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 處理旋轉 ,旋轉角單位:度 ,逆時針旋轉
     */
    private void perfomRotate() {
        EditText t = (EditText) findViewById(R.id.rotateangle);
        try {
            int rotateAngle = Integer.parseInt(t.getText().toString());
            mMapController.setRotation(rotateAngle);
        } catch (NumberFormatException e) {
            Toast.makeText(this, "請輸入正確的旋轉角度,只能為整型", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 處理俯視 俯角范圍: -45 ~ 0 , 單位: 度
     */
    private void perfomOverlook() {
        EditText t = (EditText) findViewById(R.id.overlookangle);
        try {
            int overlookAngle = Integer.parseInt(t.getText().toString());
            mMapController.setOverlooking(overlookAngle);
        } catch (NumberFormatException e) {
            Toast.makeText(this, "請輸入正確的俯角,范圍:  -45 ~ 0", Toast.LENGTH_SHORT)
                    .show();
        }
    }

    /**
     * 更新地圖狀態顯示面板
     */
    private void updateMapState() {
        if (mStateTextView == null) {
            return;
        }
        String state = "";
        if (currentPt == null) {
            state = "點擊、長按、雙擊地圖以獲取經緯度和地圖狀態";
        } else {
            state = String.format(touchType + ",當前經度 : %f 當前緯度:%f",
                    currentPt.getLongitudeE6() * 1E-6,
                    currentPt.getLatitudeE6() * 1E-6);
        }
        state += "\n";
        state += String
                .format("zoom level= %.1f    rotate angle= %d   overlaylook angle=  %d",
                        mMapView.getZoomLevel(), mMapView.getMapRotation(),
                        mMapView.getMapOverlooking());
        mStateTextView.setText(state);
    }

    @Override
    protected void onPause() {
        /**
         * MapView的生命周期與Activity同步,當activity掛起時需調用MapView.onPause()
         */
        mMapView.onPause();
        super.onPause();
    }

    @Override
    protected void onResume() {
        /**
         * MapView的生命周期與Activity同步,當activity恢復時需調用MapView.onResume()
         */
        mMapView.onResume();
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        /**
         * MapView的生命周期與Activity同步,當activity銷毀時需調用MapView.destroy()
         */
        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);
    }
}

布局XML:

<?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"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/layout_bottom"
        android:layout_width="match_parent"
        android:layout_height="50dip"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/zoombutton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="縮放" />

        <EditText
            android:id="@+id/zoomlevel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="10" />

        <Button
            android:id="@+id/rotatebutton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="旋轉" />

        <EditText
            android:id="@+id/rotateangle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:numeric="integer"
            android:text="90" />

        <Button
            android:id="@+id/overlookbutton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="俯視" />

        <EditText
            android:id="@+id/overlookangle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="-30" />
    </LinearLayout>

    <TextView
        android:id="@+id/state"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/layout_bottom"
        android:text="點擊、長按、雙擊地圖以獲取經緯度和地圖狀態" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/state"
        android:orientation="vertical" >

        <com.baidu.mapapi.map.MapView
            android:id="@+id/bmapView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clickable="true" />

        <Button
            android:id="@+id/savescreen"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="10dip"
            android:text="截圖" />
    </RelativeLayout>

</RelativeLayout>

Application類及Manifest同上文。

附上圖片效果:

演示Android百度地圖操作功能

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!