gpt4 book ai didi

Huawei Maps: MapsInitializer is not initialized(华为地图:MapsInitializer未初始化)

转载 作者:bug小助手 更新时间:2023-10-25 11:51:02 37 4
gpt4 key购买 nike



I have a trouble to implement Huawei maps even after I implemented Google Maps, OsmDroid and Mapsforge before. The error I see is:

即使在我之前实现了谷歌地图、OsmDroid和Mapsforge之后,我也在实现华为地图方面遇到了麻烦。我看到的错误是:



HmsMapKit_MapView_15 MapsInitializer is not initialized.



This topic didn't solve the problem and I need to find what is wrong.

这个话题没有解决问题,我需要找到什么是错的。


I even tried to copy the full Activity code from a demo sample:

我甚至尝试从一个演示示例中复制完整的活动代码:


    companion object {
private const val TAG = "MapViewDemoActivity"
private const val MAPVIEW_BUNDLE_KEY = "MapViewBundleKey"
private const val REQUEST_CODE = 100
private val LAT_LNG = LatLng(48.893478, 2.334595)
private val RUNTIME_PERMISSIONS = arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE)

/**
* checkSelfPermission
*
* @param context Context
* @param permissions permissions
* @return true or false
*/
private fun hasPermissions(context: Context, vararg permissions: String): Boolean {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && permissions != null) {
for (permission in permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false
}
}
}
return true
}
}

private lateinit var mMapView: MapView
private var hMap: HuaweiMap? = null
private var mMarker: Marker? = null
private var mCircle: Circle? = null

override fun onCreate(savedInstanceState: Bundle?) {
Log.d(TAG, "map onCreate:")
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (!hasPermissions(this, *RUNTIME_PERMISSIONS)) {
ActivityCompat.requestPermissions(this, RUNTIME_PERMISSIONS, REQUEST_CODE)
}

// get mapView by layout view
mMapView = findViewById(R.id.mapView)
var mapViewBundle: Bundle? = null
if (savedInstanceState != null) {
mapViewBundle = savedInstanceState.getBundle(MAPVIEW_BUNDLE_KEY)
}
// please replace "Your API key" with api_key field value in
// agconnect-services.json if the field is null.
MapsInitializer.setApiKey("MyKEY ends ==")
mMapView.onCreate(mapViewBundle)

// get map by async method
mMapView.getMapAsync(this@MainActivity)
}

override fun onMapReady(map: HuaweiMap) {
Log.d(TAG, "onMapReady: ")

// after call getMapAsync method ,we can get HuaweiMap instance in this call back method
hMap = map
if (hasPermissions(this, RUNTIME_PERMISSIONS[0], RUNTIME_PERMISSIONS[1])) {
setLocationEnabled(true)
}

// move camera by CameraPosition param ,latlag and zoom params can set here
val build = CameraPosition.Builder().target(LAT_LNG).zoom(10f).build()
val cameraUpdate = CameraUpdateFactory.newCameraPosition(build)
hMap?.animateCamera(cameraUpdate)

// mark can be add by HuaweiMap
mMarker = hMap?.addMarker(
MarkerOptions().position(LAT_LNG)
// .icon(BitmapDescriptorFactory.fromResource(R.drawable.logo))
.clusterable(true))

// circle can be add by HuaweiMap
mCircle = hMap?.addCircle(CircleOptions().center(LatLng(48.793478, 2.334595)).radius(1000.0).fillColor(Color.GREEN))
mCircle?.fillColor = Color.TRANSPARENT
}

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == REQUEST_CODE) {
setLocationEnabled(grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED)
}
}

/**
* set location enable
*
* @param enable true:enable, false:disable
*/
private fun setLocationEnabled(enable: Boolean) {
hMap?.isMyLocationEnabled = enable
hMap?.uiSettings?.isMyLocationButtonEnabled = enable
}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
var mapViewBundle = outState.getBundle(MAPVIEW_BUNDLE_KEY)
if (mapViewBundle == null) {
mapViewBundle = Bundle()
outState.putBundle(MAPVIEW_BUNDLE_KEY, mapViewBundle)
}
mMapView.onSaveInstanceState(mapViewBundle)
}

override fun onStart() {
super.onStart()
mMapView.onStart()
}

override fun onResume() {
super.onResume()
mMapView.onResume()
}

override fun onPause() {
super.onPause()
mMapView.onPause()
}

override fun onStop() {
super.onStop()
mMapView.onStop()
}

override fun onDestroy() {
super.onDestroy()
mMapView.onDestroy()
}

override fun onLowMemory() {
super.onLowMemory()
mMapView.onLowMemory()
}

Maybe the problem is on the HMS project setup, so I added 3 SHA-256 footprints I got from the Firebase project settings (or gradle signingReport / keytool -list -v -keystore). But also for the case I sign my debug app with a release key:

可能问题出在HMS项目设置上,所以我添加了从Firebase项目设置(或Gradle signingReport/keyTool-list-v-keystore)获得的3个SHA-256足迹。但对于我用发布密钥签署调试应用程序的情况也是如此:


signingConfigs {
create("release") {
keyAlias = keystoreProperties.getProperty("keyAlias")
keyPassword = keystoreProperties.getProperty("keyPassword")
storeFile = file(keystoreProperties.getProperty("storeFile"))
storePassword = keystoreProperties.getProperty("storePassword")
enableV1Signing = true
enableV2Signing = true
}
}

buildTypes {
getByName("release") {
signingConfig = signingConfigs.getByName("release")
}

getByName("debug") {
signingConfig = signingConfigs.getByName("release")
}

And added all the permissions from the demo project.

并添加了演示项目中的所有权限。


更多回答
优秀答案推荐
更多回答

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com