Android:未调用 GoogleMap 回调

标签 android google-maps-android-api-2

我已经使用 Android Studio 模板启动了一个基于 Google map 的新 Android 项目,但我的回调根本没有被调用。

这是我的 Activity 代码:

class MapsActivity : BaseActivity(), OnMapReadyCallback {

    private lateinit var mMap: GoogleMap

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        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)
    }

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

        val center = LatLng(/*Redacted*/)

        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(center, 18.0F))

        mMap.setOnCameraChangeListener { GoogleMap.OnCameraChangeListener {
            val bounds = mMap.projection.visibleRegion.latLngBounds
        } }

        mMap.setOnMapClickListener { GoogleMap.OnMapClickListener {
            val lat = it.latitude
        } }

        mMap.setOnCameraIdleListener { GoogleMap.OnCameraIdleListener {
            val bounds = mMap.projection.visibleRegion.latLngBounds
        } }

        mMap.setOnCameraMoveListener { GoogleMap.OnCameraMoveListener {
            val bounds = mMap.projection.visibleRegion.latLngBounds
        } }
    }
}

回调的内容目前并不重要,我在它们中设置了断点,但没有一个被调用。

这是我的应用程序 gradle 文件:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "redacted"
        minSdkVersion 23
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.google.android.gms:play-services-maps:15.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    implementation 'com.squareup.retrofit2:retrofit:2.6.1'
    implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
}

这可能是一个转移注意力的问题,但偶尔我会在控制台中收到错误消息, map 完全停止工作:

A/art: art/runtime/stack.cc:153] Check failed: success Failed to read the this object in void com.google.maps.api.android.lib6.gmm6.vector.gl.drawable.y.a(com.google.maps.api.android.lib6.gmm6.vector.gl.f, com.google.maps.api.android.lib6.gmm6.vector.camera.c, com.google.maps.api.android.lib6.gmm6.vector.m)

我做错了什么?

编辑:

这是我要求的 list 文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="redacted">

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

    <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">

        <meta-data
                android:name="com.google.android.geo.API_KEY"
                android:value="@string/google_maps_key"/>

        <activity
                android:name=".MapsActivity"
                android:label="@string/title_activity_maps">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

编辑 2:

这是我的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          xmlns:map="http://schemas.android.com/apk/res-auto"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:id="@+id/map"
          tools:context=".MapsActivity"
          android:name="com.google.android.gms.maps.SupportMapFragment"/>

最佳答案

This breakpoint in your code will hit 在您的代码中,上述断点将命中。

试试这个。为了执行该功能,您需要采用以下方法之一。

class MapsActivity : AppCompatActivity(), OnMapReadyCallback, GoogleMap.OnCameraMoveListener, GoogleMap.OnMapClickListener {

private lateinit var mMap: GoogleMap

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    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)
}



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

    val center = LatLng(/*Redacted*/)
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(center, 18.0F))

    mMap.setOnCameraMoveListener(this)
    mMap.setOnMapClickListener(this)

}

override fun onCameraMove() {
    val i = 0
    val bounds = mMap.projection.visibleRegion.latLngBounds

}

override fun onMapClick(p0: LatLng?) {
    val i = 0
    val bounds = mMap.projection.visibleRegion.latLngBounds
}

}

或者另一种设置监听器的方式如下

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

    val center = LatLng(/*Redacted*/)

    mMap.setOnCameraMoveListener {
        val bounds = mMap.projection.visibleRegion.latLngBounds
    }

    mMap.setOnMapClickListener  {
        val bounds = mMap.projection.visibleRegion.latLngBounds

    }

    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(center, 18.0F))

}

关于Android:未调用 GoogleMap 回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57870454/

相关文章:

android - GoogleMap 在 Android API 23 上将为空

java - 在 android 上使用 google maps v2 获取用户位置?

android - 如何在Android上的谷歌地图上设置固定图像

android - 不同设备的授权失败(Android Google Map API v2)

android - 通过 Kotlin 以编程方式获取 Android 广告 ID

android - 在 Android Studio 中,无法删除不需要的文件夹

android - fragment 中的 MapFragment 与 Google Maps api v2 fragment 中的 MapView

android - 如何在 Android 中使用内容解析器插入文件?

android - 我怎么知道我是否还在听 Android 中的套接字

android - 如何使用 Eclipse 调试 UIAutomator 脚本