0

I'm attempting to create a heatmap using the google api, but have come across a small problem.

Here is the sample code:

 mProvider = new HeatmapTileProvider.Builder()
                    .weightedData((Collection<WeightedLatLng>) weightedList)
                    .gradient(gradient);
         mOverlay = mMap.addTileOverlay(new TileOverlayOptions().tileProvider((TileProvider) mProvider));// this is the line of code that produces the error
    }

In particular, the argument "mProvider" in that line is highlighted.

The error log is:

java.lang.ClassCastException: com.google.maps.android.heatmaps.HeatmapTileProvider$Builder cannot be cast to com.google.android.gms.maps.model.TileProvider
Nimantha
  • 6,405
  • 6
  • 28
  • 69
HorribleCoder
  • 29
  • 1
  • 6

2 Answers2

0

If you set up your dependencies right, you should have everything to work correctly. And you should have no need for thinking about casting. because as in documentation, your code should be.

      mOverlay = mMap.addTileOverlay(new TileOverlayOptions().tileProvider(mProvider));

My problem (the difference is a few years, and it is Kotlin, not Java, but still gradle problem and not in code :) was this dependency

implementation 'com.google.maps.android:maps-v3-ktx:2.2.0' 

and one that works correctly is

implementation 'com.google.maps.android:maps-ktx:2.3.0'
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Nenad Štrbić
  • 367
  • 4
  • 6
-1

You forgot to call .build() method.
It's like you want to build a car, and do like Car car = new CarFactory().type(SEDAN).color(BLACK); and forgetting to tell the carFactory to build the car. It results in that you try to assign a carFactory to a Car variable.
Your code should look like this:

 mProvider = new HeatmapTileProvider.Builder()
                .weightedData((Collection<WeightedLatLng>) weightedList)
                .gradient(gradient)
                .build(); //missing this line
Vladyslav Matviienko
  • 10,610
  • 4
  • 33
  • 52