这是我获取当前位置的代码。如果有人知道这样做的方法,请提供帮助。我被困住了。
private fun enableMyLocation() {
if (isPermissionGranted()) {
if (ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_COARSE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
return
}
map.isMyLocationEnabled = true
} else {
ActivityCompat.requestPermissions(
this, arrayOf<String>(Manifest.permission.ACCESS_FINE_LOCATION),
REQUEST_LOCATION_PERMISSION
)
}
}
在 Google 地图中,您无法直接更改地图上指示您当前位置的蓝点。解决此问题的方法是隐藏蓝点并在用户当前位置添加您自己的自定义标记。
FusedLocationProviderClient fusedLocationClient = LocationServices.getFusedLocationProviderClient(/* provide context here = */ this);
// Get the current location
fusedLocationClient.getLastLocation().addOnSuccessListener(this, location -> {
if (location != null) {
LatLng currentLocation = new LatLng(location.getLatitude(), location.getLongitude());
// Add a custom marker at the user's current location
mMap.addMarker(new MarkerOptions()
.position(currentLocation)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.custom_icon)) // Replace with your custom icon
);
// Move the camera to the user's current location
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLocation, 15));
}
});