1. 程式人生 > >最新基於高德地圖的android進階開發(5)地圖的基本操作、事件監聽、使用者UI、圖層選擇等

最新基於高德地圖的android進階開發(5)地圖的基本操作、事件監聽、使用者UI、圖層選擇等

1.高德地圖的基本操作:最簡單的莫過於第一次載入地圖

佈局檔案:basic_map.xml,在下面的操作中,未作特別說明都採用此佈局檔案。

<?xml version="1.0" encoding="utf-8"?>
<com.amap.api.maps.MapView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
>
</com.amap.api.maps.MapView>

基本操作:第一次載入地圖。

package com.dragon.arnav.basicFuction.basic;

import android.app.Activity;
import android.os.Bundle;

import com.amap.api.maps.AMap;
import com.amap.api.maps.MapView;
import com.dragon.arnav.R;

/**
 * This file created by dragon on 2016/7/28 21:26,belong to com.dragon.arnav.basicFuction.basic .
 */
public class basicMap extends Activity { private MapView mapView; private AMap aMap; @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); //採用這種方式或是下面注視的動態載入都是可以的 setContentView(R.layout.basic_map); mapView = (MapView) findViewById(R.id.map); // mapView = new MapView(this);
// setContentView(mapView); mapView.onCreate(savedInstanceState); init(); } private void init(){ if(aMap == null){ aMap = mapView.getMap(); } } @Override protected void onResume(){ super.onResume(); mapView.onResume(); } @Override protected void onPause() { super.onPause(); mapView.onPause(); } @Override protected void onDestroy(){ super.onDestroy(); mapView.onDestroy(); } @Override public void onSaveInstanceState(Bundle outState){ super.onSaveInstanceState(outState); mapView.onSaveInstanceState(outState); } }

2.camera檢視實戰,包括移動、動畫檢視、停止等,來自官方的demo

佈局檔案:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <Button
                android:id="@+id/stop_animation"
                android:layout_width="40dip"
                android:layout_height="40dip"
                android:text="@string/stop_animation" />

            <ToggleButton
                android:id="@+id/animate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:textOff="@string/animate"
                android:textOn="@string/animate" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <Button
                android:id="@+id/Lujiazui"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/go_to_lujiazui" />

            <Button
                android:id="@+id/Zhongguancun"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/go_to_zhongguancun" />
        </LinearLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center_horizontal" >

            <Button
                android:id="@+id/scroll_left"
                android:layout_width="40dip"
                android:layout_height="40dip"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:text="@string/left_arrow" />

            <Button
                android:id="@+id/scroll_up"
                android:layout_width="40dip"
                android:layout_height="40dip"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@id/scroll_left"
                android:text="@string/up_arrow" />

            <Button
                android:id="@+id/scroll_down"
                android:layout_width="40dip"
                android:layout_height="40dip"
                android:layout_below="@id/scroll_up"
                android:layout_toRightOf="@id/scroll_left"
                android:text="@string/down_arrow" />

            <Button
                android:id="@+id/scroll_right"
                android:layout_width="40dip"
                android:layout_height="40dip"
                android:layout_centerVertical="true"
                android:layout_toRightOf="@id/scroll_down"
                android:text="@string/right_arrow" />
        </RelativeLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="right"
            android:orientation="vertical" >

            <Button
                android:id="@+id/zoom_in"
                android:layout_width="40dip"
                android:layout_height="40dip"
                android:text="@string/zoom_in" />

            <Button
                android:id="@+id/zoom_out"
                android:layout_width="40dip"
                android:layout_height="40dip"
                android:text="@string/zoom_out" />
        </LinearLayout>
    </LinearLayout>

    <com.amap.api.maps.MapView
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

核心程式碼:

package com.dragon.arnav.basicFuction.basic;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ToggleButton;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.MarkerOptions;
import com.dragon.arnav.R;
import com.dragon.arnav.basicFuction.util.Constants;
import com.dragon.arnav.basicFuction.util.ToastUtil;

/**
 * This file created by dragon on 2016/7/28 21:26,belong to com.dragon.arnav.basicFuction.basic .
 */
