本文介绍了无法在Kotlin中初始化GoogleMap对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试创建成员变量mMap会引发错误分类器GoogleMap没有伴随对象,因此必须在此处初始化".在Java中,我将只使用私有GoogleMap mMap".

Attempting to create a member variable mMap throws an error "Classifier GoogleMap does not have a companion object, and thus must be initialized here". In Java, I would have just used "Private GoogleMap mMap".

感谢您的帮助!

private val TAG = "MapActivity"

private val FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION
private val COURSE_LOCATION = Manifest.permission.ACCESS_COARSE_LOCATION
private val LOCATION_PERMISSION_REQUEST_CODE = 1234;

private var mLocationPermissionGranted : Boolean = false
private var mMap = GoogleMap

推荐答案

像这样初始化GoogleMap

initialize GoogleMap like this

 private lateinit var mMap: GoogleMap? = null

实现OnMapReadyCallback接口,并使用onMapReady(GoogleMap)回调方法来获取GoogleMap对象的句柄. GoogleMap对象是地图本身的内部表示形式.要设置地图的视图选项,请修改其GoogleMap对象.

Implement the OnMapReadyCallback interface and use the onMapReady(GoogleMap) callback method to get a handle to the GoogleMap object. The GoogleMap object is the internal representation of the map itself. To set the view options for a map, you modify its GoogleMap object.

class MainActivity : OnMapReadyCallback {

  // your other code

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

if (mMap != null) {
    val permission = ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION)

    if (permission == PackageManager.PERMISSION_GRANTED) {
        mMap?.isMyLocationEnabled = true
    } else {
        requestPermission(
                Manifest.permission.ACCESS_FINE_LOCATION,
                LOCATION_REQUEST_CODE)
     }
 }
 }
}

这篇关于无法在Kotlin中初始化GoogleMap对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 07:53