伙计们,我正在尝试获取用户位置,但是出了点问题,当我运行我的应用程序时,没有播放器图标,并且位置有误-它仍然显示lat = 0.0,lon = 0.0。我不使用模拟器,而是在手机上测试应用(Android 4.4.2如此重要)。请看一下我的代码,也许我只是看不到一个错误。先感谢您!
void Timer::checkTimer() {
auto now = std::chrono::system_clock::now().time_since_epoch();
auto milisecondsDuration = std::chrono::milliseconds(duration);
auto futureDuration = now + milisecondsDuration;
while (std::chrono::system_clock::now().time_since_epoch() < futureDuration)
{
if(endTimer)
{
std::terminate();
}
std::this_thread::sleep_for (std::chrono::milliseconds(1));
cout << "Running\n";
}
completedListener.timerCompleted(entityId, cause);
}
}
`
答案 0 :(得分:0)
尝试这种方式
实现这些依赖的实现
implementation 'com.google.android.gms:play-services:11.4.0'
implementation 'com.google.android.gms:play-services-maps:11.4.0'
在您的AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
创建一个locationListener
界面
interface locationListener {
fun locationResponse(locationResult: LocationResult)
}
然后创建一个Location.kt
类
class Location (var activity: AppCompatActivity, locationListener: locationListener){
private val permissionFineLocation=android.Manifest.permission.ACCESS_FINE_LOCATION
private val permissionCoarseLocation=android.Manifest.permission.ACCESS_COARSE_LOCATION
private val REQUEST_CODE_LOCATION=100
private var fusedLocationClient: FusedLocationProviderClient?=null
private var locationRequest: LocationRequest?=null
private var callbabck: LocationCallback?=null
init {
fusedLocationClient= FusedLocationProviderClient(activity.applicationContext)
inicializeLocationRequest()
callbabck=object: LocationCallback(){
override fun onLocationResult(p0: LocationResult?) {
super.onLocationResult(p0)
locationListener.locationResponse(p0!!)
}
}
}
private fun inicializeLocationRequest() {
locationRequest= LocationRequest()
locationRequest?.interval=50000
locationRequest?.fastestInterval=5000
locationRequest?.priority=LocationRequest.PRIORITY_HIGH_ACCURACY
}
private fun validatePermissionsLocation():Boolean{
val fineLocationAvailable= ActivityCompat.checkSelfPermission(activity.applicationContext, permissionFineLocation)== PackageManager.PERMISSION_GRANTED
val coarseLocationAvailable=ActivityCompat.checkSelfPermission(activity.applicationContext, permissionCoarseLocation)==PackageManager.PERMISSION_GRANTED
return fineLocationAvailable && coarseLocationAvailable
}
private fun requestPermissions(){
val contextProvider=ActivityCompat.shouldShowRequestPermissionRationale(activity, permissionFineLocation)
if(contextProvider){
Toast.makeText(activity.applicationContext, "Permission is required to obtain location", Toast.LENGTH_SHORT).show()
}
permissionRequest()
}
private fun permissionRequest(){
ActivityCompat.requestPermissions(activity, arrayOf(permissionFineLocation, permissionCoarseLocation), REQUEST_CODE_LOCATION)
}
fun onRequestPermissionsResult(requestCode:Int, permissions:Array<out String>, grantResults:IntArray){
when(requestCode){
REQUEST_CODE_LOCATION->{
if(grantResults.size>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
getLocation()
}else{
Toast.makeText(activity.applicationContext, "You did not give permissions to get location", Toast.LENGTH_SHORT).show()
}
}
}
}
fun stopUpdateLocation(){
this.fusedLocationClient?.removeLocationUpdates(callbabck)
}
fun inicializeLocation(){
if (validatePermissionsLocation()){
getLocation()
}else{
requestPermissions()
}
}
@SuppressLint("MissingPermission")
private fun getLocation() {
validatePermissionsLocation()
fusedLocationClient?.requestLocationUpdates(locationRequest, callbabck, null)
}
}
用法
class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
var location: Location?=null
private var mMap: GoogleMap? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_maps)
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
val mapFragment = supportFragmentManager
.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)
location= Location(this, object :locationListener{
override fun locationResponse(locationResult: LocationResult) {
mMap?.clear()
val sydney = LatLng(locationResult.lastLocation.latitude, locationResult.lastLocation.longitude)
mMap?.addMarker(MarkerOptions().position(sydney).title("Hi").snippet("Let's go!"))
mMap?.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 14f))
}
})
}
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
location?.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
override fun onStart() {
super.onStart()
location?.inicializeLocation()
}
override fun onPause() {
super.onPause()
location?.stopUpdateLocation()
}
}
希望对您有帮助