public class basicMap extends Activity implements View.OnClickListener,AMap.CancelableCallback {
    private MapView mapView;
    private AMap aMap;
//    上下左右一次的移動畫素點
    private static final int SCROLL_BY_PX = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.basic_map);

        mapView = (MapView) findViewById(R.id.map);
        mapView.onCreate(savedInstanceState);
        init();
    }
    private void init(){
        if(aMap == null){
            aMap = mapView.getMap();
        }
//        各種事件的監聽方式
        Button stopAnimation = (Button)findViewById(R.id.stop_animation);
        stopAnimation.setOnClickListener(this);

        ToggleButton animate = (ToggleButton)findViewById(R.id.animate);
        animate.setOnClickListener(this);

        Button Lujiazui = (Button) findViewById(R.id.Lujiazui);
        Lujiazui.setOnClickListener(this);

        Button Zhongguancun = (Button) findViewById(R.id.Zhongguancun);
        Zhongguancun.setOnClickListener(this);

        Button scrollLeft = (Button)findViewById(R.id.scroll_left);
        scrollLeft.setOnClickListener(this);

        Button scrollRight = (Button)findViewById(R.id.scroll_right);
        scrollRight.setOnClickListener(this);

        Button scrollUp = (Button)findViewById(R.id.scroll_up);
        scrollUp.setOnClickListener(this);

        Button scrollDown = (Button) findViewById(R.id.scroll_down);
        scrollDown.setOnClickListener(this);

        Button zoomIn = (Button) findViewById(R.id.zoom_in);
        zoomIn.setOnClickListener(this);

        Button zoomOut = (Button)findViewById(R.id.zoom_out);
        zoomOut.setOnClickListener(this);
    }

    @Override
    protected void onResume(){
        super.onResume();
        mapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }
    @Override
    protected void onDestroy(){
        super.onDestroy();
        mapView.onDestroy();
    }
    @Override
    public void onSaveInstanceState(Bundle outState){
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }
//    根據動畫按鈕的狀態來選擇不同的camera來改變可視區域
//    AMap.CanclelableCallback:當一個任務完成或關閉時的回撥介面。
    private void changeCamera(CameraUpdate update, AMap.CancelableCallback callback){
        boolean animated = ((CompoundButton)findViewById(R.id.animate)).isChecked();
        if(animated){
//在指定的持續時間內,動畫地移動地圖到指定的位置,完成時呼叫可選的回撥方法。 如果運動過程中呼叫getCameraPosition(),它將返回可視區域飛行中的當前位置。
//            第一個引數:定義轉換的時間,第二個引數:運動的持續時間,第三個引數:回撥函式
            aMap.animateCamera(update,1000,callback);
        }else{
//            照傳入的CameraUpdate 引數移動可視區域。這個方法為瞬間移動,沒有移動過程,如果在呼叫此方法後再呼叫getCameraPosition()將返回移動後位置。
            aMap.moveCamera(update);
        }
    }

    @Override
    public void onClick(View v){
        switch(v.getId()){
            /**
             * 點選停止動畫按鈕響應事件
             */
            case R.id.stop_animation:
                aMap.stopAnimation();
                break;
            /**
             * 點選“去中關村”按鈕響應事件
             */
            case R.id.Zhongguancun:
                changeCamera(
                        CameraUpdateFactory.newCameraPosition(new CameraPosition(
                                Constants.ZHONGGUANCUN, 18, 0, 30)), null);
                break;

            /**
             * 點選“去陸家嘴”按鈕響應事件
             */
            case R.id.Lujiazui:
                changeCamera(
                        CameraUpdateFactory.newCameraPosition(new CameraPosition(
                                Constants.SHANGHAI, 18, 30, 0)), this);
                break;
            /**
             * 點擊向左移動按鈕響應事件,camera將向左邊移動
             */
            case R.id.scroll_left:
                changeCamera(CameraUpdateFactory.scrollBy(-SCROLL_BY_PX, 0), null);
                break;
            /**
             * 點擊向右移動按鈕響應事件,camera將向右邊移動
             */
            case R.id.scroll_right:
                changeCamera(CameraUpdateFactory.scrollBy(SCROLL_BY_PX, 0), null);
                break;
            /**
             * 點擊向上移動按鈕響應事件,camera將向上邊移動
             */
            case R.id.scroll_up:
                changeCamera(CameraUpdateFactory.scrollBy(0, -SCROLL_BY_PX), null);
                break;
            /**
             * 點擊向下移動按鈕響應事件,camera將向下邊移動
             */
            case R.id.scroll_down:
                changeCamera(CameraUpdateFactory.scrollBy(0, SCROLL_BY_PX), null);
                break;
            /**
             * 點選地圖放大按鈕響應事件
             */
            case R.id.zoom_in:
                changeCamera(CameraUpdateFactory.zoomIn(), null);
                break;
            /**
             * 點選地圖縮小按鈕響應事件
             */
            case R.id.zoom_out:
                changeCamera(CameraUpdateFactory.zoomOut(), null);
                break;
            default:
                break;
        }
    }
    /**
     * 地圖動畫效果終止回撥方法
     */
    @Override
    public void onCancel() {
        ToastUtil.show(basicMap.this, "Animation to 陸家嘴 canceled");
    }

    /**
     * 地圖動畫效果完成回撥方法
     */
    @Override
    public void onFinish() {
        ToastUtil.show(basicMap.this, "Animation to 陸家嘴 complete");
    }

}

