安卓开发高德地图实现定位简单优化

很多小伙伴对高德地图开发应该很熟悉,毕竟高德文档已经说得很清楚了,关于如何接入和配置我这里就不在细说了。下面给大家讲一下实现定位的简单优化

一、AndroidManifest.xml文件




    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    
        
        
            
                

                
            
        
        
        
    


二、layout布局代码




    



上面的配置都是按官方文档来,下面的就是不同的,也就是简单优化的

三、首先声明一个接口MapUtilsInter,继承LocationSource和AMapLocationListener这两个接口,主要负责设置定位资源和监听。

/**
 * Created by Andream on 2018/12/10/010.
 * 高德地图配置定位接口接口
 */

public interface MapUtilsInter extends LocationSource, AMapLocationListener {
    /**
     * 设置定义样式
     * @return MyLocationStyle
     */
    MyLocationStyle getMyLocationStyle();

    /**
     * activity销毁时销毁资源
     */
    void destroy();
}

四、定义MapUtils类实现MapUtilsInter以及接口的方法,主要看注释

**
 * Created by Andream on 2018/12/9/009.
 * 高德地图定位参数设置
 */

public class MapUtils implements MapUtilsInter{
    private AMapLocationClient mLocationClient;
    private LocationSource.OnLocationChangedListener mListener;
    private Context context;

    public MapUtils(Context context) {
        this.context = context;
    }

    public  MyLocationStyle getMyLocationStyle(){
        MyLocationStyle myLocationStyle;
        myLocationStyle = new MyLocationStyle();//初始化定位蓝点样式类myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);//连续定位、且将视角移动到地图中心点,定位点依照设备方向旋转,并且会跟随设备移动。(1秒1次定位)如果不设置myLocationType,默认也会执行此种模式。
        myLocationStyle.interval(2000); //设置连续定位模式下的定位间隔,只在连续定位模式下生效,单次定位模式下不会生效。单位为毫秒。
        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_FOLLOW);
        myLocationStyle.radiusFillColor(0x00000000);
        myLocationStyle.strokeColor(0x00000000);
        return myLocationStyle;
    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if (mListener != null && aMapLocation != null) {
            if (aMapLocation.getErrorCode() == 0) {
               //在这里获取定位参数,如经纬度等
                mListener.onLocationChanged(aMapLocation);// 显示系统小蓝点
            } else {
                String errText = "定位失败," + aMapLocation.getErrorCode() + ": " + aMapLocation.getErrorInfo();
                Log.e("AmapErr", errText);
            }
        }
    }

    @Override
    public void activate(OnLocationChangedListener onLocationChangedListener) {
        mListener = onLocationChangedListener;
        if (mLocationClient == null) {
            mLocationClient = new AMapLocationClient(context);
            AMapLocationClientOption mLocationOption = new AMapLocationClientOption();
            //设置定位监听
            mLocationClient.setLocationListener(this);
            //设置为高精度定位模式
            mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
            //设置定位参数
            mLocationClient.setLocationOption(mLocationOption);
            // 此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗,
            // 注意设置合适的定位时间的间隔(最小间隔支持为2000ms),并且在合适时间调用stopLocation()方法来取消定位请求
            // 在定位结束后,在合适的生命周期调用onDestroy()方法
            // 在单次定位情况下,定位无论成功与否,都无需调用stopLocation()方法移除请求,定位sdk内部会移除
            mLocationClient.startLocation();
        }
    }

    @Override
    public void deactivate() {
        mListener = null;
        if (mLocationClient != null) {
            mLocationClient.stopLocation();
            mLocationClient.onDestroy();
        }
        mLocationClient = null;
    }

    /**
     * 退出销毁资源
     */
    public void destroy(){
        mListener = null;
        if (mLocationClient != null) {
            mLocationClient.stopLocation();
            mLocationClient.onDestroy();
        }
        mLocationClient = null;
    }
}

六、activity实现,主要采用MVC模式,调用MapUtilsInter实现方式是多态

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
    private MapView mMapView = null;
    private AMap aMap;
    private MapUtilsInter mapUtils = new MapUtils(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initPermission();
        //获取地图控件引用
        //在activity执行onCreate时执行mMapView.onCreate(savedInstanceState),创建地图
        mMapView = findViewById(R.id.map);
        mMapView.onCreate(savedInstanceState);
        aMap = mMapView.getMap();
        aMap.moveCamera(CameraUpdateFactory.zoomTo(16));//设置大小
        initMap();
    }

    /**
     * 定位初始化
     */
    private void initMap() {
        aMap.setMyLocationStyle(mapUtils.getMyLocationStyle());//设置定位蓝点的Style
        aMap.setLocationSource(mapUtils);
        aMap.getUiSettings().setMyLocationButtonEnabled(true);//设置默认定位按钮是否显示,非必需设置。
        aMap.setMyLocationEnabled(true);// 设置为true表示启动显示定位蓝点,false表示隐藏定位蓝点并不进行定位,默认是false。
    }

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

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

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

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

    /**
     * android 6.0 以上需要动态申请权限
     */
    private void initPermission() {
        String[] permissions = {
                Manifest.permission.VIBRATE,
                Manifest.permission.WAKE_LOCK,
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.CHANGE_WIFI_STATE,
                Manifest.permission.INTERNET,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS,
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.VIBRATE,
                Manifest.permission.CAMERA,
        };

        ArrayList toApplyList = new ArrayList();

        for (String perm : permissions) {
            if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(this, perm)) {
                toApplyList.add(perm);
                // 进入到这里代表没有权限.
            }
        }
        String[] tmpList = new String[toApplyList.size()];
        if (!toApplyList.isEmpty()) {
            ActivityCompat.requestPermissions(this, toApplyList.toArray(tmpList), 123);
        }

    }
}

上面就是我的优化,主要好处就是避免了activity过于臃肿,方便维护。

你可能感兴趣的:(安卓开发高德地图实现定位简单优化)