Android mapbox integration problems

Posted by pbjpb on Mon, 04 May 2020 11:00:57 +0200

First step dependence.

build.gradle under project

 repositories {
        jcenter()
        mavenCentral()
    }

build.gradle under moudle

compile('com.mapbox.mapboxsdk:mapbox-android-sdk:5.0.2'){
        transitive=true
    }

Error after synchronization:

WTF? What the hell is this.
Search online and add @ aar after the version number

compile('com.mapbox.mapboxsdk:mapbox-android-sdk:5.0.2@aar'){
        transitive=true
    }

Dependency succeeded after resynchronization.

The second step is to register an account on MapBox official website and apply for token. MapBox

Step 3 initialize in Application

public class App extends Application{
    @Override
    public void onCreate() {
        super.onCreate();
        //Set the token applied by accessToken to strings.xml
        Mapbox.getInstance(this,getString(R.string.MapBox_token));
    }
}
Configuration under application node in Android manifest.xml  
android:name=".App"

Step 4 add permission

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Step 5 use map

layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:mapbox="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.mapbox.mapboxsdk.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</RelativeLayout>

Activity

public class MainActivity extends Activity {
    private MapView mapView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mapView = (MapView) findViewById(R.id.mapView);
        mapView.onCreate(savedInstanceState);
        mapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(MapboxMap mapboxMap) {

            }
        });

    }

    @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);
    }

}

Then run the project, it should be OK. The result is

What the hell is this. On the Internet, it may be the cause of the simulator.
Nocturnal God was just used, so it replaced with its own 6.0 simulator to run again.
The discovery map has been shown.

Topics: Android xml Gradle SDK