ArcGIS Runtime:检测点是否在要素图层中的某个区域内?

时间:2017-11-05 07:18:05

标签: android arcgis arcgis-runtime

我在使用ArcGIS Runtime的应用程序中有两个图层。一个是底图图层,另一个是在其上标记了某些区域的要素图层。

如何检测我的位置是否在这些标记区域内?

1 个答案:

答案 0 :(得分:0)

您需要做两件事:

  1. 获取设备的位置
  2. 查看该位置是否与图层的某个要素相交
  3. 以下是我编写的一些代码,将它们放在一起。

    activity_main.xml中:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="so47119156.so47119156.MainActivity">
    
        <TextView
            android:id="@+id/textView_locationLabel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:text="Looking for your location..."/>
    
        <com.esri.arcgisruntime.mapping.view.MapView
            android:id="@+id/mapView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_below="@+id/textView_locationLabel">
        </com.esri.arcgisruntime.mapping.view.MapView>
    
    </RelativeLayout>
    

    MainActivity.java:

    public class MainActivity extends Activity {
    
        private static final int PERM_REQ_START_LOCATION_DATA_SOURCE = 1;
    
        // Change these to match your feature service.
        private static final String FEATURE_SERVICE_URL =
                "https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/3";
        private static final String FEATURE_SERVICE_NAME_FIELD = "STATE_NAME";
    
        private MapView mapView;
        private FeatureLayer statesLayer;
        private TextView textView_locationLabel;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // Get the output label.
            textView_locationLabel = findViewById(R.id.textView_locationLabel);
    
            // Set up the map with a basemap and a feature layer.
            mapView = findViewById(R.id.mapView);
            ArcGISMap map = new ArcGISMap(Basemap.createTopographicVector());
            statesLayer = new FeatureLayer(new ServiceFeatureTable(FEATURE_SERVICE_URL));
            map.getOperationalLayers().add(statesLayer);
            mapView.setMap(map);
    
            // Check location permission and request if needed.
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                    == PackageManager.PERMISSION_GRANTED) {
                // Permission already granted.
                startLocationServices();
            } else {
                // Permission not yet granted.
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERM_REQ_START_LOCATION_DATA_SOURCE);
            }
        }
    
        /**
         * Callback for ActivityCompat.requestPermissions. This method runs when the user allows or
         * denies permission.
         */
        @Override
        public void onRequestPermissionsResult(
                int requestCode,
                @NonNull String[] permissions,
                @NonNull int[] grantResults) {
            if (PERM_REQ_START_LOCATION_DATA_SOURCE == requestCode) {
                // This is a callback for our call to requestPermissions.
                for (int i = 0; i < permissions.length; i++) {
                    String permission = permissions[i];
                    if (Manifest.permission.ACCESS_FINE_LOCATION.equals(permission)
                            && PackageManager.PERMISSION_GRANTED == grantResults[i]) {
                        startLocationServices();
                        break;
                    }
                }
            } else {
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            }
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            mapView.resume();
        }
    
        @Override
        protected void onPause() {
            mapView.pause();
            super.onPause();
        }
    
        @Override
        protected void onStop() {
            mapView.getLocationDisplay().stop();
            super.onStop();
        }
    
        private void startLocationServices() {
            // Add a location listener and then start the location display.
            mapView.getLocationDisplay().addLocationChangedListener(new LocationDisplay.LocationChangedListener() {
                @Override
                public void onLocationChanged(LocationDisplay.LocationChangedEvent locationChangedEvent) {
                    // Location has changed. Query the feature layer.
                    QueryParameters params = new QueryParameters();
                    params.setGeometry(locationChangedEvent.getLocation().getPosition());
                    params.setSpatialRelationship(QueryParameters.SpatialRelationship.INTERSECTS);
                    try {
                        final FeatureQueryResult result = statesLayer.getFeatureTable()
                                .queryFeaturesAsync(params).get();
                        final Iterator<Feature> iterator = result.iterator();
                        if (iterator.hasNext()) {
                            textView_locationLabel.setText("You are in a state named "
                                    + iterator.next().getAttributes().get(FEATURE_SERVICE_NAME_FIELD));
                        } else {
                            textView_locationLabel.setText("You are not inside one of the states.");
                        }
                    } catch (InterruptedException | ExecutionException e) {
                        e.printStackTrace();
                    }
                }
            });
            mapView.getLocationDisplay().startAsync();
        }
    
    }
    

    结果:

    App Screenshot