地图活动中的错误未显示用户的当前位置

时间:2019-09-16 18:37:52

标签: android google-maps android-studio kotlin runtime-error

我正在使用Google地图制作Apk,其中我必须获取用户位置,因此标记将随着用户位置位置的变化而移动,但是在执行后,出现以下错误:

Error:(63, 71) Unresolved reference: GPS_PROVIDER
Error:(107, 59) Unresolved reference: sydney
Error:(156, 17) Unresolved reference: runOnUiThread
Error:(157, 21) Unresolved reference: mMap
Error:(159, 17) Unresolved reference: mMap
Error:Execution failed for task ':app:compileDebugKotlin'.
> Compilation error. See log for more details

参考:Error:Execution failed for task ':app:compileDebugKotlin'. > Compilation error. See log for more details

通过重建项目尝试了一些,但是没有用。

class MapsActivity : AppCompatActivity(), OnMapReadyCallback {

    private lateinit var mMap: GoogleMap

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        checkPermission()
        setContentView(R.layout.activity_maps)
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        val mapFragment = supportFragmentManager
                .findFragmentById(R.id.map) as SupportMapFragment
        mapFragment.getMapAsync(this)
    }

    var ACCESSLOCATION=143
    fun checkPermission()
    {
        if(Build.VERSION.SDK_INT>=23)
        {
            if(ActivityCompat.checkSelfPermission(this,
                    android.Manifest.permission.ACCESS_FINE_LOCATION)!=PackageManager.PERMISSION_GRANTED)


                requestPermissions(arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION), ACCESSLOCATION)

        }
        getLocation()
    }
           fun getLocation()

           {
                  Toast.makeText(this,"User Location access is on",Toast.LENGTH_LONG).show()

               var mylocation=MyLocationListener()

               var LocationManager=getSystemService(Context.LOCATION_SERVICE) as LocationManager

               LocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,3,3f,mylocation)
               var thred=myThread()
               thred.start()
           }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>?, grantResults: IntArray?) {
        when(requestCode)
        {
            ACCESSLOCATION->
            {
                if(grantResults!![0]==PackageManager.PERMISSION_GRANTED)

                    getLocation()
                else
                {
                    Toast.makeText(this,"Access not granted to your location",Toast.LENGTH_LONG).show()
                }
            }
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
    }

    override fun onMapReady(googleMap: GoogleMap) {
        mMap = googleMap

        // Add a marker in Sydney and move the camera



        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney,10f))
    }
}

var mlocation:Location?=null

//to get the user location

  class MyLocationListener: LocationListener
{

    constructor()
    {
        mlocation=Location("Start")
        mlocation!!.longitude=0.0
        mlocation!!.latitude=0.0

    }

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

 override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) {
     //TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
 }

 override fun onProviderEnabled(provider: String?) {
    //TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
 }

 override fun onProviderDisabled(provider: String?) {
    // TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
 }

}

class myThread: Thread
{
 constructor():super()
 {

 }

    override fun run() {
        while (true)
        {

            try {
                runOnUiThread{
                    mMap!!.clear()
                val sydney = LatLng(mlocation!!.longitude, mlocation!!.latitude)
                mMap.addMarker(MarkerOptions()
                        .position(sydney)
                        .title("Me")
                        .snippet("Here's my location")
                        .icon(BitmapDescriptorFactory.fromResource(R.drawable.p1)))
                }
                Thread.sleep(1000)
            }catch (ex:Exception){}

        }
    }
}

0 个答案:

没有答案