markdown

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Mapbox Integration with Android

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'
...
}

And after sync, updatee the dependency to

Step 1.1: Update to Latest Dependency

dependencies {
...
implementation libs.android.sdk
...
}

Step 2: Import Necessary Classes


Though we will use maplibre package, it uses mapbox legacy classes in functionality. Import the following classes in your
MainActivity.java or the relevant activity file:

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

Step 4: Handle Location Permissions


Check for location permissions and get the current location:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.
ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
fusedLocationClient.getLastLocation().addOnSuccessListener(this, location -> {
if (location != null) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Toast.makeText(MainActivity.this,"Current Location: " +
latitude +", " + longitude,
Toast.LENGTH_LONG).show();

LocationComponent locationComponent = mapboxMap.getLocationComponent();

locationComponent.activateLocationComponent(
LocationComponentActivationOptions.builder(this,
Objects.requireNonNull(mapboxMap.getStyle())).build());
locationComponent.setLocationComponentEnabled(true);
locationComponent.setCameraMode(CameraMode.TRACKING_GPS_NORTH);
locationComponent.setRenderMode(RenderMode.NORMAL);
}
});
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy