async Task <float> CalculatePollution(List <Position> pollutionPoints, bool main = false) { float overall = 0, max = 0; if (pollutionPoints.Count > 0) { List <List <string> > list = new List <List <string> >(); for (int i = 0; i < pollutionPoints.Count; i++) { List <string> point = new List <string>(); point.Add(pollutionPoints[i].Latitude.ToString("F6")); point.Add(pollutionPoints[i].Longitude.ToString("F6")); list.Add(point); } PollutionRequest request = new PollutionRequest() { RAD = 50, PAIRS = list }; var result = await rest.GetPollution(JsonConvert.SerializeObject(request)); if (result != null) { int maxIndex = 0; if (result.val.Count >= 5) { for (int index = 2; index < result.val.Count - 2; index++) { float subTotal = 0; for (int j = index - 2; j < index + 2; j++) { subTotal += (float)Convert.ToDouble(result.val[j]); } if (subTotal > max) { max = subTotal; maxIndex = index; peak = max; } overall += (float)Convert.ToDouble(result.val[index]); } overall /= result.val.Count; double lat, lng; lat = Convert.ToDouble(result.lat[maxIndex]); lng = Convert.ToDouble(result.lon[maxIndex]); hotspot = new Position(lat, lng); } } } return(overall); }
async Task <bool> GetHeatMap(Bounds bounds) { // set the size of the pixel in degrees lat / lon map.Polygons.Clear(); map.Polylines.Clear(); // build the request for polution info var request = new PollutionRequest(); request.RAD = 100; request.PAIRS = new List <List <string> >(); var radius = Math.Max(bounds.HeightDegrees / 2, bounds.WidthDegrees / 2); var unit = Math.Max(.0015, radius / 100); var halfU = unit / 2; var left = Convert.ToDouble((bounds.Center.Longitude + radius * 2).ToString("F6")); var right = Convert.ToDouble((bounds.Center.Longitude - radius * 2).ToString("F6")); var top = Convert.ToDouble((bounds.Center.Latitude + radius).ToString("F6")); var bottom = Convert.ToDouble((bounds.Center.Latitude - radius).ToString("F6")); for (var Y = bottom; Y <= top; Y += unit) { for (var X = left; X >= right; X -= unit) { List <string> point = new List <string>(); point.Add(Y.ToString()); point.Add(X.ToString()); (request.PAIRS).Add(point); if (request.PAIRS.Count >= 400 || (Y + unit > top && X - unit < right)) { var result = await rest.GetPollution(JsonConvert.SerializeObject(request)); if (result != null) { // get the results var x = 0; double lvl = 0; // loop through the results for (x = 0; x < result.lon.Count; x++) { string level = (result.val)[x]; lvl = Convert.ToDouble(level) * 1; Color fc = Color.Black; if (lvl <= 50) { fc = Color.Green; } else { if (lvl <= 100) { fc = Color.Yellow; } else { fc = Color.Red; } } var north = Convert.ToDouble(result.lat[x]) * 1 + halfU * 1; var south = Convert.ToDouble(result.lat[x]) * 1 - halfU * 1; var east = Convert.ToDouble(result.lon[x]) * 1 + halfU * 1; var west = Convert.ToDouble(result.lon[x]) * 1 - halfU * 1; var tileBounds = new Bounds(new Position(south, west), new Position(north, east)); var rectangle = new Xamarin.Forms.GoogleMaps.Polygon(); rectangle.StrokeWidth = 0; rectangle.FillColor = Color.FromRgba(255, 0, 0, (lvl / 30.0) - 0.25); rectangle.Positions.Add(tileBounds.NorthEast); rectangle.Positions.Add(tileBounds.NorthWest); rectangle.Positions.Add(tileBounds.SouthWest); rectangle.Positions.Add(tileBounds.SouthEast); map.Polygons.Add(rectangle); } } request.PAIRS.Clear(); } } } return(true); }