2017-08-30 119 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) {} 
}