获取位置android Kotlin [英] Get location android Kotlin
问题描述
我最近添加了获取位置功能.当我尝试显示经度和纬度时,它返回零.
I recently added get location function. When I try to show longitude and latitude, it returns zero.
这是我的LocationListener类:
This my LocationListener class:
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?) {}
}
这是我的GetUserLocation函数:
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)
}
这是我的功能,可返回我的经度和纬度:
And this my function to return my longitude and latitude:
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
的调用并提供更新.
When GetUserLocation
returns, locationManager
goes out of scope and presumably is destroyed, preventing onLocationChanged
from being called and providing updates.
此外,您已经在GetUserLocation
内定义了mylocation
,因此它也超出了范围,并进一步消除了任何机会或您得到更新.
Also, you've defined mylocation
inside of GetUserLocation
so it also goes out of scope and further kills any chance or your getting an update.
您尚未显示外部mylocation
的声明位置和方式(在GetUserLocation
之外),但是无论如何声明,它都被GetUserLocation
内部的阴影遮盖.所以你收不到多少钱.
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扩展名进行访问.)
Here is an example of how you might do it. (The variable thetext
is defined within the layout xml and accessed with Kotlin extensions.)
// 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) {}
}
这篇关于获取位置android Kotlin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!