在片段中放一张地图,只是想做显示悉尼的基本事情。有水印,但它只是空白。
<com.google.android.gms.maps.MapView
android:id="@+id/map_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
map_view.onCreate(savedInstanceState)
map_view.onResume()
MapsInitializer.initialize(context)
map_view.getMapAsync(this)
}
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
val sydney = LatLng(-34.0, 151.0)
mMap.addMarker(MarkerOptions().position(sydney).title("Marker Title").snippet("Marker Description"))
val cameraPosition = CameraPosition.Builder().target(sydney).zoom(12f).build()
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition))
}
尝试下面的代码。
private Marker sydney;
private static final LatLng SYDNEY = new LatLng(-33.87365, 151.20689);
private GoogleMap mMap;
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
sydney = mMap.addMarker(new MarkerOptions().position(SYDNEY).title("Marker Title").snippet("Marker Description"))
val cameraPosition = CameraPosition.Builder().target(SYDNEY).zoom(12f).build()
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition))
}
您可以参考此页面以获取更多示例。 希望有帮助。
您是否在类片段上实现了OnMapReadyCallback?例如:
class MylocationMap : Fragment() ,OnMapReadyCallback{}