Currently, the Map SDK supports the following map containers: MapView, MapFragment, SupportMapFragment, TextureMapView, TextureMapFragment, and TextureSupportMapFragment. SupportMapFragment and MapView are commonly used map containers, which are described as follows:
The following describes how to create a map instance using SupportMapFragment.
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/mapfragment_mapfragmentdemo"
class="com.huawei.hms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraTargetLat="48.893478"
map:cameraTargetLng="2.334595"
map:cameraZoom="10" /> Java
public class SupportMapDemoActivity extends AppCompatActivity implements OnMapReadyCallback {
...
} Kotlin
class SupportMapDemoActivity : AppCompatActivity(),OnMapReadyCallback {
...
} Load SupportMapFragment and call getMapAsync() to register a callback after the initialization is complete.
Java
// Initialize the SDK.
MapsInitializer.initialize(this);
setContentView(R.layout.mapfragment_mapfragmentdemo);
SupportMapFragment mSupportMapFragment;
mSupportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapfragment_mapfragmentdemo);
mSupportMapFragment.getMapAsync(this); Kotlin
// Initialize the SDK.
MapsInitializer.initialize(this);
setContentView(R.layout.mapfragment_mapfragmentdemo);
var mSupportMapFragment: SupportMapFragment? = null
mSupportMapFragment = supportFragmentManager.findFragmentById(R.id.mapfragment_mapfragmentdemo) as SupportMapFragment?
mSupportMapFragment?.getMapAsync(this)
You are advised to initialize the SDK after the user accepts the privacy statement.
Java
public void onMapReady(HuaweiMap huaweiMap) {
Log.d(TAG, "onMapReady: ");
hMap = huaweiMap;
} Kotlin
override fun onMapReady(huaweiMap: HuaweiMap) {
Log.d(TAG, "onMapReady: ")
hMap = huaweiMap
} 
The following describes how to create a map instance using MapView.
<com.huawei.hms.maps.MapView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/mapview_mapviewdemo"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraTargetLat="48.893478"
map:cameraTargetLng="2.334595"
map:cameraZoom="10"/> Java
public class MapViewDemoActivity extends AppCompatActivity implements OnMapReadyCallback {
...
} Kotlin
class MapViewDemoActivity : AppCompatActivity(), OnMapReadyCallback {
...
} Java
// Initialize the SDK.
MapsInitializer.initialize(this);
MapView mMapView;
mMapView = findViewById(R.id.mapview_mapviewdemo);
Bundle mapViewBundle = null;
if (savedInstanceState != null) {
mapViewBundle = savedInstanceState.getBundle("MapViewBundleKey");
}
mMapView.onCreate(mapViewBundle);
mMapView.getMapAsync(this); Kotlin
// Initialize the SDK.
MapsInitializer.initialize(this);
private lateinit var mMapView: MapView
mMapView = findViewById(R.id.mapview_mapviewdemo)
var mapViewBundle: Bundle? = null
if (savedInstanceState != null) {
mapViewBundle = savedInstanceState.getBundle("MapViewBundleKey")
}
mMapView.onCreate(mapViewBundle)
mMapView.getMapAsync(this)
You are advised to initialize the SDK after the user accepts the privacy statement.
Java
public void onMapReady(HuaweiMap huaweiMap) {
Log.d(TAG, "onMapReady: ");
hMap = huaweiMap;
} Kotlin
override fun onMapReady(huaweiMap: HuaweiMap) {
Log.d(TAG, "onMapReady: ")
hMap = huaweiMap
} Java
private static final String MAPVIEW_BUNDLE_KEY = "MapViewBundleKey";
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Bundle mapViewBundle = outState.getBundle(MAPVIEW_BUNDLE_KEY);
if (mapViewBundle == null) {
mapViewBundle = new Bundle();
outState.putBundle(MAPVIEW_BUNDLE_KEY, mapViewBundle);
}
mMapView.onSaveInstanceState(mapViewBundle);
}
@Override
protected void onStart() {
super.onStart();
mMapView.onStart();
}
@Override
protected void onResume() {
super.onResume();
mMapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mMapView.onPause();
}
@Override
protected void onStop() {
super.onStop();
mMapView.onStop();
}
@Override
protected void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mMapView.onLowMemory();
} Kotlin
companion object {
private const val MAPVIEW_BUNDLE_KEY = "MapViewBundleKey"
}
override fun onSaveInstanceState(outState: Bundle?) {
super.onSaveInstanceState(outState)
var mapViewBundle: Bundle? = outState?.getBundle(MAPVIEW_BUNDLE_KEY)
if (mapViewBundle == null) {
mapViewBundle = Bundle()
outState?.putBundle(MAPVIEW_BUNDLE_KEY, mapViewBundle)
}
mMapView.onSaveInstanceState(mapViewBundle)
}
override fun onStart() {
super.onStart()
mMapView.onStart()
}
override fun onResume() {
super.onResume()
mMapView.onResume()
}
override fun onPause() {
super.onPause()
mMapView.onPause()
}
override fun onStop() {
super.onStop()
mMapView.onStop()
}
override fun onDestroy() {
super.onDestroy()
mMapView.onDestroy()
}
override fun onLowMemory() {
super.onLowMemory()
mMapView.onLowMemory()
}
When creating a map using MapView, you must override the onDestroy() method of your activity or fragment so that the onDestroy() method of MapView can be called, preventing the created map from memory leaks. When creating a map using multi-entity objects such as ListView or RecyclerView, you also need to call the onDestroy() method of each entity. Otherwise, memory leaks may occur.

In the Map SDK 5.0.0.300 or later for Android, you must set the API key in either of the following ways before initializing the map:
Way 1: Directly copy the API key in api_key in the agconnect-services.json file.
Java // In the entry class (inherited from android.app.Application) of the app, call this method in the overridden onCreate() method. public class MyApp extends Application { @Override public void onCreate() { super.onCreate(); // Set the API key. MapsInitializer.setApiKey("Your API key"); } }
Kotlin // In the entry class (inherited from android.app.Application) of the app, call this method in the overridden onCreate() method. class MyApp : Application() { override fun onCreate() { super.onCreate() // Set the API key. MapsInitializer.setApiKey("Your API key") } }
Java @Override protected void onCreate(Bundle savedInstanceState) { Log.i(TAG, "onCreate: "); super.onCreate(savedInstanceState); // Set the API key before calling setContentView. MapsInitializer.setApiKey("Your API key"); setContentView(R.layout.basic_demo); }
Kotlin override fun onCreate(savedInstanceState: Bundle?) { Log.i(TAG, "onCreate: ") super.onCreate(savedInstanceState) // Set the API key before calling setContentView. MapsInitializer.setApiKey("Your API key") setContentView(R.layout.basic_demo) }