How do I update the points for a Heatmap layer in the Google Maps Android API library?
I have this code, but when I try to update the Heatmap points, nothing happens. I don't know if I need something else, I looked at examples, but it doesn't work in my case. I am using fragments.
Is there another way to update the heatmap layer without restoring 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();
}
}
}
}
+3
source to share
1 answer
Not sure if there is any better approach, I use delete and recreate to force the heatmap to update:
private void removeHeatMap() {
mOverlay.remove();
}
private void addHeatMap() {
mProvider = new HeatmapTileProvider.Builder()
.weightedData(samples)
.build();
mOverlay = mMap.addTileOverlay(new TileOverlayOptions().tileProvider(mProvider));
}
One thing I noticed is that if I hadn't recreated the HeatmapTileProvider instance with an overlay, then the heatmap would not update as expected.
0
source to share