mapbox / mapbox-gl-native

Interactive, thoroughly customizable maps in native Android, iOS, macOS, Node.js, and Qt applications, powered by vector tiles and OpenGL
https://mapbox.com/mobile
Other
4.37k stars 1.33k forks source link

addImages() images are disappearing from the style #16516

Open csimpi opened 4 years ago

csimpi commented 4 years ago

Platform: Android Mapbox SDK version: 9.2.0

Steps to trigger behavior

  1. Generate and use addImages() to add images to the style.
  2. Update the layer -> new images are appearing
  3. Zoom out then zoom in, and the images won't appear

Long story: I would like to add donut-chart clusters to my map with Android SDK (the same as here, but with Android SDK: https://docs.mapbox.com/mapbox-gl-js/example/cluster-html/)

Here's my totally unanswered question on Stackoverflow: https://stackoverflow.com/questions/63162432/display-donut-chart-clusters-with-custom-properties-in-mapbox-android-sdk

Finally, I've solved the problem with generating bitmaps as clusters add them with addImages() and reload the style. It works but a bit slow so I want to skip generating and adding images for the cluster ids that have been added before, but those are disappearing.

Example code (NativeScript + Angular):

  reloadClusters(){
    let cluster_features = this.ChecklistMarkersSource.querySourceFeatures(this.mapboxSdk.style.expressions.Expression.raw('["has","cluster"]'));
    if(!cluster_features.isEmpty()) {
      //console.log('CLUSTERS',cluster_features);
      let imagesMap = new java.util.HashMap();
      for (let i = 0; i < cluster_features.size(); i++) {
        let feature = cluster_features.get(i);
        let id = feature.getProperty('cluster_id').getAsString();
        if(this.Clusters[id] === true){
          console.log('IMAGE EXISTS SKIP IT!', id);
          continue;
        } else{
          console.log('IMAGE MISSING MAKE IT...', id);

        }
        //console.log('CLUSTER',feature,feature.getProperty('point_count'),);
        let start_angle = 0;
        let imageBitmap = android.graphics.Bitmap.createBitmap(100, 100, android.graphics.Bitmap.Config.ARGB_8888);
        let canvas = new    android.graphics.Canvas(imageBitmap);
        let p = new android.graphics.Paint();

        let rectF = new android.graphics.RectF(0, 0, 100, 100);
        let point_count = feature.getProperty('point_count');

        //Generate the chart segment images for the different checklist types and for the visited data
        let types:any = Object.assign({visited:{color:"#609018"}},this.ChecklistAttributes);
        for(let type in types) {
          if (feature.getProperty(type)) {
            //console.log('CLUSTER DATA OF ',type, feature.getProperty('cluster'), feature.getProperty(type));
            let type_count = feature.getProperty(type);
            let percent = (type_count/point_count)*100;
            let angle = (360/100) * percent;

            p.setColor(android.graphics.Color.parseColor(types[type].color));
            canvas.drawArc (rectF, start_angle, angle, true, p);
            start_angle += angle;
          }
        }

        //Inner circle
        rectF = new android.graphics.RectF(20, 20, 80, 80);
        p.setColor(android.graphics.Color.WHITE);
        canvas.drawArc (rectF, 0, 360, true, p);
        //Counter
        p.setColor(android.graphics.Color.BLACK);
        p.setTextAlign(android.graphics.Paint.Align.CENTER);
        p.setTextSize(42);
        canvas.drawText(feature.getProperty('point_count').getAsString(), 50,65,p);

        //feature.getProperty(type)feature.getProperty('cluster_id')
        imagesMap.put(id,imageBitmap);
        this.Clusters[id] = true;
      }

      this.mapStyle.addImages(imagesMap);
      this.updateGeoJsonSource(); // this.ChecklistMarkersSource.setGeoJson(JSON.stringify(this.ChecklistsGeoJson));
    }
  }

Some reason if I zoom out/in/move the clusters are appearing only ONE time, then I zoom/move back and the same images won't' appear anymore. In short: The same cluster appears only one time then never again.

This might be related: https://github.com/mapbox/mapbox-gl-js/issues/8478

Expected behavior

If I add images to the style that image should be available permanently for the same session and disappear only when the user destroys reloads the style.

Actual behavior

The added images are disappearing and appearing only one time.

Comment

I've opened a ticket with support for more than a week ago and got 0 answers....