属性图未在片段中初始化

发布于 2025-02-05 02:51:18 字数 1075 浏览 2 评论 0原文

Google地图没有在片段中初始化。即使在不同的片段生命周期中尝试调用getmapasync

class MyMapFragment : Fragment(), OnMapReadyCallback {
   private lateinit var map: GoogleMap
  
   override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
    //...binding

   }

   override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val mapFragment = childFragmentManager.findFragmentById(R.id.r_map) as SupportMapFragment?
        mapFragment?.getMapAsync(this)
      //...
       
    }
  
  override fun onMapReady(googleMap: GoogleMap) {
       map = googleMap
   }
  
  private fun configureMap(
        start: Pair<Double, Double>,
        end: Pair<Double, Double>,
   ) {
    //Here the map says it's not initialized
    //so the condition fails
     if (this::map.isInitialized) {
         drawPolyLine(start.first, start.second, end.first, end.second)
            //...
     }
  }
  //...
}

Google map is not being initialized in fragment. Even after trying to call getMapAsync in different fragment lifecycles

class MyMapFragment : Fragment(), OnMapReadyCallback {
   private lateinit var map: GoogleMap
  
   override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
    //...binding

   }

   override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val mapFragment = childFragmentManager.findFragmentById(R.id.r_map) as SupportMapFragment?
        mapFragment?.getMapAsync(this)
      //...
       
    }
  
  override fun onMapReady(googleMap: GoogleMap) {
       map = googleMap
   }
  
  private fun configureMap(
        start: Pair<Double, Double>,
        end: Pair<Double, Double>,
   ) {
    //Here the map says it's not initialized
    //so the condition fails
     if (this::map.isInitialized) {
         drawPolyLine(start.first, start.second, end.first, end.second)
            //...
     }
  }
  //...
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘虞初梦 2025-02-12 02:51:18

我在onmapready上打了configureMap,感谢 Ammar Abdullah

override fun onMapReady(googleMap: GoogleMap) {
       map = googleMap
       configureMap(//...)
   }
  
  private fun configureMap(
        start: Pair<Double, Double>,
        end: Pair<Double, Double>,
   ) {
    //Here the map says it's not initialized
    //so the condition fails
     if (this::map.isInitialized) {
         drawPolyLine(start.first, start.second, end.first, end.second)
            //...
     }
  }

I called the configureMap on the onMapReady thanks to Ammar Abdullah

override fun onMapReady(googleMap: GoogleMap) {
       map = googleMap
       configureMap(//...)
   }
  
  private fun configureMap(
        start: Pair<Double, Double>,
        end: Pair<Double, Double>,
   ) {
    //Here the map says it's not initialized
    //so the condition fails
     if (this::map.isInitialized) {
         drawPolyLine(start.first, start.second, end.first, end.second)
            //...
     }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文