The onMapReady method needs to be reloaded. The following describes how to create a map instance using SupportMapFragment. For more details, see docs.
- Add a Fragment object in the layout file (for example, activity_main.xml), and set map attributes in the file
<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" />
- To use a map in your app, implement the OnMapReadyCallback API in the MainActivity.java file.
The sample code is as follows:
public class SupportMapDemoActivity extends AppCompatActivity implements OnMapReadyCallback {
...
}
- In the MainActivity.java file, load SupportMapFragment in the onCreate() method and call getMapAsync() to register the callback.
The sample code is as follows:
private SupportMapFragment mSupportMapFragment;
mSupportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapfragment_mapfragmentdemo);
mSupportMapFragment.getMapAsync(this);
- Call the onMapReady callback to obtain the HuaweiMap object.
The sample code is as follows:
public void onMapReady(HuaweiMap huaweiMap) {
Log.d(TAG, "onMapReady: ");
hMap = huaweiMap;
}
- Run your project and then install your app to view the map in your app.