i want change Location in all apps iam use that but dont work
我想在我使用的所有应用程序中更改位置,但不起作用
override fun onPlaceSelected(place: Place) {
val latLng = place.latLng!!
zoomOnMap(latLng)
changeLocation(latLng)
setMockLocation(latLng)
Log.i("gk", "Place: ${place.latLng}")
}
@SuppressLint("WrongConstant")
private fun changeLocation(latLng: LatLng) {
if (ActivityCompat.checkSelfPermission(
requireContext(),
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
requireContext(),
Manifest.permission.ACCESS_COARSE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
return
}
val locationProvider: FusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(requireContext())
locationProvider.setMockMode(true)
val providerName = LocationManager.GPS_PROVIDER
val mockLocation = Location(providerName)
mockLocation.latitude = latLng.latitude
mockLocation.longitude = latLng.longitude
mockLocation.time = System.currentTimeMillis()
mockLocation.accuracy = 1f
mockLocation.elapsedRealtimeNanos = SystemClock.elapsedRealtimeNanos()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mockLocation.bearingAccuracyDegrees = 0.1f
mockLocation.verticalAccuracyMeters = 0.1f
mockLocation.speedAccuracyMetersPerSecond = 0.01f
}
locationProvider.setMockLocation(mockLocation)
}
@SuppressLint("WrongConstant")
fun setMockLocation(latLng: LatLng) {
val locationManager = requireActivity().getSystemService(Context.LOCATION_SERVICE) as LocationManager?
locationManager!!.addTestProvider(
LocationManager.GPS_PROVIDER, false, false,
false, false, true, true, true, 0, 5
)
locationManager.setTestProviderEnabled(LocationManager.GPS_PROVIDER, true)
val mockLocation = Location(LocationManager.GPS_PROVIDER)
mockLocation.latitude = latLng.latitude // Sydney
mockLocation.longitude = latLng.longitude
@SuppressLint(“WrongConstant”)Fun setMockLocation(LatLng:LatLng){val LocationManager=requireActivity().getSystemService(Context.LOCATION_SERVICE)as LocationManager?LocationManager!!.addTestProvider(LocationManager.GPS_PROVIDER,FALSE,TRUE,0,5)locationManager.setTestProviderEnabled(LocationManager.GPS_PROVIDER,TRUE)VAL mock Location=Location(LocationManager.GPS_Provider)mock Location.atitude=latLng.atitude//悉尼mock Location.Longize=latLng.Longize
mockLocation.accuracy = 5f
mockLocation.time = System.currentTimeMillis()
mockLocation.elapsedRealtimeNanos = System.nanoTime()
locationManager.setTestProviderLocation(
LocationManager.GPS_PROVIDER,
mockLocation
)
}
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission
android:name="android.permission.ACCESS_MOCK_LOCATION"
tools:ignore="MockLocation,ProtectedPermissions" />
更多回答
Please trim your code to make it easier to find your problem. Follow these guidelines to create a minimal reproducible example.
请修改您的代码,以便更容易地找到您的问题。遵循这些指导原则,创建一个最小的可重现示例。
优秀答案推荐
我是一名优秀的程序员,十分优秀!