private async Task CreateHeatMap() { BeginLoadingIcon(); var samples = await sampleTable.ToCollectionAsync(); samplesList = samples.Where(s => s.PatientID == StoredData.getPatientID()).ToList(); //Create a list contains all groups of samples with distance up to 100m List <List <Sample> > sampleGroups = new List <List <Sample> >(); List <int> sampleIndexesAdded = new List <int>(); int maxGroupCount = 0; for (int i = 0; i < samplesList.Count; i++) { //Get group's index that contains the current sample int groupIndex = -1; for (int j = 0; j < sampleGroups.Count; j++) { if (sampleGroups[j].Contains(samplesList[i])) { groupIndex = j; break; } } if (groupIndex == -1) { sampleGroups.Add(new List <Sample>() { samplesList[i] }); groupIndex = sampleGroups.Count - 1; sampleIndexesAdded.Add(i); if (maxGroupCount == 0) { maxGroupCount = 1; } } //Find more samples to add this group for (int j = i + 1; j < samplesList.Count; j++) { if (samplesList[i].DistanceTo(samplesList[j]) < 100) { if (!sampleGroups[groupIndex].Contains(samplesList[j]) & !sampleIndexesAdded.Contains(j)) { sampleGroups[groupIndex].Add(samplesList[j]); sampleIndexesAdded.Add(j); if (maxGroupCount < sampleGroups[groupIndex].Count) { maxGroupCount = sampleGroups[groupIndex].Count; } } } } } foreach (var sampleGroup in sampleGroups) { if (sampleGroup.Count > 2) { List <Sample> chSampleGroup = HeatMap.ConvexHull(sampleGroup); var polygon = new MapPolygon(); //Set appearance polygon.FillColor = HeatMap.GetColorByDensity(sampleGroup.Count, maxGroupCount); polygon.StrokeThickness = 0; var geoPosList = new List <BasicGeoposition>(); for (int i = 0; i < chSampleGroup.Count; i++) { geoPosList.Add(new BasicGeoposition() { Latitude = chSampleGroup[i].Latitude, Longitude = chSampleGroup[i].Longitude }); } //Create path polygon.Path = new Geopath(geoPosList); //Add to map mcMap.MapElements.Add(polygon); } } EndLoadingIcon(); }