我无法获得我的申请中的确切位置

时间:2018-05-12 16:13:30

标签: android kotlin

当我选择一个联系人到达我的应用程序中的位置时,它会显示一个带有位置图标的地图,但是在地图中间不在卷轴位置,以便MainActivity中的代码:

    fun getUserLocation() {
var myLocation = MyLocationListener()
val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3, 3f, myLocation)

//list sent to request
var userData=UserData(this)
val myPhoneNumber=userData.loadPhoneNumber()
databaseRef!!.child("Users").child(myPhoneNumber).child("request").addValueEventListener(
    object :ValueEventListener{
        override fun onDataChange(p0: DataSnapshot?) {
            if (MainActivity.myLocation==null) return
            //get datatime
            val df = SimpleDateFormat("yyyy/MM/dd HH:MM:ss")
            val date = Date()

           databaseRef!!.child("Users").child(myPhoneNumber).child("location").child("lat").setValue(MainActivity.myLocation!!.latitude)
           databaseRef!!.child("Users").child(myPhoneNumber).child("location").child("log").setValue(MainActivity.myLocation!!.longitude)
           databaseRef!!.child("Users").child(myPhoneNumber).child("location").child("lastOnline").setValue(df.format(date).toString())

        } 

我的Full应用程序的URL帮助了我 https://files.fm/u/xnrm88u7

1 个答案:

答案 0 :(得分:0)

您需要从回调侦听器获得协调。

所以在位置监听器里面列出发送请求:

   inner class MyLocationListener: LocationListener {

                     constructor():super(){
                     AccesLocation=true
                     myLocation= Location("me")
                     myLocation?.longitude=0.0
                     myLocation?.latitude=0.0
                     }

                     override fun onLocationChanged(location: Location?) {
                         myLocation=location

                      //list sent to request
    var userData=UserData(this)
    val myPhoneNumber=userData.loadPhoneNumber()
    databaseRef?.child("Users").child(myPhoneNumber).child("request").addValueEventListener(
        object :ValueEventListener{
            override fun onDataChange(p0: DataSnapshot?) {
                if (MainActivity.myLocation==null) return
                //get datatime
                val df = SimpleDateFormat("yyyy/MM/dd HH:MM:ss")
                val date = Date()

               databaseRef?.child("Users").child(myPhoneNumber).child("location").child("lat").setValue(loction?.latitude)
               databaseRef?.child("Users").child(myPhoneNumber).child("location").child("log").setValue(loction?.longitude)
               databaseRef?.child("Users").child(myPhoneNumber).child("location").child("lastOnline").setValue(df.format(date).toString())

            } 

                     }

...
};   

尝试使用插入 !!