Reputation:
I'm very new to programming and I need to make an Android application. I just found out a good API other than Google Maps. It's called Mapbox. So, I've been following their documentation and as well as the sample codes but, I think some of their examples are outdated and that a few classes used are deprecated already. So, I was able to initialize the map already and put runtime permissions in my program.
Now, I don't know how to put a "location button" just like what Google Maps API use. I have programmed an app before using Google Maps API and I used the mMap.getUISettings().setMyLocationEnabled(true)
and mMap.setMyLocationEnabled(true)
to show the location button. As I browsed the internet, I can't seem to find a code that brings that kind of UI in Mapbox. I found a few code snippets but then, as I've said, some of them are outdated. Also, they have their own .xml files which I don't have so when I try to copy their code, I always run into errors.
So, if you have any idea how to edit my files or if I need to add certain files into my project, please help me.
I will attach my files below. Thank you!
You can find the files in my GitHub repository: https://github.com/CyrilOlanolan/Tara-JMapbox
My MainActivity.java:
package com.garate.tara_j;
//Mapbox imports
import android.Manifest;
import android.app.AlertDialog;
import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import com.mapbox.android.core.location.LocationEngine;
import com.mapbox.android.core.location.LocationEngineProvider;
import com.mapbox.android.core.permissions.PermissionsListener;
import com.mapbox.android.core.permissions.PermissionsManager;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.location.LocationComponent;
import com.mapbox.mapboxsdk.location.LocationComponentActivationOptions;
import com.mapbox.mapboxsdk.location.LocationComponentOptions;
import com.mapbox.mapboxsdk.location.OnCameraTrackingChangedListener;
import com.mapbox.mapboxsdk.location.OnLocationClickListener;
import com.mapbox.mapboxsdk.location.modes.CameraMode;
import com.mapbox.mapboxsdk.location.modes.RenderMode;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.maps.Style;
import com.mapbox.mapboxsdk.maps.UiSettings;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import java.util.List;
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback, PermissionsListener {
private MapView mapView;
private MapboxMap mapboxMap;
private PermissionsManager permissionsManager;
private boolean isInTrackingMode;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Mapbox.getInstance(getApplicationContext(), getString(R.string.mapbox_access_token));
setContentView(R.layout.activity_main);
mapView = findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
}
@Override
public void onMapReady(@NonNull final MapboxMap mapboxMap) {
MainActivity.this.mapboxMap = mapboxMap;
//ADD Aster's Map Style
//mapboxMap.setStyle(new Style.Builder().fromUri("mapbox://styles/cyrilolanolan/ck5gqdepj0u991io1g3743owe")
mapboxMap.setStyle((Style.MAPBOX_STREETS), 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.
UiSettings uiSettings = mapboxMap.getUiSettings();
uiSettings.setTiltGesturesEnabled(false);
enableLocationComponent(style);
}
});
}
@SuppressWarnings( {"MissingPermission"})
private void enableLocationComponent(@NonNull Style loadedMapStyle) {
if (PermissionsManager.areLocationPermissionsGranted(this)) {
// Permission sensitive logic called here, such as activating the Maps SDK's LocationComponent to show the device's location
// Create and customize the LocationComponent's options
LocationComponentOptions customLocationComponentOptions = LocationComponentOptions.builder(this)
.elevation(5)
.accuracyAlpha(.6f)
.accuracyColor(Color.BLUE)
.build();
LocationComponentActivationOptions locationComponentActivationOptions =
LocationComponentActivationOptions.builder(this, loadedMapStyle)
.locationComponentOptions(customLocationComponentOptions)
.build();
// Get an instance of the component
final LocationComponent locationComponent = mapboxMap.getLocationComponent();
// Activate with options
locationComponent.activateLocationComponent(locationComponentActivationOptions);
// Enable to make component visible
locationComponent.setLocationComponentEnabled(true);
// Set the component's render mode
locationComponent.setRenderMode(RenderMode.COMPASS);
} else {
permissionsManager = new PermissionsManager(this);
permissionsManager.requestLocationPermissions(this);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
permissionsManager.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
@Override
public void onExplanationNeeded(List<String> permissionsToExplain) {
Toast.makeText(this, "Tara-J needs permission to function", Toast.LENGTH_SHORT);
}
@Override
public void onPermissionResult(boolean granted) {
if (granted) {
mapboxMap.getStyle(new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
enableLocationComponent(style);
}
});
} else {
Toast.makeText(this, "Permissions are required. Please restart Tara-J.", Toast.LENGTH_SHORT);
finish();
}
}
@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);
}
}
My activity_main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
mapbox:mapbox_cameraTargetLat="7.0707"
mapbox:mapbox_cameraTargetLng="125.6087"
mapbox:mapbox_cameraZoom="13" />
</androidx.constraintlayout.widget.ConstraintLayout>
Upvotes: 1
Views: 1685
Reputation: 2546
Your LocationComponent
setup code looks great! Mapbox provides no built-in UI button. You'll need to add it to your XML layout. In the button's onClick()
, get the LocationComponent
's last known location, and animate the camera to the last known location's coordinates:
findViewById(R.id.device_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Location lastKnownLocation = mapboxMap.getLocationComponent().getLastKnownLocation();
if (lastKnownLocation != null) {
CameraPosition position = new CameraPosition.Builder()
.target(new LatLng(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude())) // Sets the new camera position
.zoom(16)
.bearing(0)
.tilt(0)
.build(); // Creates a CameraPosition from the builder
mapboxMap.animateCamera(CameraUpdateFactory
.newCameraPosition(position), 7000);
}
}
});
Upvotes: 5