markdown
markdown
markdown
This guide will help you integrate Mapbox (MapLibre) into your Android application. I followed the steps below to set up the
dependencies, import necessary classes, and implement the map functionality.
Installation
Step 1: Add Dependencies
Add the following dependencies to your build.gradle (app) file:
dependencies {
...
implementation 'org.maplibre.gl:android-sdk:10.0.2'
...
}
dependencies {
...
implementation libs.android.sdk
...
}
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.Style;
import com.mapbox.mapboxsdk.location.LocationComponent;
import com.mapbox.mapboxsdk.locationLocationComponentActivationOptions;
import com.mapbox.mapboxsdk.location.modes.CameraMode;
import com.mapbox.mapboxsdk.location.modes.RenderMode;
Step 3: Initialize MapView
Initialize the MapView in your activity and set the map style:
mapView = binding.mapView;
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(map -> {
// Set the style after mapView was loaded
String styleUrl = "https://api.maptiler.com/maps/
streets/style.json?key=MAP_TILER_API_KEY"; " +
map.setStyle(new Style.Builder().fromUri(styleUrl), style -> {
map.getUiSettings().setAttributionMargins(15, 0, 0, 15);
// Set the map view center
map.setCameraPosition(new CameraPosition.Builder().target(new LatLng(LATITUDE, LONGITUDE))
.zoom(ZOOM_LEVEL)
.build());
// Check for location permissions
if (ContextCompat.checkSelfPermission(//SET ANDROID PERMISSION) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},PERMISSIONS_REQUEST_LOCATION);
} else {
//IMPLEMENT NECESSARY VIEWS
}
});
});
locationComponent.activateLocationComponent(
LocationComponentActivationOptions.builder(this,
Objects.requireNonNull(mapboxMap.getStyle())).build());
locationComponent.setLocationComponentEnabled(true);
locationComponent.setCameraMode(CameraMode.TRACKING_GPS_NORTH);
locationComponent.setRenderMode(RenderMode.NORMAL);
}
});
}