我正在尝试用Mapbox在android studio中制作一个地图应用程序,它以前是可以工作的,但现在只显示一个空白屏幕。
多年来,我一直在摆弄它,试图让它发挥作用,但都没有效果。我也有一个'Compativle side by side NDK version is not found. Default is 20.0.5594570‘错误/警告,我也不明白。任何帮助都将非常感谢和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();
}代码的其余部分只是onStop()函数等。
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'
}我认为这是足够的范围和解决问题的代码,但如果需要任何其他东西,我会编辑和提供它。
发布于 2020-04-01 22:51:34
您的活动实现了OnMapReadyCallback,但是在mapView.onCreate(savedInstanceState);之后遗漏了mapView.getMapAsync(this)。
@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.
}
});
}然后,在onStyleLoaded()回调区域添加标记。
与其使用过时的addMarker(),不如考虑使用Mapbox的注解插件https://docs.mapbox.com/android/plugins/overview/annotation或SymbolLayers和源代码https://docs.mapbox.com/android/maps/overview/annotations/#source-and-layer
发布于 2020-04-30 04:25:29
Android Gradle Plugin 3.6+有一个内置的NDK版本,如果你不知道该使用哪个版本,你可以使用它。将在模块的build.gradle文件中使用android.ndkVersion。
即使你的应用程序不直接使用NDK,你也可能会遇到这个错误,因为mapbox需要NDK来构建/拥有原生库,请参考the filed issue。
https://stackoverflow.com/questions/60971445
复制相似问题