《ArcGIS Runtime for Android 100.2.1学习笔记》三:使用LocationDisplay实现定位

      在ArcGIS Runtime for Android100里新增了一个类LocationDisplay,其官方解释为:管理当前位置在显示地图里的展示,包括当前位置信息、符号、以及随地图的平移、旋转、缩放等进行自动变化。也就是有了这个类,不仅可以获取当前位置信息进行定位,也可以将位置信息展示出来。

1、加载地图

    //定义一个MapView对象
    public MapView mapView = null;
    //定义LocationDisplay对象
    public LocationDisplay locationDisplay;
    private int requestCode = 2;
    //定位所需权限
    String[] reqPermissions = new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};
    //将MapVIew对象与控件进行绑定
    this.mapView = (MapView)this.getView().findViewById(R.id.mapView);
    //添加天地图底图
    WebTiledLayer webTiledLayer = TianDiTuMethodsClass.CreateTianDiTuTiledLayer(TianDiTuMethodsClass.LayerType.TIANDITU_VECTOR_2000);
    Basemap tdtBasemap =new Basemap(webTiledLayer);
    WebTiledLayer webTiledLayer1 = TianDiTuMethodsClass.CreateTianDiTuTiledLayer(TianDiTuMethodsClass.LayerType.TIANDITU_VECTOR_ANNOTATION_CHINESE_2000);
    tdtBasemap.getBaseLayers().add(webTiledLayer1);
    ArcGISMap map = new ArcGISMap(tdtBasemap);
    mapView.setMap(map);

2、使用LocationDisplay实现定位

        //根据mapview对象获取locationDisplay对象
        locationDisplay = mapView.getLocationDisplay();
        //设置定位模式
        locationDisplay.setAutoPanMode(LocationDisplay.AutoPanMode.RECENTER );
        locationDisplay.startAsync();
        //监听位置的变化
        locationDisplay.addDataSourceStatusChangedListener(new LocationDisplay.DataSourceStatusChangedListener() {
            @Override
            public void onStatusChanged(LocationDisplay.DataSourceStatusChangedEvent dataSourceStatusChangedEvent) {
                if (dataSourceStatusChangedEvent.isStarted())
                    return;
                if (dataSourceStatusChangedEvent.getError() == null)
                    return;
                //检查ACCESS_FINE_LOCATION权限是否允许
                boolean permissionCheck1 = ContextCompat.checkSelfPermission(getActivity(), reqPermissions[0]) ==
                        PackageManager.PERMISSION_GRANTED;
                //检查ACCESS_COARSE_LOCATION权限是否允许
                boolean permissionCheck2 = ContextCompat.checkSelfPermission(getActivity(), reqPermissions[1]) ==
                        PackageManager.PERMISSION_GRANTED;
                if (!(permissionCheck1 && permissionCheck2))
                {
                    ActivityCompat.requestPermissions(getActivity(),reqPermissions,requestCode);
                }
                else
                {
                    String message = String.format("Error in DataSourceStatusChangedListener: %s", dataSourceStatusChangedEvent
                            .getSource().getLocationDataSource().getError().getMessage());
                    Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show();
                }
            }
        });

4、关于定位模式和定位符号的设置,请参考下文:

http://blog.csdn.net/bit_kaki/article/details/78044398?locationNum=4&fps=1


猜你喜欢

转载自blog.csdn.net/wnag_qing_zhong/article/details/79416484