Android 获取谷歌地图片段时遇到问题,请调用onMapReady
所以我遇到的问题是,我不确定如何访问地图来设置初始位置。片段总是返回NULL,因此它似乎没有调用getMapReady函数。我只是不确定如何在布局中正确引用map小部件,以便它能够正确调用map的设置 布局Android 获取谷歌地图片段时遇到问题,请调用onMapReady,android,google-maps,android-mapview,Android,Google Maps,Android Mapview,所以我遇到的问题是,我不确定如何访问地图来设置初始位置。片段总是返回NULL,因此它似乎没有调用getMapReady函数。我只是不确定如何在布局中正确引用map小部件,以便它能够正确调用map的设置 布局 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="ma
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.user.thisweekintown.CalendarFragment">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/mySwipe"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/bus_list_recycle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="horizontal" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<fragment
android:id="@+id/businessslocation"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="15dp"
android:background="@color/cardview_light_background"/>
</FrameLayout>
然后
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val view : View = inflater.inflate(R.layout.fragment_bus_list,container,false)
var social_recycle: RecyclerView = view.findViewById(R.id.bus_list_recycle)
Log.d("should be calling", "onMapReady")
swipey = view.findViewById(R.id.mySwipe)
swipey.setColorSchemeColors(Color.RED, Color.BLUE, Color.GREEN, Color.CYAN);
swipey.setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener { setupRecycle(true) })
calFrPrefs = context?.getSharedPreferences("main_prefs",0)
(activity as MainActivity).getSupportActionBar()?.setTitle(calFrPrefs!!.getString("current_town_name","No Town")+ " Businesses")
setupRecycle(false)
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
mapView = childFragmentManager.findFragmentById(R.id.businessslocation) as? SupportMapFragment
Log.d("isMapView", mapView.toString())
mapView?.getMapAsync(this)
}
然后
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val view : View = inflater.inflate(R.layout.fragment_bus_list,container,false)
var social_recycle: RecyclerView = view.findViewById(R.id.bus_list_recycle)
Log.d("should be calling", "onMapReady")
swipey = view.findViewById(R.id.mySwipe)
swipey.setColorSchemeColors(Color.RED, Color.BLUE, Color.GREEN, Color.CYAN);
swipey.setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener { setupRecycle(true) })
calFrPrefs = context?.getSharedPreferences("main_prefs",0)
(activity as MainActivity).getSupportActionBar()?.setTitle(calFrPrefs!!.getString("current_town_name","No Town")+ " Businesses")
setupRecycle(false)
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
mapView = childFragmentManager.findFragmentById(R.id.businessslocation) as? SupportMapFragment
Log.d("isMapView", mapView.toString())
mapView?.getMapAsync(this)
}
它应该调用以下命令
override fun onMapReady(googleMap: GoogleMap){
mMap = googleMap
Log.d("onMapready", "MapReady")
val cphbusiness = LatLng(42.187, 71.3065)
mMap.addMarker(MarkerOptions().position(cphbusiness).title("Her"))
mMap.moveCamera(CameraUpdateFactory.zoomTo(16.0F))
mMap.moveCamera(CameraUpdateFactory.newLatLng(cphbusiness))
val geocoder = Geocoder(context)
if (geocoder is Geocoder) {
var list: List<Address> = geocoder.getFromLocationName(totaladd, 1)
if(list.size >0) {
var mAddress: Address = list.get(0)
Log.d("latitiude", mAddress.latitude.toString())
Log.d("latitiude", mAddress.longitude.toString())
var address1 = LatLng(mAddress.latitude, mAddress.longitude)
changeMap(address1)
}else
{
Toast.makeText(context, "Unable to find Valid Location...Centering on Random place", Toast.LENGTH_LONG).show()
changeMap(cphbusiness)
}
} else {
}
}
fun changeMap(myLL: LatLng) {
mMap.moveCamera(CameraUpdateFactory.newLatLng(myLL))
mMap.addMarker(MarkerOptions().position(myLL))
}
override-fun-onMapReady(谷歌地图:谷歌地图){
mMap=谷歌地图
Log.d(“onMapready”、“MapReady”)
val cphbusiness=LatLng(42.187,71.3065)
mMap.addMarker(MarkerOptions().position(cphbusiness.title(“她”))
mMap.移动摄像头(摄像头更新工厂缩放(16.0F))
mMap.moveCamera(CameraUpdateFactory.newLatLng(cphbusiness))
val geocoder=geocoder(上下文)
if(地理编码器是地理编码器){
变量列表:list=geocoder.getFromLocationName(totaladd,1)
如果(list.size>0){
var mAddress:Address=list.get(0)
Log.d(“latitiude”,mAddress.latitude.toString())
Log.d(“纬度”,mAddress.longitude.toString())
var address1=LatLng(mAddress.latitude,mAddress.longitude)
更改地图(地址1)
}否则
{
Toast.makeText(上下文,“无法找到有效位置…以随机位置为中心”,Toast.LENGTH\u LONG.show()
变更图(cphbusiness)
}
}否则{
}
}
趣味变化图(myLL:LatLng){
mMap.moveCamera(CameraUpdateFactory.newLatLng(myLL))
mMap.addMarker(MarkerOptions().position(myLL))
}
通过活动片段管理器访问片段:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// Fragments added to XML via <fragment> tag get added to the ACTIVITY fragment manager
// so access the fragment through that instead.
mapView = activity.supportFragmentManager.findFragmentById(R.id.businessslocation) as? SupportMapFragment
Log.d("isMapView", mapView.toString())
mapView?.getMapAsync(this)
}
覆盖已创建的视图(视图:视图,savedInstanceState:Bundle?){
super.onViewCreated(视图,savedInstanceState)
//通过标记添加到XML的片段将添加到活动片段管理器
//因此,通过它来访问片段。
mapView=activity.supportFragmentManager.findFragmentById(R.id.businessslocation)作为?SupportMapFragment
Log.d(“isMapView”,mapView.toString())
mapView?.getMapAsync(此)
}
或者根据,通过代码添加MapFragment
希望有帮助 最后,我通过将片段名称从
android:name=“com.google.android.gms.maps.MapFragment”
更改为android:name=“com.google.android.gms.maps.SupportMapFragment”
getChildFragmentManager
用于片段内部的片段-尝试使用fragmentManager。是否有错误?添加stacktrace(如果是)printed@MaratZangiev不,只是片段返回null@AndymapView=fragmentManager?.findFragmentById(R.id.businessslocation)为?MapFragment仍以Null结束No dice仍将mapView.ToString作为Null返回mapView=activity?.supportFragmentManager?.findFragmentById(R.id.businessslocation)作为?MapFragmentMap片段的父级是当我单击导航按钮ib5时调用的片段集。setOnClickListener({v->changeFragment(frag5,false,null)})