我有一个在 Google 地图上显示商店位置的片段:
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.OnMapReadyCallback
import com.google.android.gms.maps.SupportMapFragment
import com.google.android.gms.maps.model.BitmapDescriptorFactory
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.MarkerOptions
class AboutFragment : Fragment(), OnMapReadyCallback {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val mapFragment = childFragmentManager.findFragmentById(R.id.jollycat_map) as SupportMapFragment?
mapFragment?.getMapAsync(this)
return inflater.inflate(R.layout.fragment_about, container, false)
}
override fun onMapReady(gmap: GoogleMap) {
val AWESOME_STORE_POSITION = LatLng(-6.20175, 106.78208)
val markerOptions = MarkerOptions().position(AWESOME_STORE_POSITION).title("Awesome Store").icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_location))
gmap.addMarker(markerOptions)
gmap.moveCamera(CameraUpdateFactory.newLatLng(AWESOME_STORE_POSITION))
gmap.animateCamera(CameraUpdateFactory.zoomTo(20f))
}
}
以及 XML 布局:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:background="@color/white"
android:orientation="vertical"
tools:context=".AboutFragment">
<androidx.cardview.widget.CardView
android:id="@+id/layout_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="4dp"
app:layout_constraintTop_toTopOf="parent">
<Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:title="About Us"
android:titleTextColor="@color/darkBlue" />
</androidx.cardview.widget.CardView>
<LinearLayout
android:id="@+id/layout_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:gravity="center"
android:orientation="vertical"
android:padding="16dp"
app:layout_constraintTop_toBottomOf="@id/layout_toolbar">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="8dp"
android:background="@drawable/rounded_edit_text"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/tvDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/robaga_rounded_regular"
android:textColor="@color/darkBlue"
android:textSize="20dp"
android:textStyle="bold"
tools:text="Yet another description" />
<TextView
android:id="@+id/tvDescription1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/robaga_rounded_regular"
android:textColor="@color/darkBlue"
android:textSize="20dp"
android:textStyle="bold"
tools:text="Yet another company..." />
<FrameLayout
android:layout_marginTop="20dp"
android:layout_width="400dp"
android:layout_height="400dp">
<fragment
android:id="@+id/store_map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
val AWESOME_STORE_POSITION = LatLng(-6.20175, 106.78208)
是真实位置。谷歌地图显示这是印度尼西亚西雅加达的一个地区。运行应用程序会得到以下结果:
最初,谷歌地图显示非洲。所以我必须捏和缩放几次才能到达西雅加达,但标记不在那里。这是怎么回事?
gmap.animateCamera(CameraUpdateFactory.zoomTo(20f))
不是应该自动将地图缩放到标记吗?
OnMapReady 永远不会被调用
val mapFragment = childFragmentManager.findFragmentById(R.id.jollycat_map) as SupportMapFragment?
这里您尝试将mapFragment转换为SupportMapFragment类型或null。 因为下一行的转换没有成功
mapFragment?.getMapAsync(this)
不调用 getMapAsync。由于您使用的是安全呼叫, 你不会收到错误。
现在因为选角不成功的缘故, 这只是因为您使用错误的 ID 调用片段。
val mapFragment = childFragmentManager.findFragmentById(R.id.store_map) as SupportMapFragment?
mapFragment?.getMapAsync(this)
应该可以。 您可以在此处阅读有关 Kotlin 类型检查的更多信息。