目录

1 Overview

This section describes how to use a map container to quickly obtain a map object, change the map type, display my-location on the map, and enable the map's lite mode. For details, please refer to files such as MapViewDemoActivity.java and MapFragmentDemoActivity.java in the sample code project. Click here to download sample code.

2 Map Instance Creation

Map Container

Currently, the HMS Core Map SDK supports two map containers: MapFragment and MapView.

MapFragment is a subclass of the Android Fragment class. You can use it to place a map within a fragment. It can also function as a map container and provide an entry for accessing a HuaweiMap object.

MapView is a subclass of the Android View class. You can use it to place a map within an Android view. Like MapFragment, MapView functions as a map container and displays core map functions through a HuaweiMap object. To use the MapView class when calling APIs in normal interaction mode, you must call the following methods in the corresponding methods of your activity/fragment: onCreate(), onStart(), onResume(), onPause(), onStop(), onDestroy(), onSaveInstanceState(Bundle outState), and onLowMemory(). For details, please refer to the MapViewCodeDemoActivity.java file.

The following describes how to create a map instance using MapFragment.

1. Add a MapFragment in the layout file of an activity, and set the map attributes using the XML file.

  1. <fragment xmlns:android="http://schemas.android.com/apk/res/android"
  2.    xmlns:map="http://schemas.android.com/apk/res-auto"
  3.    android:id="@+id/mapfragment_mapfragmentdemo"
  4.    class="com.huawei.hms.maps.MapFragment"
  5.    android:layout_width="match_parent"
  6.    android:layout_height="match_parent"
  7.    map:cameraTargetLat="48.893478"
  8.    map:cameraTargetLng="2.334595"
  9.    map:cameraZoom="10" />

2. To use a map in your app, you need to implement the OnMapReadyCallback API.

  1. public class MapFragmentDemoActivity extends AppCompatActivity implements OnMapReadyCallback {
  2.    ...
  3. }

3. Load MapFragment in the onCreate() method of your activity and call getMapAsync() to register the callback.

  1. private MapFragment mMapFragment;
  2. mMapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.mapfragment_mapfragmentdemo);
  3. mMapFragment.getMapAsync(this);

4. Call the onMapReady callback to obtain the HuaweiMap object.

  1. public void onMapReady(HuaweiMap map) {
  2.    Log.d(TAG, "onMapReady: ");
  3.    hMap = map;
  4. }

5. Run your project to view the map in your app, as shown in the screenshot below.

Figure 1: Creating a map using MapFragment


The following describes how to create a map instance using MapView.

Procedure

1. Add a MapView in the layout file of an activity, and set the map attributes using the XML file.

  1. <com.huawei.hms.maps.MapView xmlns:android="http://schemas.android.com/apk/res/android"
  2.    xmlns:map="http://schemas.android.com/apk/res-auto"
  3.    android:id="@+id/mapview_mapviewdemo"
  4.    android:layout_width="match_parent"
  5.    android:layout_height="match_parent"
  6.    map:cameraTargetLat="48.893478"
  7.    map:cameraTargetLng="2.334595"
  8.    map:cameraZoom="10" />

2. To use a map in your app, you need to implement the OnMapReadyCallback API.

  1. public class MapViewDemoActivity extends AppCompatActivity implements OnMapReadyCallback {
  2.    ...
  3. }

3. Load MapView in the onCreate() method of your activity and call getMapAsync() to register the callback.

  1. private MapView mMapView;
  2. mMapView = findViewById(R.id.mapview_mapviewdemo);
  3. Bundle mapViewBundle = null;
  4. if (savedInstanceState != null) {
  5.    mapViewBundle = savedInstanceState.getBundle("MapViewBundleKey");
  6. }
  7. mMapView.onCreate(mapViewBundle);
  8. mMapView.getMapAsync(this);

4. Call the onMapReady callback to obtain the HuaweiMap object.

  1. public void onMapReady(HuaweiMap map) {
  2.    Log.d(TAG, "onMapReady: ");      
  3.    hMap = map;  
  4. }

5. Call the onStart(), onStop(), onResume(), onPause(), onDestroy(), onLowMemory(), and onSaveInstanceState(Bundle outState) methods of the MapView class in corresponding methods of your activity/fragment.

  1. private static final String MAPVIEW_BUNDLE_KEY = "MapViewBundleKey";
  2. @Override
  3. public void onSaveInstanceState(Bundle outState) {
  4.    super.onSaveInstanceState(outState);
  5.    Bundle mapViewBundle = outState.getBundle(MAPVIEW_BUNDLE_KEY);
  6.    if (mapViewBundle == null) {
  7.        mapViewBundle = new Bundle();
  8.        outState.putBundle(MAPVIEW_BUNDLE_KEY, mapViewBundle);
  9.    }
  10.    mMapView.onSaveInstanceState(mapViewBundle);
  11. }
  12. @Override
  13. protected void onStart() {
  14.    super.onStart();
  15.    mMapView.onStart();
  16. }
  17. @Override
  18. protected void onResume() {
  19.    super.onResume();
  20.    mMapView.onResume();
  21. }
  22. @Override
  23. protected void onPause() {
  24.    mMapView.onPause();
  25.    super.onPause();
  26. }
  27. @Override
  28. protected void onStop() {
  29.    super.onStop();
  30.    mMapView.onStop();
  31. }
  32. @Override
  33. protected void onDestroy() {
  34.    super.onDestroy();
  35.    mMapView.onDestroy();
  36. }
  37. @Override
  38. public void onLowMemory() {
  39.    super.onLowMemory();
  40.    mMapView.onLowMemory();
  41. }

