0

I'm trying to make a map application in android studio using Mapbox and it used to work but now only displays a blank screen.

I have been fiddling with it trying to make it work for ages, to no avail. I also have a 'Compativle side by side NDK version was not found. Default is 20.0.5594570' error/warning I do not understand either. Any help will be much appreciated and TIA.

MainActivity.java:

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {

private MapView mapView;
private MapboxMap map;

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

    Mapbox.getInstance(this, getString(R.string.access_token));
    setContentView(R.layout.activity_main);
    mapView = (MapView) findViewById(R.id.mapView);
    mapView.onCreate(savedInstanceState);

}

@SuppressWarnings("deprecation")
public void onMapReady(@NonNull MapboxMap mapboxMap) {
    map = mapboxMap;
    mapboxMap.setStyle(Style.OUTDOORS);

    mapboxMap.setCameraPosition(
            new CameraPosition.Builder()
                .target(new LatLng(53.472, -2.239))
                .zoom(8.0)
                .build());

    mapboxMap.addMarker(new MarkerOptions()
            .position(new LatLng(53.472, -2.239))
            .title("John Dalton"));
}

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

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

The rest of the code is just onStop() functions etc.

build.gradle (:app):


android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.example.stationmapper"
        minSdkVersion 23
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    //Mapbox dependencies
    implementation 'com.mapbox.mapboxsdk:mapbox-android-sdk:9.0.0'
    implementation 'com.mapbox.mapboxsdk:mapbox-android-plugin-annotation-v9:0.8.0'
}

I think this is enough code to scope and fix the problem but if anything else is needed, I will edit and provide it.

2 Answers2

2

Your activity implements OnMapReadyCallback, but you're missing mapView.getMapAsync(this) after mapView.onCreate(savedInstanceState);.

@Override public void onMapReady(@NonNull MapboxMap mapboxMap) {
  map = mapboxMap;
  mapboxMap.setStyle(Style.OUTDOORS, new Style.OnStyleLoaded() {
    @Override public void onStyleLoaded(@NonNull Style style) {

    // Map is set up and the style has loaded. Now you can add data or make other map adjustments.

    }
  });
}

Then, add the marker in the onStyleLoaded() callback area.

Rather than using the deprecated addMarker(), consider using Mapbox's Annotation Plugin https://docs.mapbox.com/android/plugins/overview/annotation or SymbolLayers and sources https://docs.mapbox.com/android/maps/overview/annotations/#source-and-layer

langsmith
  • 2,529
  • 1
  • 11
  • 13
0

Android Gradle Plugin 3.6+ has an internally embedded NDK version to use if you do NOT call out which version to use. The SHORT ANSWER is to use android.ndkVersion in your module's build.gradle file.

Even your app does not directly use NDK, you might still experience the error as mapbox needs NDK to build/has native libs, refer to the filed issue.

Gerry
  • 1,223
  • 9
  • 17