MapExample.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using QuestPDF.Fluent;
  2. using QuestPDF.Helpers;
  3. using QuestPDF.Infrastructure;
  4. namespace QuestPDF.DocumentationExamples;
  5. static class MapboxStaticMapRenderer
  6. {
  7. private const string MapboxBaseUrl = "https://api.mapbox.com/styles/v1/mapbox/streets-v12/static";
  8. private const string AccessToken = "pk.eyJ1IjoibWFyY2luLXppYWJlayIsImEiOiJjbTc5cHZkZTUwNmM4MmxxdGN2cnRxMTBpIn0.8G-_nwFqjjfNQUCmHSOqKw";
  9. public static async Task<byte[]?> FetchStaticMapAsync(double longitude, double latitude, float zoom, int width, int height)
  10. {
  11. var longitudeString = longitude.ToString(System.Globalization.CultureInfo.InvariantCulture);
  12. var latitudeString = latitude.ToString(System.Globalization.CultureInfo.InvariantCulture);
  13. var url = $"{MapboxBaseUrl}/{longitudeString},{latitudeString},{zoom},0,0/{width}x{height}@2x?access_token={AccessToken}";
  14. using var client = new HttpClient();
  15. try
  16. {
  17. var response = await client.GetAsync(url);
  18. return await response.Content.ReadAsByteArrayAsync();
  19. }
  20. catch (Exception ex)
  21. {
  22. return null;
  23. }
  24. }
  25. }
  26. public class MapExample
  27. {
  28. [Test]
  29. public async Task SimpleExample()
  30. {
  31. var map = await MapboxStaticMapRenderer.FetchStaticMapAsync(19.9376052f, 50.0616087f, 10, 500, 400);
  32. Document
  33. .Create(document =>
  34. {
  35. document.Page(page =>
  36. {
  37. page.ContinuousSize(550);
  38. page.Margin(25);
  39. page.Content()
  40. .Column(column =>
  41. {
  42. column.Item().Text("Map of Kraków").FontSize(20).Bold();
  43. column.Item().Text("Capital of Lesser Poland Voivodeship").FontSize(16).Light();
  44. column.Item().Height(15);
  45. column.Item()
  46. .Background(Colors.Grey.Lighten3)
  47. .ShowIf(map != null)
  48. .Image(map);
  49. });
  50. });
  51. })
  52. .GenerateImages(x => "map.webp", new ImageGenerationSettings() { ImageFormat = ImageFormat.Webp, ImageCompressionQuality = ImageCompressionQuality.VeryHigh, RasterDpi = 144 });
  53. }
  54. }