java - 如何在 Mapbox 中添加位置按钮,如 Google Maps 的 setMyLocationButtonEnabled?
问题描述
我对编程很陌生,我需要制作一个 Android 应用程序。我刚刚发现了 Google Maps 之外的一个很好的 API。它被称为地图盒。所以,我一直在关注他们的文档以及示例代码,但是我认为他们的一些示例已经过时,并且使用的一些类已经被弃用。因此,我已经能够初始化地图并将运行时权限放入我的程序中。
现在,我不知道如何放置一个“位置按钮”,就像 Google Maps API 使用的那样。在使用 Google Maps API 之前,我已经编写了一个应用程序,并使用mMap.getUISettings().setMyLocationEnabled(true)
andmMap.setMyLocationEnabled(true)
来显示位置按钮。当我浏览互联网时,我似乎找不到在 Mapbox 中带来这种 UI 的代码。我找到了一些代码片段,但正如我所说,其中一些已经过时了。此外,他们有自己的 .xml 文件,而我没有,所以当我尝试复制他们的代码时,总是会遇到错误。
所以,如果您知道如何编辑我的文件,或者如果我需要将某些文件添加到我的项目中,请帮助我。
我将在下面附上我的文件。谢谢!
您可以在我的 GitHub 存储库中找到这些文件:https ://github.com/CyrilOlanolan/Tara-JMapbox
我的 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);
}
}
我的 activity_main.xml 文件:
<?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>
解决方案
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);
}
}
});
推荐阅读
- python - 如何在列表理解中执行多项操作
- assembly - 启用分页后的指令似乎没有执行
- puppet - How to download puppet manifest file from master using agent?
- javascript - 如何在 php html css js 中创建网格视图/列表视图?
- c# - Google People Api 联系人已创建,但我在任何地方都看不到它
- go - 如何检查两个结构是否相等
- c# - 在 ASP.NET Core MVC 中保留下拉值
- javascript - 在打字稿(javascript)中向类方法添加方法
- java - ElasticSearch - 模糊搜索 java api 结果不正确
- python - 在 CloudML 中的 Tensorflow 中读取 pandas pickle 文件