【发布时间】:2021-12-18 00:21:47
【问题描述】:
class MapsMarkerActivity : Fragment(),OnMapReadyCallback {
private lateinit var mMap:GoogleMap
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val mapFragment = childFragmentManager.findFragmentById(R.id.map) as? SupportMapFragment
mapFragment?.getMapAsync(this)
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
val sydney = LatLng(-33.852, 151.211)
mMap.addMarker(
MarkerOptions()
.position(sydney)
.title("Marker in Sydney")
)
}
}
我要做的是使用底部导航栏导航到片段并在那里打开地图 这是 XML:
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.dashboard.DashboardFragment" >
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.test.MapsMarkerActivity" />
</androidx.constraintlayout.widget.ConstraintLayout>
地图已创建,但 OnMapReady 未初始化
【问题讨论】:
-
好的...所以您正在尝试导航...并且...问题/问题是...什么?
-
地图已创建,但 OnMapReady 未添加标记
-
您查看日志是否有错误?您是否使用调试器单步执行以验证该行是否实际被命中?
标签: android kotlin google-maps