6. Run your project to view the map in your app, as shown in the screenshot below.

Figure 2: Creating a map using MapView

3 Map Type

Currently, HuaweiMap supports two types of maps:


To set the map type, call the setMapType method of the HuaweiMap object and transfer the static constant defined in HuaweiMap. 

To set an empty map, call the setMapType(HuaweiMap.MAP_TYPE_NONE) method of the HuaweiMap object. Figure 3 shows the map display effect.

To set a standard map, call the setMapType(HuaweiMap.MAP_TYPE_NORMAL) method of the HuaweiMap object. Figure 4 shows the map display effect.

Figure 3: Empty map

       

Figure 4: Standard map

4 My Location

The my-location function allows users to get their current locations when using your app. Before using this function, ensure that your app has been granted the location permission. The Android OS provides two location permissions: ACCESS_COARSE_LOCATION (approximate location permission) and ACCESS_FINE_LOCATION (precise location permission).You need to apply for the permissions in the Manifest file.

  1. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
  2. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

After adding the preceding information to the Manifest file, you need to dynamically apply for the permissions in the code (according to risky permission requirements in Android 6.0).

  1. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  2.    Log.i(TAG, "sdk >= 23 M");
  3.    if (ActivityCompat.checkSelfPermission(this,
  4.        Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
  5.        || ActivityCompat.checkSelfPermission(this,
  6.            Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  7.        String[] strings =
  8.            {Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};
  9.        ActivityCompat.requestPermissions(this, strings, 1);
  10.    }
  11. }

To enable this function, call the setMyLocationEnabled(true) method of the HuaweiMap object. 

  1. @RequiresPermission(allOf = {ACCESS_FINE_LOCATION, ACCESS_WIFI_STATE})
  2. @Override
  3. public void onMapReady(HuaweiMap map){
  4.    hMap = map;
  5.    hMap.setMyLocationEnabled(true);// Enable the my-location overlay.
  6.    hMap.getUiSettings().setMyLocationButtonEnabled(true);// Enable the my-location icon.
  7. }

Once this function has been enabled, the my-location icon will be displayed in the lower right corner of the map by default and the user's current location will be displayed as a blue dot (Figure 5). When a user taps this icon, the user's current location (if obtained) will be displayed in the center of the screen (Figure 6). After you set a listener for the my-location icon and a user taps the icon, the onMyLocationButtonClick() callback of HuaweiMap.OnMyLocationButtonClickListener will be triggered.

You can also call getUiSettings().setMyLocationButtonEnabled(false) of the HuaweiMap object to hide the my-location icon.

Figure 5: Screen before a user taps the my-location icon


Figure 6: Screen after a user taps the my-location icon

5 Map Attribute Settings

You can set initial map attributes using XML or in code and dynamically update them later.

Setting Attributes Using XML

The HMS Core Map SDK defines a group of custom XML attributes for MapFragment and MapView. You can directly add these attributes in the layout file of a map as the map's initial attributes.

Before using these attributes, ensure that you have declared the following namespace:

  1. xmlns:map="http://schemas.android.com/apk/res-auto"

After declaring the namespace, you can add the following attributes as the initial attributes of a map:

  1. <fragment xmlns:android="http://schemas.android.com/apk/res/android"
  2.    xmlns:map="http://schemas.android.com/apk/res-auto"
  3.    android:id="@+id/mapfragment_mapfragmentdemo"
  4.    class="com.huawei.hms.maps.MapFragment"
  5.    android:layout_width="match_parent"
  6.    android:layout_height="match_parent"
  7.    map:cameraBearing="45"
  8.    map:cameraMaxZoomPreference="13"
  9.    map:cameraMinZoomPreference="3"
  10.    map:cameraTargetLat="48.893478"
  11.    map:cameraTargetLng="2.334595"
  12.    map:cameraTilt="20"
  13.    map:cameraZoom="10"
  14.    map:latLngBoundsNorthEastLatitude="49.893478"
  15.    map:latLngBoundsNorthEastLongitude="1.334595"
  16.    map:latLngBoundsSouthWestLatitude="47.893478"
  17.    map:latLngBoundsSouthWestLongitude="3.334595"
  18.    map:liteMode="false"
  19.    map:mapType="normal"
  20.    map:uiCompass="true"
  21.    map:uiRotateGestures="false"
  22.    map:uiScrollGestures="true"
  23.    map:uiTiltGestures="true"
  24.    map:uiZoomControls="false"
  25.    map:uiZoomGestures="true"
  26.    map:useViewLifecycle="true"
  27.    map:zOrderOnTop="true" />

Setting Attributes in Code

You can also set initial attributes for a map in code and update these attributes when the camera status needs to be updated. All XML attributes can be set in code.

  1. MapFragment mMapFragment;
  2. CameraPosition cameraPosition =
  3.    CameraPosition.builder().target(new LatLng(48.893478, 2.334595)).zoom(10).bearing(45).tilt(20).build();
  4. LatLng southwest = new LatLng(47.893478, 3.334595);
  5. LatLng northeast = new LatLng(49.893478, 1.334595);
  6. LatLngBounds latLngBounds = new LatLngBounds(southwest, northeast);
  7. HuaweiMapOptions options = new HuaweiMapOptions();
  8. options.mapType(HuaweiMap.MAP_TYPE_NORMAL)
  9.    .camera(cameraPosition)
  10.    .zoomControlsEnabled(false)
  11.    .compassEnabled(true)
  12.    .zoomGesturesEnabled(true)
  13.    .scrollGesturesEnabled(true)
  14.    .rotateGesturesEnabled(false)
  15.    .tiltGesturesEnabled(true)
  16.    .zOrderOnTop(true)
  17.    .useViewLifecycleInFragment(true)
  18.    .liteMode(false)
  19.    .minZoomPreference(3)
  20.    .maxZoomPreference(13)
  21.    .latLngBoundsForCameraTarget(latLngBounds);
  22. mMapFragment = MapFragment.newInstance(options);

6 Lite Mode (Not supported currently)

Overview

In the lite mode, the HMS Core Map SDK allows your app to show the static map image of a specified location at a specified zoom level to a user. The lite mode supports only static image–based functions. It is useful when the user wants to show multiple maps on one screen, or share their location using WeChat Moments.

The following table lists the functions supported by the HMS Core Map SDK in lite mode.

FunctionSupport
MapFragment and MapViewCompletely supported
My locationCompletely supported
Interaction gestureNot supported
Map cameraPartially supported (Tilt and bearing cannot be set and animation is not supported.)
Map eventPartially supported (Only tap and long press events are supported.)
Map type

Partially supported (Only standard maps and empty maps are supported.)

MarkerPartially supported (Dragging and rotation are not supported.)
Marker information windowCompletely supported
GroundOverlayNot supported
Shape (Circle/Polyline/Polygon)Completely supported
Custom map styleNot supported


Usage

You can enable the lite mode in either of the following ways:

1. Configure the XML file as follows:

  1. <com.huawei.hms.maps.MapView xmlns:android="http://schemas.android.com/apk/res/android"
  2.   xmlns:map="http://schemas.android.com/apk/res-auto"
  3.   android:id="@+id/mapView"
  4.   android:layout_width="match_parent"
  5.   android:layout_height="match_parent"
  6.   map:mapType="normal"        
  7.   map:liteMode="true"        
  8.   map:uiCompass="true"
  9.   map:uiZoomControls="true"
  10.   map:cameraTargetLat="51"
  11.   map:cameraTargetLng="10"
  12.   map:cameraZoom="8.5"/>

2. Add the following code:

  1. private MapView mMapView;  
  2. @Override        
  3. protected void onCreate(Bundle savedInstanceState) {
  4.    super.onCreate(savedInstanceState);
  5.    setContentView(R.layout.activity_map_lite_mode);
  6.    HuaweiMapOptions huaweiMapOptions = new HuaweiMapOptions();
  7.    huaweiMapOptions.liteMode(true);//Enable the lite mode for the map.
  8.    mMapView = new MapView(this, huaweiMapOptions);
  9.    Bundle mapViewBundle = null;
  10.    if (savedInstanceState != null) {
  11.        mapViewBundle = savedInstanceState.getBundle("MapViewBundleKey");
  12.    }
  13.    mMapView.onCreate(mapViewBundle);
  14.    mMapView.getMapAsync(this);
  15.    //Add mMapView to the page.
  16.   ((LinearLayout)findViewById(R.id.view_main)).addView(mMapView, 0);
  17. }

7 Map Padding

HUAWEI Map Kit is implemented by simulating a camera. The map camera area centers on the target location and will be padded. Map controls (such as the compass, zoom icons, and my-location icon) and Huawei logo are placed at the border of the map camera area. By default, the compass is placed in the upper right corner of the map camera area, the zoom icons and my-location icon are placed in the lower right corner of the map camera area, and the Huawei logo is placed in the lower left corner of the map camera area.

You can use the HuaweiMap.setPadding(int left, int top, int right, int bottom) method to set the padding between the map camera area bounds and map controls or HUAWEI logo. Input parameters of the method are as follows:

Figures 7 and 8 show the default and custom map padding effects, respectively.

Figure 7: Default padding effect


Figure 8: Custom padding effect