Android:未调用GoogleMap回调

时间:2019-09-10 12:05:25

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

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

这是我的活动代码:

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'
}

这可能是一条红色的鲱鱼,但偶尔在控制台中出现错误,地图完全停止工作:

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)

我在做什么错了?

编辑:

这是我要求的清单文件:

<?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"/>

2 个答案:

答案 0 :(得分:1)

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))

}

答案 1 :(得分:0)

您必须使用onCreate()方法初始化google api客户端。

mGoogleApiClient = GoogleApiClient.Builder(this)
        .addApi(Places.GEO_DATA_API)
        .addApi(Places.PLACE_DETECTION_API)
        .enableAutoManage(this, this)
        .build()