尝试创建成员变量mMap会引发错误“分类器GoogleMap没有伴随对象,因此必须在此处初始化”。在Java中,我将只使用“私有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
答案 0 :(得分:0)
像这样初始化GoogleMap
private lateinit var mMap: GoogleMap? = null
实现OnMapReadyCallback接口,并使用onMapReady(GoogleMap)回调方法来获取GoogleMap对象的句柄。 GoogleMap对象是地图本身的内部表示形式。要设置地图的视图选项,请修改其GoogleMap对象。
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)
}
}
}
}
答案 1 :(得分:0)
我尝试应用它
<块引用>私有 lateinit var 地图:GoogleMap? = 空
但我得到了
<块引用>'lateinit' 修饰符不能用于可空类型的属性