I have this code but when I try to refresh the heatmap points, nothing happens. I don't know if I need something else, I've looked at examples but in my case it doesn't work. I'm using fragments.
Is there some other way to refresh a heatmap layer without rebuilding the map object?
public class tab2 extends Fragment{
MapView mapView;
GoogleMap MygoogleMap;
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.tab2, container, false);
mapView = (MapView) view.findViewById(R.id.mi_mapa);
mapView.onCreate(savedInstanceState);
MygoogleMap = mapView.getMap();
MygoogleMap.setMyLocationEnabled(true);
MapsInitializer.initialize(this.getActivity());
latitude = 18.916;
longitude = -99.236;
MygoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude,
longitude), 3.5f));
return view;
}
public void setHotPoints(){
Handler_sqlite_mapa helper = new Handler_sqlite_mapa(this.miContexto);
helper.abrir(); //Abre la conexion a la BD
String resultado[][] = helper.getTop(99);
helper.cerrar();
int lecturasConfirmadas=0;
int columnas = (resultado.length);
int filas = (resultado[0].length);
Log.d("MiMapa:", "Columnas:" + columnas);
Log.d("MiMapa:", "Filas:" + filas);
if (mProvider == null) {
List<LatLng> list = new ArrayList<LatLng>();
String valores = "";
for(int i = 0; i < filas; i++) {
if(resultado[3][i]!=null){
double la = Double.parseDouble(resultado[1][i]);
double lo = Double.parseDouble(resultado[2][i]);
list.add(new LatLng(la,lo));
lecturasConfirmadas++;
}
}
if(lecturasConfirmadas>0){
MygoogleMap.clear();
mProvider = new HeatmapTileProvider.Builder()
.data(list)
.build();
mOverlay = MygoogleMap.addTileOverlay(new TileOverlayOptions().tileProvider(mProvider));
mOverlay.clearTileCache();
}
}
}
}
Not sure if there is any better approach, I am using remove and recreate to force update heat map:
private void removeHeatMap() {
mOverlay.remove();
}
private void addHeatMap() {
mProvider = new HeatmapTileProvider.Builder()
.weightedData(samples)
.build();
mOverlay = mMap.addTileOverlay(new TileOverlayOptions().tileProvider(mProvider));
}
One thing that I noticed is that if I didn't recreate the HeatmapTileProvider instance with the overlay, the heatmap will not be updated properly.
clearTileCache odes the trick.
if(mHeatmapTileOverlay == null) {
mHeatmapTileOverlay = mMap.addTileOverlay(options);
} else {
mHeatmapTileOverlay.clearTileCache();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With