ArcGIS Runtime: определить, находится ли точка в определенной области векторного слоя?

У меня есть два слоя в приложении, использующем ArcGIS Runtime. Один — это слой базовой карты, а другой — векторный слой, на котором отмечены определенные области.

Как я могу определить, находится ли мое местоположение в этих отмеченных областях или нет?


person Pranjal Kaler    schedule 05.11.2017    source источник
comment
Не могли бы вы опубликовать свой код, отредактировав этот вопрос? Пожалуйста, покажите нам, что вы пытались сделать.   -  person Dima Kozhevin    schedule 05.11.2017
comment
Какую версию ArcGIS Runtime вы используете? 100.1.0 является последним.   -  person Gary Sheppard    schedule 06.11.2017


Ответы (1)


Вам нужно сделать две вещи:

  1. Get the device's location
  2. See if that location intersects one of the layer's features

Вот некоторый код, который я написал, который объединяет все это.

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>

Основная активность.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();
    }

}

Результат:

Снимок экрана приложения

person Gary Sheppard    schedule 06.11.2017