Ian Newson
Ian Newson

Reputation: 7949

Android: GoogleMap callbacks not being called

I've started a new Android project based around a Google map using the Android Studio template, and my callbacks are not getting called at all.

Here is my activity code:

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

The contents of the callbacks aren't important at the moment, I'm setting breakpoints within them all and none of them are getting called.

Here's my app gradle file:

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

This might be a red herring, but occasionally I get an error in the console and the map stops working at all:

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)

What am I doing wrong?

Edit:

Here's my manifest file as requested:

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

Edit 2:

Here's my layout file:

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

Upvotes: 1

Views: 1320

Answers (2)

Zaartha
Zaartha

Reputation: 1126

This breakpoint in your code will hit In your code the above breakpoint will hit.

Try this. In order for the function to execute you need to go by one of the following approaches.

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
}

}

Or another way to set listeners is as follows

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

}

Upvotes: 1

Bhanu Prakash Pasupula
Bhanu Prakash Pasupula

Reputation: 1002

you have to initialize google api client in onCreate() method.

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

Upvotes: 0

Related Questions