效果圖

這裡寫圖片描述

3.事件監聽,主要包括單擊,長按,觸控等事件

佈局檔案

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tap_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/tap_instructions" />

    <TextView
        android:id="@+id/camera_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/move_the_camera" />
    <TextView
        android:id="@+id/touch_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/touch_the_map" />
    <com.amap.api.maps.MapView
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

核心程式碼(來自官方demo)

package com.amap.map3d.demo.basic;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.TextView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.AMap.OnCameraChangeListener;
import com.amap.api.maps.AMap.OnMapClickListener;
import com.amap.api.maps.AMap.OnMapLongClickListener;
import com.amap.api.maps.AMap.OnMapTouchListener;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.LatLngBounds;
import com.amap.api.maps.model.VisibleRegion;
import com.amap.map3d.demo.util.Constants;
import com.amap.map3d.demo.util.ToastUtil;
import com.amap.map3d.demo.R;

/**
 * AMapV2地圖中簡單介紹OnMapClickListener, OnMapLongClickListener,
 * OnCameraChangeListener三種監聽器用法
 */

public class EventsActivity extends Activity implements OnMapClickListener,
        OnMapLongClickListener, OnCameraChangeListener, OnMapTouchListener {
    private AMap aMap;
    private MapView mapView;
    private TextView mTapTextView;
    private TextView mCameraTextView;
    private TextView mTouchTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.events_activity);
        mapView = (MapView) findViewById(R.id.map);
        mapView.onCreate(savedInstanceState);// 此方法必須重寫
        init();
    }

    /**
     * 初始化AMap物件
     */
    private void init() {
        if (aMap == null) {
            aMap = mapView.getMap();
            setUpMap();
        }
        mTapTextView = (TextView) findViewById(R.id.tap_text);
        mCameraTextView = (TextView) findViewById(R.id.camera_text);
        mTouchTextView = (TextView) findViewById(R.id.touch_text);
    }

    /**
     * amap新增一些事件監聽器
     */
    private void setUpMap() {

        aMap.setOnMapClickListener(this);// 對amap新增單擊地圖事件監聽器
        aMap.setOnMapLongClickListener(this);// 對amap新增長按地圖事件監聽器
        aMap.setOnCameraChangeListener(this);// 對amap新增移動地圖事件監聽器
        aMap.setOnMapTouchListener(this);// 對amap新增觸控地圖事件監聽器
    }

    /**
     * 方法必須重寫
     */
    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    /**
     * 方法必須重寫
     */
    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    /**
     * 方法必須重寫
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    /**
     * 方法必須重寫
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    /**
     * 對單擊地圖事件回撥
     */
    @Override
    public void onMapClick(LatLng point) {
        mTapTextView.setText("tapped, point=" + point);
    }

    /**
     * 對長按地圖事件回撥
     */
    @Override
    public void onMapLongClick(LatLng point) {
        mTapTextView.setText("long pressed, point=" + point);
    }

    /**
     * 對正在移動地圖事件回撥
     */
    @Override
    public void onCameraChange(CameraPosition cameraPosition) {
        mCameraTextView.setText("onCameraChange:" + cameraPosition.toString());
    }

    /**
     * 對移動地圖結束事件回撥
     */
    @Override
    public void onCameraChangeFinish(CameraPosition cameraPosition) {
        mCameraTextView.setText("onCameraChangeFinish:"
                + cameraPosition.toString());
        VisibleRegion visibleRegion = aMap.getProjection().getVisibleRegion(); // 獲取可視區域、

        LatLngBounds latLngBounds = visibleRegion.latLngBounds;// 獲取可視區域的Bounds
        boolean isContain = latLngBounds.contains(Constants.SHANGHAI);// 判斷上海經緯度是否包括在當前地圖可見區域
        if (isContain) {
            ToastUtil.show(EventsActivity.this, "上海市在地圖當前可見區域內");
        } else {
            ToastUtil.show(EventsActivity.this, "上海市超出地圖當前可見區域");
        }
    }

    /**
     * 對觸控地圖事件回撥
     */
    @Override
    public void onTouch(MotionEvent event) {

        mTouchTextView.setText("觸控事件:螢幕位置" + event.getX() + " " + event.getY());
    }
}