0

我有一种情况,我需要创建具有使用 google mapview 的自定义地图视图的 sdk。如何创建这样一个?我尝试了一些东西。但是当我在我的示例应用程序中使用它时。它给了我错误。

CustomeMapView.kt

import android.content.Context
import android.os.Bundle
import android.util.AttributeSet
import com.google.android.gms.maps.*

class CustomMapView: MapView, OnMapReadyCallback {

    private val customMap: CustomMap by lazy { CustomMap() }
    private lateinit var onMapReadyCallback: CustomOnMapReadyCallback

    constructor(context: Context): super(context)

    constructor(context: Context, attrs: AttributeSet): super(context, attrs)

    constructor(context: Context, attrs: AttributeSet, defStyle: Int): super(context, attrs, defStyle)

    constructor(context: Context, options: GoogleMapOptions): super(context, options)

    fun getCustomMapAsync(callback: CustomOnMapReadyCallback) {
        onMapReadyCallback = callback
        super.getMapAsync(this)
    }

    override fun onMapReady(map: GoogleMap) {
        customMap.addMap(map)

        if (::onMapReadyCallback.isInitialized) {
            onMapReadyCallback.onCustomMapReady(customMap)
        }
    }

    fun onMapCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    fun onMapResume() {
        super.onResume()
    }

    fun onMapPause() {
        super.onPause()
    }

    fun onMapDestroy() {
        super.onDestroy()
    }

    fun onMapLowMemory() {
        super.onLowMemory()
    }
}

自定义地图.kt

class CustomMap {

    private lateinit var googleMap: GoogleMap

    fun addCustomMarker(lat: Double, lng: Double) {
        if (::googleMap.isInitialized) {
            googleMap.addMarker(MarkerOptions().position(LatLng(lat, lng)))
            moveCamera(lat,lng)
        }
    }

    fun moveCamera(lat: Double, lng: Double) {
        val latlng = LatLng(lat, lng)
        val zoom = 15f
        googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latlng, zoom))
    }

    fun addMap(map: GoogleMap) {
        googleMap = map
    }
}

CustomOnMapReadyCallback.kt

interface CustomOnMapReadyCallback {

    fun onCustomMapReady(map: CustomMap)
}

但是当我在我的示例应用程序活动中使用这个 sdk 时,无论我用我的视图调用什么方法,我都会收到以下错误

Cannot access 'com.google.android.gms.maps.MapView' which is a supertype of 'com.example.map.CustomMapView'. Check your module classpath for missing or conflicting dependencies
Cannot access 'com.google.android.gms.maps.OnMapReadyCallback' which is a supertype of 'com.example.map.CustomMapView'. Check your module classpath for missing or conflicting dependencies

我的示例应用程序中的 MapActivity.kt

class MainActivity : AppCompatActivity(), com.example.map.CustomOnMapReadyCallback {

    private lateinit var mapView: com.example.map.CustomMapView
    private lateinit var customMap: CustomMap
    private lateinit var markerButton: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        mapView = findViewById(R.id.mapView)
        markerButton = findViewById(R.id.add_marker_button) as Button

        mapView.onMapCreate(savedInstanceState)

        mapView.getCustomMapAsync(this)

        markerButton.setOnClickListener {
            moveCamera()
        }
    }

    override fun onCustomMapReady(map: CustomMap) {
        customMap = map
    }

    private fun moveCamera() {
        if (::customMap.isInitialized) {
            customMap.addCustomMarker(8.700000, 77.470001)
        }
    }

    override fun onResume() {
        super.onResume()
        mapView.onMapResume()
    }

    override fun onPause() {
        super.onPause()
        mapView.onMapPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        mapView.onMapDestroy()
    }

    override fun onLowMemory() {
        super.onLowMemory()
        mapView.onMapLowMemory()
    }
}
4

0 回答 0