I am trying to implement a card in my activity, but when moving from category to activation, the card does not load:

enter image description here

MapActivity class

public class MapActivity extends AppCompatActivity implements OnMapReadyCallback { GoogleMap googleMap; private static final double TARGET_LATITUDE = 17.893366; private static final double TARGET_LONGITUDE = 19.511868; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_map); createMapView(); } private void createMapView() { // MapsInitializer.initialize(context); try { if (null == googleMap) { ((MapFragment) getFragmentManager().findFragmentById( R.id.mapView)).getMapAsync(this); if (null == googleMap) { Toast.makeText(getApplicationContext(), "Error creating map", Toast.LENGTH_SHORT).show(); } } } catch (NullPointerException exception) { Log.e("mapApp", exception.toString()); } } private void addMarker() { double lat = TARGET_LATITUDE; double lng = TARGET_LONGITUDE; CameraPosition cameraPosition = new CameraPosition.Builder() .target(new LatLng(lat, lng)) .zoom(15) .build(); CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition); googleMap.animateCamera(cameraUpdate); if (null != googleMap) { googleMap.addMarker(new MarkerOptions() .position(new LatLng(lat, lng)) .title("Mark") .draggable(false) ); } } @Override public void onMapReady(GoogleMap googleMap) { this.googleMap = googleMap; addMarker(); } } 

Before that there was a mistake in the code itself,

Cannot resolve method getMap

In the code section:

 try { if(null == googleMap){ googleMap = ((MapFragment) getFragmentManager().findFragmentById( R.id.mapView)).getMap(); 

But I decided to replace getMap with getMapAsync , and also implement the OnMapReadyCallback class. Perhaps this is the cause of errors.

layout code activation:

 <?xml version="1.0" encoding="utf-8"?> 

 <fragment android:id="@+id/mapView" android:name="com.google.android.gms.maps.MapFragment" android:layout_width="match_parent" android:layout_height="match_parent" /> 

    2 answers 2

    Try to mark your map with a marker as follows:

    layout code:

     <fragment xmlns:android="http://schemas.android.com/apk/res/android" android:name="com.google.android.gms.maps.MapFragment" android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent"/> 

    In your createMapView method, leave this for now:

      private void createMapView() { MapFragment mapFragment = (MapFragment) getFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); } 

    Well, in the onMapReady method:

     @Override public void onMapReady(GoogleMap map) { map.addMarker(new MarkerOptions() .position(new LatLng(0, 0)) .title("Marker")); } 

    Before you implement:

     public class MapActivity extends FragmentActivity implements OnMapReadyCallback 

    And also here is a link to help you, I hope it will help.

      You need to call addMarker () in the onMapReady method. onMapReady is a method that is called when the map is ready for use. You need to do the initial settings in it, and draw the markers in it. Also call the getMapAsync (this) method in the onCreate method;

      • Thanks for the reaction, the marker appeared, but on a white screen, because it was not possible to create a map. Flashing to map - Error creating map. Please explain exactly where to specify getMapAsync (this) because I already have it in the code. - Morozov
      • Did you register the sha1 key in google console developer? - pavel163
      • Yes, it seems like everything is correct. In the manifesto also put down the key. - Morozov
      • You in the console must provide those keys that sign the release assembly, the one with which you sign the assembly debug - pavel163
      • Show the layout where you insert the map fragment - pavel163