Mehdi.ncb
Mehdi.ncb

Reputation: 352

Global location search in mapbox doesn't working on android

I'm stuck on a problem with mapbox.
The search place doesn't work.
I have tried many things but nothing works. When i click on the search button, nothing happen, and when i'm searching a place(by tipping in the search bar), nothing happen too, there is no suggestion

The xml:file

<com.mapbox.mapboxsdk.plugins.places.autocomplete.ui.SearchView
    android:layout_width="match_parent"
    android:background="@drawable/layout_form_text"
    android:elevation="3dp"
    android:gravity="bottom|left"
    android:padding="10dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginBottom="19dp"
    android:layout_height="wrap_content"/>

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab_location_search"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end|bottom"
    android:layout_margin="16dp"
    android:tint="@android:color/white"
    app:backgroundTint="@color/colorPrimary"
    app:srcCompat="@android:drawable/ic_search_category_default" />

PlacePluginActivity file :

public class PlacesPluginActivity extends AppCompatActivity implements OnMapReadyCallback {

    private static final int REQUEST_CODE_AUTOCOMPLETE = 1;
    private MapView mapView;
    private MapboxMap mapboxMap;
    private CarmenFeature home;
    private CarmenFeature work;
    private String geojsonSourceLayerId = "geojsonSourceLayerId";
    private String symbolIconId = "symbolIconId";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Mapbox access token is configured here. This needs to be called either in your application
        // object or in the same activity which contains the mapview.
        Mapbox.getInstance(this, Token);

        // This contains the MapView in XML and needs to be called after the access token is configured.
        setContentView(R.layout.activity_layout_map_priere);

        mapView = findViewById(R.id.mapView);
        mapView.onCreate(savedInstanceState);
        mapView.getMapAsync(this);
    }


    private void initSearchFab() {
        findViewById(R.id.fab_location_search).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new PlaceAutocomplete.IntentBuilder()
                    .accessToken("pk.eyJ1IjoibWVoZGluIiwiYSI6ImNrYTRobzR4YTB1MnAzbG9nMGljejBpb2UifQ.nG3vtoddC1TCHW3Skpkttg")
                        .placeOptions(PlaceOptions.builder().build())
                        .build(PlacesPluginActivity.this);
                startActivityForResult(intent, REQUEST_CODE_AUTOCOMPLETE);
            }
        });
    }
}

I don't know why it's not working, I can't find the problem here

Upvotes: 2

Views: 787

Answers (1)

langsmith
langsmith

Reputation: 2546

Using the SearchView directly in your XML is probably the problem. The SearchView is used internally by the plugin.

https://github.com/mapbox/mapbox-plugins-android/tree/master/app/src/main/java/com/mapbox/mapboxsdk/plugins/testapp/activity/places shows your various options for setting up the plugin. Make sure to look at the XML layout file for each example activity https://github.com/mapbox/mapbox-plugins-android/tree/83b191404b2d53da30f14fd7c671fe0b3eb9c4b6/app/src/main/res/layout (activity_picker_launcher.xml, activity_places_fragment.xml, andactivity_places_launcher.xml`)

I'd clone the repo, add your token at https://github.com/mapbox/mapbox-plugins-android/blob/master/app/src/main/java/com/mapbox/mapboxsdk/plugins/testapp/PluginApplication.kt#L20, and then run the app on your device so that you can see what the examples look like.

Also, be careful about posting your Mapbox token on a public site. I'd rotate the token at https://account.mapbox.com/access-tokens/ so that others can't use your currently exposed one.

Upvotes: 1

Related Questions