简体   繁体   中英

Android google map inside fragment not showing map

Have a map inside a fragment, just trying to do the basic thing of showing Sydney. Has the watermark, but its just blank.

<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))
    }

Try below code.

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))
    }

You can refer this page for more examples. Hope that helps.

are u implements OnMapReadyCallback on your class fragment? like:

class MylocationMap : Fragment() ,OnMapReadyCallback{}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM