问题描述
当我打开那个片段时,我在我的应用程序中实现了谷歌地图(在片段中)它只显示灰色屏幕,而我得到正确的纬度和经度
代码
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
有什么想法吗?
解决方法
问题
在调试过程中,我发现我的地图需要点击屏幕才能加载它的每个单独部分,并且它不会自行加载元素。
解决方案
为了解决我发现的问题并回答 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()
}
问题解决了。