(四)高德地图之定位的几种模式

这一节主要实现的功能是地图定位的几种模式,包括展示、定位、追随、旋转、旋转位置、跟随不移动中心点、旋转不移动中心点、旋转位置不移动到中心点,我们根据实际需要来选择用那种模式。下面还是主要从代码中来体现,主要部分有注释。

还是先新建布局文件:activity_locationmodesource.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">

    <com.amap.api.maps.MapView
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </com.amap.api.maps.MapView>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <Button
            android:id="@+id/btn_show"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="展示"/>
        <Button
            android:id="@+id/btn_locate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="定位"/>
        <Button
            android:id="@+id/btn_follow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="追随"/>
        <Button
            android:id="@+id/btn_rotate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="旋转"/>
        <Button
            android:id="@+id/btn_rotate_location"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="旋转位置"/>
        <Button
            android:id="@+id/btn_follow_nocenter"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="跟随不移动中心点"/>
        <Button
            android:id="@+id/btn_rotate_nocenter"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="旋转不移动到中心点"/>
        <Button
            android:id="@+id/btn_rotate_location_nocenter"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="旋转位置不移动到中心点"/>
    </LinearLayout>

</RelativeLayout>

然后创建类文件LocationModeSource.java

package com.junto.gdmaptest.activity;

import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.amap.api.maps.AMap;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.MyLocationStyle;
import com.junto.gdmaptest.R;

/**
 * Created by WangJinyong on 2018/10/24.
 * 定位的几种模式
 */

public class LocationModeSourceActivity extends Activity implements View.OnClickListener,AMap.OnMyLocationChangeListener {

    MapView mapView;
    AMap aMap;
    Button btn_show,btn_locate,btn_follow,btn_rotate,btn_rotate_location,btn_follow_nocenter,btn_rotate_nocenter,btn_rotate_location_nocenter;
    private MyLocationStyle myLocationStyle;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activiy_locationmodesource);
        mapView = findViewById(R.id.map);
        mapView.onCreate(savedInstanceState);
        initView();
    }

    @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
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    private void initView(){
        if (aMap == null) {
            aMap = mapView.getMap();
            setUpMap();
        }

        btn_show = findViewById(R.id.btn_show);
        btn_show.setOnClickListener(this);
        btn_locate = findViewById(R.id.btn_locate);
        btn_locate.setOnClickListener(this);
        btn_follow = findViewById(R.id.btn_follow);
        btn_follow.setOnClickListener(this);
        btn_rotate = findViewById(R.id.btn_rotate);
        btn_rotate.setOnClickListener(this);
        btn_rotate_location = findViewById(R.id.btn_rotate_location);
        btn_rotate_location.setOnClickListener(this);
        btn_follow_nocenter = findViewById(R.id.btn_follow_nocenter);
        btn_follow_nocenter.setOnClickListener(this);
        btn_rotate_nocenter = findViewById(R.id.btn_rotate_nocenter);
        btn_rotate_nocenter.setOnClickListener(this);
        btn_rotate_location_nocenter = findViewById(R.id.btn_rotate_location_nocenter);
        btn_rotate_location_nocenter.setOnClickListener(this);

        //设置SDK 自带定位消息监听
        aMap.setOnMyLocationChangeListener(this);
    }

    /**
     * 设置一些amap的属性
     */
    private void setUpMap() {
        // 如果要设置定位的默认状态,可以在此处进行设置
        myLocationStyle = new MyLocationStyle();
        aMap.setMyLocationStyle(myLocationStyle);

        aMap.getUiSettings().setMyLocationButtonEnabled(true);// 设置默认定位按钮是否显示
        aMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_show://展示
                // 只定位,不进行其他操作
                aMap.setMyLocationStyle(myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_SHOW));
                break;
            case R.id.btn_locate://定位
                aMap.setMyLocationStyle(myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATE));
                break;
            case R.id.btn_follow://追随
                aMap.setMyLocationStyle(myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_FOLLOW));
                break;
            case R.id.btn_rotate://旋转
                aMap.setMyLocationStyle(myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_MAP_ROTATE));
                break;
            case R.id.btn_rotate_location://旋转位置
                aMap.setMyLocationStyle(myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE));
                break;
            case R.id.btn_follow_nocenter://跟随比移动中心点
                aMap.setMyLocationStyle(myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_FOLLOW_NO_CENTER));
                break;
            case R.id.btn_rotate_nocenter://旋转不移动到中心点
                aMap.setMyLocationStyle(myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_MAP_ROTATE_NO_CENTER));
                break;
            case R.id.btn_rotate_location_nocenter://旋转位置不移动到中心点
                aMap.setMyLocationStyle(myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE_NO_CENTER));
                break;
        }
    }

    @Override
    public void onMyLocationChange(Location location) {
        // 定位回调监听
        if (location != null){
            Log.e("amap", "onMyLocationChange 定位成功, lat: " + location.getLatitude() + " lon: " + location.getLongitude());
            Bundle bundle = location.getExtras();
            if (bundle != null){
                int errorCode = bundle.getInt(MyLocationStyle.ERROR_CODE);
                String errorInfo = bundle.getString(MyLocationStyle.ERROR_INFO);
                // 定位类型,可能为GPS WIFI等,具体可以参考官网的定位SDK介绍
                int locationType = bundle.getInt(MyLocationStyle.LOCATION_TYPE);
                Log.e("amap", "定位信息, code: " + errorCode + " errorInfo: " + errorInfo + " locationType: " + locationType );
            }else {
                Log.e("amap", "定位信息, bundle is null ");
            }
        }else {
            Log.e("amap", "定位失败");
        }
    }
}

上面就是实现地图定位几种模式的全部内容,根据需要来选择使用吧。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

时代新人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值