(三)高德地图之自定义缩放及缩放动画效果

尉迟韬
2023-12-01

这一节主要实现的功能是地图的自定义缩放及缩放的动画效果,还是直接放上代码更直观些,主要部位里面基本有注解

还是老样子,首先是新建activity_zoom_animate.xml布局文件

<?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" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

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

            <CheckBox
                android:id="@+id/animate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:textOff="动画"
                android:textOn="动画" />
        </LinearLayout>



        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:padding="5dp"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/zoom_in"
                android:layout_width="40dip"
                android:layout_height="40dip"
                android:layout_alignParentLeft="true"
                android:textSize="20sp"
                android:gravity="center"
                android:text="+" />

            <Button
                android:id="@+id/zoom_out"
                android:layout_width="40dip"
                android:layout_height="40dip"
                android:layout_toRightOf="@id/zoom_in"
                android:layout_marginLeft="5dp"
                android:textSize="20sp"
                android:gravity="center"
                android:text="-" />
        </RelativeLayout>
    </RelativeLayout>

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

</LinearLayout>

然后是创建类文件Zoom_Animate.java

package com.junto.gdmaptest.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;

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.junto.gdmaptest.R;

/**
 * Created by WangJinyong on 2018/10/22.
 * 地图自定义缩放及缩放动画效果
 */

public class Zoom_AnimateActivity extends Activity implements View.OnClickListener {

    MapView mapView = null;
    AMap aMap;
    CheckBox animate;//是否添加动画
    Button zoomIn,zoomOut;//+  -

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zoom_animate);
        //获取地图控件引用
        mapView = findViewById(R.id.map);
        //在activity执行onCreat时执行mapView.onCreate(savedInstanceState),创建地图,必需要写的
        mapView.onCreate(savedInstanceState);
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume (),重新绘制加载地图
        mapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView.onPause (),暂停地图的绘制
        mapView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy(),销毁地图
        mapView.onDestroy();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState),保存地图当前的状态
        mapView.onSaveInstanceState(outState);
    }

    private void initView() {
        //初始化AMap对象
        if (aMap == null) {
            aMap = mapView.getMap();
        }

        animate = findViewById(R.id.animate);//是否添加动画
        animate.setOnClickListener(this);
        zoomIn = findViewById(R.id.zoom_in);//+
        zoomIn.setOnClickListener(this);
        zoomOut = findViewById(R.id.zoom_out);//-
        zoomOut.setOnClickListener(this);
    }

    /**
     * 根据动画按钮状态,调用函数animateCamera或moveCamera来改变可视区域
     */
    private void changeCamera(CameraUpdate update, AMap.CancelableCallback callback) {
        boolean animated = animate.isChecked();
        if (animated) {
            aMap.animateCamera(update, 1000, callback);//1000是动画时间
        } else {
            aMap.moveCamera(update);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.zoom_in://点击地图放大按钮(+)响应事件
                changeCamera(CameraUpdateFactory.zoomIn(), null);
                break;
            case R.id.zoom_out://点击地图缩小按钮(-)响应事件
                changeCamera(CameraUpdateFactory.zoomOut(), null);
                break;
        }
    }
}

通过上面的代码就能实现地图的自定义缩放和缩放的动画效果了,这些都是可控的。

下一节再接着介绍地图定位的几种模式

 类似资料: