【问题标题】:OnMapReady is not being called/shown in fragmentContainerViewFragmentContainerView 中未调用/显示 OnMapReady
【发布时间】:2021-10-22 06:18:37
【问题描述】:

我是 Google Maps API 的新手,我遇到的问题是,当我尝试在 fragmentContainerView 上显示地图时,它不显示标记并且不缩放相机,但是当我在片段本身上设置内容视图时它显示标记缩放相机。

这只是在 fragmentContainerView 中显示地图,没有标记并且不缩放,它基本上只是调用地图。

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_map)
        supportActionBar?.hide()

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

另一方面,现在这也显示了标记和缩放,但由于 setContentView,它替换了整个活动视图。

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_map)
        supportActionBar?.hide()

        binding = FragmentMapsBinding.inflate(layoutInflater)
        setContentView(binding.root)

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

        fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
    }

请帮忙。

【问题讨论】:

    标签: android google-maps kotlin


    【解决方案1】:

    问题是您正在尝试使用 Activity 的 FragmentManager,而您应该使用 Fragment 的子 FragmentManager。 移除 Fragment 中的 onCreate() 覆盖,并添加一个 onCreateView() 覆盖在您膨胀布局的位置。

      override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
        ): View {
    
        binding = FragmentMapsBinding.inflate(inflate,R.layout.activity_map, 
                  container, false)
    
    
        val mapFragment = childFragmentManager
            .findFragmentById(R.id.map) as SupportMapFragment?
        mapFragment?.getMapAsync(this)
    
        fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
        
        return binding.root
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多