We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All

Ground Overlay

A ground overlay is an image fixed on a map.

Adding a Ground Overlay

  1. Use BitmapDescriptorFactory to create a BitmapDescriptor object.
    The sample code is as follows:
    Java
    Kotlin
    Bitmap bitmap = BitmapFactory.decodeStream(getAssets().open("images/avocado.jpg"));
    String fileName = "maomao.jpg";
    String path = "/data/data/com.huawei.hms.maps.demo/avocado.jpg";
    BitmapDescriptor descriptorFromAsset = BitmapDescriptorFactory.fromAsset("images/avocado.jpg");
    BitmapDescriptor descriptorFromBitmap = BitmapDescriptorFactory.fromBitmap(bitmap);
    BitmapDescriptor descriptorFromFile = BitmapDescriptorFactory.fromFile(fileName);
    BitmapDescriptor descriptorFromPath = BitmapDescriptorFactory.fromPath(path);
    BitmapDescriptor descriptorFromResource = BitmapDescriptorFactory.fromResource(R.drawable.makalong);
    val bitmap = BitmapFactory.decodeStream(assets.open("images/avocado.jpg"))
    val fileName = "maomao.jpg"
    val path = "/data/data/com.huawei.hms.maps.demo/avocado.jpg"
    val descriptorFromAsset = BitmapDescriptorFactory.fromAsset("images/avocado.jpg")
    val descriptorFromBitmap = BitmapDescriptorFactory.fromBitmap(bitmap)
    val descriptorFromFile = BitmapDescriptorFactory.fromFile(fileName)
    val descriptorFromPath = BitmapDescriptorFactory.fromPath(path)
    val descriptorFromResource = BitmapDescriptorFactory.fromResource(R.drawable.makalong)
  2. Create a GroundOverlayOptions object to determine the image and position of the ground overlay to be added.
    The sample code is as follows:
    Java
    Kotlin
    String path = "/data/data/com.huawei.hms.maps.demo/avocado.jpg";
    BitmapDescriptor descriptor = BitmapDescriptorFactory.fromPath(path);
    GroundOverlayOptions options =
        new GroundOverlayOptions().position(new LatLng(48.893478, 2.334595), 60, 30).image(descriptor);
    var path = "/data/data/com.huawei.hms.maps.demo/avocado.jpg";
    var descriptor = BitmapDescriptorFactory.fromPath(path);
    val options = GroundOverlayOptions().position(LatLng(48.893478, 2.334595), 60f, 30f).image(descriptor)
  3. Use the addGroundOverlay(GroundOverlayOptions) method of the HuaweiMap object to add a ground overlay, as shown in Figure 1. The method returns the GroundOverlay object.
    The sample code is as follows:
    Java
    Kotlin
    private GroundOverlay mGroundOverlay;
    
    // Add a ground overlay on the map.
    mGroundOverlay = hMap.addGroundOverlay(options);
    private var mGroundOverlay: GroundOverlay? = null
    
    // Add a ground overlay on the map.
    mGroundOverlay = hMap.addGroundOverlay(options)
    Figure 1 Ground overlay
  4. Modify and remove the ground overlay.
    The sample code is as follows:
    Java
    Kotlin
    if (null != mGroundOverlay) {
        // Set a ground overlay.
        mGroundOverlay.setImage(BitmapDescriptorFactory.fromResource(R.drawable.makalong));
    }
    
    if (null != mGroundOverlay) {
        // Remove the ground overlay.
        mGroundOverlay.remove();
    }
    if (null != mGroundOverlay) {
        // Set a ground overlay.
        mGroundOverlay?.setImage(BitmapDescriptorFactory.fromResource(R.drawable.makalong))
    }
    
    if (null != mGroundOverlay) {
        // Remove the ground overlay.
        mGroundOverlay?.remove()
    }
  5. Set the position of the ground overlay.
    NOTE

    position(LatLng location, float width) and position(LatLng location, float width, float height) cannot be used together with positionFromBounds(LatLngBounds bounds). Otherwise, IllegalStateException will be thrown.

    • public GroundOverlayOptions position(LatLng location, float width)

      This method sets the position of a ground overlay based on the longitude and latitude and image width (in meters). The image height is automatically calculated and adjusted based on the aspect ratio and width of the image.

    • public GroundOverlayOptions position(LatLng location, float width, float height)

      This method sets the position of a ground overlay based on the longitude and latitude, image width (in meters), and image height (in meters). The image will be zoomed to adapt to the specified resolutions and may be deformed.

    • public GroundOverlayOptions positionFromBounds(LatLngBounds bounds)

      This method specifies a LatLngBounds object that provides two LatLng objects (southwest corner and northeast corner) to form a rectangle area. It will set the position of a ground overlay based on the rectangular area.

Listening for Ground Overlay Events

Ground Overlay Tap Event

You can use HuaweiMap.OnGroundOverlayClickListener to listen for ground overlay tap events. To set a listener on a map, call the setOnGroundOverlayClickListener(HuaweiMap.OnGroundOverlayClickListener) method of the HuaweiMap object. When a user taps a ground overlay, onGroundOverlayClick(GroundOverlay) calls the ground overlay.

The sample code is as follows:
Java
Kotlin
hMap.setOnGroundOverlayClickListener(new HuaweiMap.OnGroundOverlayClickListener() {
    @Override
    public void onGroundOverlayClick(GroundOverlay groundOverlay) {
        Toast.makeText(getApplicationContext(), "GroundOverlay is clicked.", Toast.LENGTH_LONG).show();
    }
});
hMap.setOnGroundOverlayClickListener { Toast.makeText(applicationContext, "GroundOverlay is clicked.", Toast.LENGTH_LONG).show() }
Search
Enter a keyword.