2017-08-30 80 views
0

我最近添加了get location函数。当我尝试显示经度和纬度时,它返回零。获取位置android kotlin

这是我的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?) {} 
} 

这我GetUserLocation功能:

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() 
} 
+0

它返回零或空? –

+0

您的应用程序是否已获得所需的位置权限?你在设备或模拟器上测试吗?设备上的gps是否处于活动状态? –

回答

3

GetUserLocation回报,locationManager超出范围,并可能被破坏,预防从onLocationChanged被调用并提供更新。

此外,您已在GetUserLocation中定义mylocation,因此它也超出范围并进一步杀死任何机会或获取更新。

您还没有表现出在哪里以及外mylocation如何申报(外GetUserLocation),但如何以往任何时候都宣布,它正在由GetUserLocation内的一个阴影。所以你没有太多。

下面是你如何做的一个例子。 (变量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.setText("" + location.longitude + ":" + location.latitude); 
    } 
    override fun onStatusChanged(provider: String, status: Int, extras: Bundle) {} 
    override fun onProviderEnabled(provider: String) {} 
    override fun onProviderDisabled(provider: String) {} 
}