current_geo_location_win.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "platform/win/current_geo_location_win.h"
  8. #include "base/platform/win/base_windows_winrt.h"
  9. #include "core/current_geo_location.h"
  10. #include <winrt/Windows.Devices.Geolocation.h>
  11. #include <winrt/Windows.Foundation.h>
  12. #include <winrt/Windows.Services.Maps.h>
  13. #include <winrt/Windows.Foundation.Collections.h>
  14. namespace Platform {
  15. void ResolveCurrentExactLocation(Fn<void(Core::GeoLocation)> callback) {
  16. using namespace winrt::Windows::Foundation;
  17. using namespace winrt::Windows::Devices::Geolocation;
  18. const auto success = base::WinRT::Try([&] {
  19. Geolocator geolocator;
  20. geolocator.DesiredAccuracy(PositionAccuracy::High);
  21. if (geolocator.LocationStatus() == PositionStatus::NotAvailable) {
  22. callback({});
  23. return;
  24. }
  25. geolocator.GetGeopositionAsync().Completed([=](
  26. IAsyncOperation<Geoposition> that,
  27. AsyncStatus status) {
  28. if (status != AsyncStatus::Completed) {
  29. crl::on_main([=] {
  30. callback({});
  31. });
  32. return;
  33. }
  34. const auto point = base::WinRT::Try([&] {
  35. const auto coordinate = that.GetResults().Coordinate();
  36. return coordinate.Point().Position();
  37. });
  38. crl::on_main([=] {
  39. if (!point) {
  40. callback({});
  41. } else {
  42. callback({
  43. .point = { point->Latitude, point->Longitude },
  44. .accuracy = Core::GeoLocationAccuracy::Exact,
  45. });
  46. }
  47. });
  48. });
  49. });
  50. if (!success) {
  51. callback({});
  52. }
  53. }
  54. void ResolveLocationAddress(
  55. const Core::GeoLocation &location,
  56. const QString &language,
  57. Fn<void(Core::GeoAddress)> callback) {
  58. callback({});
  59. }
  60. } // namespace Platform