MapBox Android插件:在活动上显示地点搜索栏

问题描述 投票:0回答:1

我有一个带有MapBox地图的地图活动,我想在屏幕的顶部显示一个搜索栏来搜索地点。我在我的应用程序gradle中包含了以下库:

implementation 'com.mapbox.mapboxsdk:mapbox-android-sdk:7.2.0'
implementation 'com.mapbox.mapboxsdk:mapbox-android-plugin-annotation-v7:0.5.0'
implementation 'com.mapbox.mapboxsdk:mapbox-android-plugin-places-v7:0.7.0'

布局xml文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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="it.quintetto.seeme.ui.maps.MapsActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_profileview2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/black"
        android:minHeight="?attr/actionBarSize"
        android:theme="?attr/actionBarTheme"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.constraint.ConstraintLayout
        android:id="@+id/mapSearchBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="30dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/toolbar_profileview2"/>

    <com.mapbox.mapboxsdk.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/mapSearchBar"
        app:layout_constraintBottom_toBottomOf="parent"
        app:mapbox_cameraTargetLat="40.73581"
        app:mapbox_cameraTargetLng="-73.99155"
        app:mapbox_cameraZoom="11" />
</android.support.constraint.ConstraintLayout>

活动的内容是:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Mapbox.getInstance(this, <token>);
    setContentView(R.layout.activity_maps);
    mapView = findViewById(R.id.mapView);
    mapView.onCreate(savedInstanceState);
    mapView.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(@NonNull MapboxMap mapboxMap) {
            mapboxMap.setStyle(Style.MAPBOX_STREETS, new Style.OnStyleLoaded() {
                @Override
                public void onStyleLoaded(@NonNull Style style) {}
            });
        }
    });
    PlaceAutocompleteFragment autocompleteFragment;

    if (savedInstanceState == null) {
        autocompleteFragment = PlaceAutocompleteFragment.newInstance(<token>);

        final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.mapSearchBar, autocompleteFragment, PlaceAutocompleteFragment.TAG);
        transaction.commit();
    } else {
        autocompleteFragment = (PlaceAutocompleteFragment)getSupportFragmentManager().findFragmentByTag(PlaceAutocompleteFragment.TAG);
    }
    autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected(CarmenFeature carmenFeature) {
            Toast.makeText(MapsActivity.this,
                    carmenFeature.text(), Toast.LENGTH_LONG).show();
            finish();
        }

        @Override
        public void onCancel() {
            finish();
        }
    });
}

@Override
public void onStart() {
    super.onStart();
    mapView.onStart();
}

@Override
public void onResume() {
    super.onResume();
    mapView.onResume();
}

@Override
public void onPause() {
    super.onPause();
    mapView.onPause();
}

@Override
public void onStop() {
    super.onStop();
    mapView.onStop();
}

@Override
public void onLowMemory() {
    super.onLowMemory();
    mapView.onLowMemory();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    mapView.onDestroy();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    mapView.onSaveInstanceState(outState);
}

当我打开活动时,地图会显示,并且还会显示虚拟键盘,而不是通常的输入按钮。但是没有搜索栏。我怎么解决?

谢谢

android mapbox openstreetmap mapbox-plugin
1个回答
0
投票

我通过简单地用垂直方向的LinearLayout替换最外面的ConstraintLayout来解决。

© www.soinside.com 2019 - 2024. All rights reserved.