不调用onMapReady方法

时间:2019-11-27 21:59:40

标签: android android-studio android-layout android-fragments android-intent

Google地图可以使用,但是我不能使用它,因为未调用onMapReady方法。 С也许有人知道原因是什么? 我认为我也许可以想到一些带有碎片的事物,因为我只是在学习与它们合作 检查的依赖项-所有已连接

MainActivity.kt:

package com.example.weather

import android.os.Bundle
import android.util.Log
import android.view.MenuItem
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.OnMapReadyCallback
import com.google.android.gms.maps.SupportMapFragment
import com.google.android.gms.maps.model.LatLng
import kotlinx.android.synthetic.main.map_fragment.*
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;


class MainActivity : AppCompatActivity(),OnMapReadyCallback {

   private lateinit var map:GoogleMap


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


        val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as? SupportMapFragment
        mapFragment?.getMapAsync(this)

    }


    fun onHistoryItemClicked(item: MenuItem) {
        supportFragmentManager.beginTransaction()
            .add(R.id.fragment_container, HistoryFragment())
            .commit()
    }

    fun onMapItemClicked(item: MenuItem) {
        supportFragmentManager.beginTransaction()
            .add(R.id.fragment_container, MapFragment())
            .commit()




    }


    override fun onMapReady(googleMap: GoogleMap?) {
        Log.d("Tag","StartsOnMap")
        googleMap ?: return
        val belarus = LatLng(-34.0, 151.0)
        with(googleMap) {
            moveCamera(CameraUpdateFactory.newLatLngZoom(belarus, 0F))
            addMarker(MarkerOptions().position(belarus))
        }
    }



}

map_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">


    <fragment
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />



</androidx.constraintlayout.widget.ConstraintLayout>

MapFragment.kt

package com.example.weather

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.OnMapReadyCallback
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.MarkerOptions
import kotlinx.android.synthetic.main.map_fragment.*

class MapFragment: Fragment()  {


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        return inflater.inflate(R.layout.map_fragment, container, false)


    }

0 个答案:

没有答案