我遵循以下指南:
https://developers.google.com/codelabs/maps-platform/maps-platform-101-android#9
我正试图从一个片段中得到一张地图。我已经成功地绘制了地图,但现在当地图打开时,它默认以非常高的级别显示非洲。我希望地图默认为在更地方级别向我的城市开放。
我的片段xml文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<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=".ViewNearbyFragment">
<androidx.fragment.app.FragmentContainerView
xmlns:map="http://schemas.android.com/apk/res-auto"
class="com.google.android.gms.maps.SupportMapFragment"
map:mapId="{my_map_id}"
android:id="@+id/map_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
与相同片段对应的Kotlin文件如下所示:
class ViewNearbyFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
(fragmentManager?.findFragmentById(
R.id.map_fragment
) as? SupportMapFragment)?.getMapAsync { googleMap ->
// Ensure all places are visible in the map.
googleMap.setOnMapLoadedCallback {
val bounds = LatLngBounds.builder()
bounds.include(LatLng(0.0, 0.0))
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds.build(), 20))
}
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_view_nearby, container, false)
}
}
我从来没有进入getMapAsync调用之后的块,我不知道为什么。如何访问在xml文件中创建的地图?或者更好的是,如何更改与此地图关联的默认值?