我最近添加了get location函数.当我试图显示经度和纬度时,它返回零.

这是我的LocationListener类:

inner class MylocationListener: LocationListener {
    constructor():super(){
        mylocation= Location("me")
        mylocation!!.longitude
        mylocation!!.latitude
    }

    override fun onLocationChanged(location: Location?) {
        mylocation=location
    }

    override fun onStatusChanged(p0: String?, p1: Int, p2: Bundle?) {}

    override fun onProviderEnabled(p0: String?) {}

    override fun onProviderDisabled(p0: String?) {}
}

And this my GetUserLocation function:

fun GetUserLocation(){
    var mylocation= MylocationListener()
    var locationManager=getSystemService(Context.LOCATION_SERVICE) as LocationManager
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0.1f,mylocation)
}

这是我的函数,返回我的经度和纬度:

fun getLoction (view: View){
    prgDialog!!.show();

    GetUserLocation()

    button.setTextColor(getResources().getColor(R.color.green));
    textView.text = mylocation!!.latitude.toFloat().toString()
    Toast.makeText(this, mylocation!!.latitude.toFloat().toString(), Toast.LENGTH_LONG).show()
    Toast.makeText(this, mylocation!!.longitude.toFloat().toString(), Toast.LENGTH_LONG).show()

    prgDialog!!.hide()
} 

推荐答案

GetUserLocation返回时,locationManager超出范围,可能会被销毁,从而阻止onLocationChanged被调用并提供更新.

此外,你在GetUserLocation中定义了mylocation,所以它也超出了范围,进一步扼杀了任何获得更新的机会.

You have not shown where and how the outer mylocation is declared (outside of GetUserLocation), but how ever it is declared, it is being shadowed by the one inside of GetUserLocation. So you aren't getting much.

这里有一个您可能如何做到这一点的示例.(变量thetext在布局XML中定义,并使用Kotlin扩展进行访问.)

// in the android manifest
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
// allow these through Appliation Manager if necessary

// inside a basic activity
private var locationManager : LocationManager? = null

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    setSupportActionBar(toolbar)

    // Create persistent LocationManager reference
    locationManager = getSystemService(LOCATION_SERVICE) as LocationManager?

    fab.setOnClickListener { view ->
        try {
            // Request location updates
            locationManager?.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0L, 0f, locationListener)
        } catch(ex: SecurityException) {
            Log.d("myTag", "Security Exception, no location available")
        }
    }
}

//define the listener
private val locationListener: LocationListener = object : LocationListener {
    override fun onLocationChanged(location: Location) {
        thetext.text = ("" + location.longitude + ":" + location.latitude)
    }
    override fun onStatusChanged(provider: String, status: Int, extras: Bundle) {}
    override fun onProviderEnabled(provider: String) {}
    override fun onProviderDisabled(provider: String) {}
}

Kotlin相关问答推荐

用浮点数或十进制数给出错误答案的阶乘计算

Groovy Gradle文件的Kotlin类似功能

处理合成层次 struct 中的深层按钮以切换视图

使用调度程序运行异步 Kotlin 代码

gradle 如何 Select 以-jvm结尾的库?

是什么让 Kotlin 中的 String 类能够使用方括号?

多次运行espresso测试

Kotlin RxJava 可空的错误

Kotlin 静态函数:伴生对象,@JvmStatic @JvmField

Kotlin 中的数据类

如何从 Firestore 查询中排除元素?

用mockk验证属性设置程序吗?

如何将 Kotlin 日期中的字符串或时间戳格式化为指定的首选格式?

Spring Boot:更改属性占位符符号

Kotlin的BiMap/2-way hashmap

内联 onFocusChange kotlin

@StringRes、@DrawableRes、@LayoutRes等android注释使用kotlin参数进行判断

Kotlin类型安全类型别名

kotlin中密封类和密封接口的区别是什么

在 IntelliJ Idea 中未为 Kotlin @ConfigurationProperties 类生成 spring-configuration-metadata.json 文件