【问题标题】:Google Maps show gray on android device谷歌地图在安卓设备上显示为灰色
【发布时间】:2021-05-12 21:25:28
【问题描述】:

当我打开该片段时,我在我的应用程序中实现了谷歌地图(在片段中),当我获得正确的纬度、经度时它只显示灰屏

代码

override fun onCreateView(
  inflater: LayoutInflater, container: ViewGroup?,
  savedInstanceState: Bundle?
): View? {
  // Inflate the layout for this fragment
  val root = inflater.inflate(R.layout.fragment_map_to_customer, container, false)
  customerAddressArgument = requireArguments().getString("customerAddressArgument").toString()
  // Google Maps
  var mapView = root.findViewById(R.id.map) as MapView
  mapView.getMapAsync(this)
  mapView.onCreate(arguments)
  return root
}

override fun onMapReady(googleMap: GoogleMap) {
  mMap = googleMap
  // mMap.uiSettings.isZoomControlsEnabled = true

  val geoCoder = Geocoder(context)
  var address = geoCoder.getFromLocationName(customerAddressArgument, 1)!![0]
  val latLng = LatLng(address.latitude, address.longitude)
  mMap!!.addMarker(MarkerOptions().position(latLng).title("Location"))
  mMap!!.animateCamera(CameraUpdateFactory.newLatLng(latLng))
  Log.d("latLng20:", latLng.toString()) <-- return: **D/latLng20:: lat/lng: (-6.3946055,106.7947916)**
  Toast.makeText(
    context,
    address.latitude.toString() + " " + address.longitude,
    Toast.LENGTH_LONG
  ).show()
}

latLng 在谷歌maps website

有什么想法吗?

【问题讨论】:

  • mapView.onCreate(arguments) 是什么,我在文档中没有找到它?
  • @PavelPoley 没有该地图将不会被加载
  • 你的地图sdk启用了吗?
  • @UsamaAltaf 是的,我想是的,但要确定我应该在哪里检查它?

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


【解决方案1】:

问题

在调试过程中,我发现我的地图需要在屏幕上点击才能加载它的每个单独部分,并且它不会自行加载元素。

解决方案

为了解决我发现的问题并回答here,建议使用onResume() 函数来加载地图,这是它的完成方式。

var mapView: MapView? = null //<-- add this

override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val root = inflater.inflate(R.layout.fragment_map_to_customer, container, false)
        customerAddressArgument = requireArguments().getString("customerAddressArgument").toString()

        // Google Maps
        mapView = root.findViewById(R.id.map) as MapView //<- define it here
        mapView!!.getMapAsync(this)
        mapView!!.onCreate(arguments)
        return root
}

override fun onResume() {
  mapView?.onResume() //<- get it in onResume()
  super.onResume()
}

问题解决了。

【讨论】:

    猜你喜欢
    • 2018-07-16
    • 1970-01-01
    • 2013-05-28
    • 1970-01-01
    • 2019-11-10
    • 2012-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多