| } | } | ||||
| private async void OnGeoLocationPermissionChanged() { | private async void OnGeoLocationPermissionChanged() { | ||||
| MouseEvent mouseEvent = new MouseEvent(); | |||||
| if (PermissionsProvider.IsGeoLocationAllowed) { | if (PermissionsProvider.IsGeoLocationAllowed) { | ||||
| MouseEvent mouseEvent = new MouseEvent(); | |||||
| mouseEvent.LatLng = await InitializeDeviceMapPosition(); | mouseEvent.LatLng = await InitializeDeviceMapPosition(); | ||||
| await OnMouseMapClicked(mouseEvent); | await OnMouseMapClicked(mouseEvent); | ||||
| } else { | |||||
| await RemoveDeviceLocationMarker(); | |||||
| await InitializeBicycleMapPosition(); | |||||
| mouseEvent.LatLng = this.geoPositionCenterSwiss; | |||||
| await OnMouseMapClicked(mouseEvent); | |||||
| Toaster.ShowWarning(I18n.GetString("Warning.Permission.GeoLocation.Title"), I18n.GetString("Warning.Permission.GeoLocation.Msg")); | |||||
| } | } | ||||
| StateHasChanged(); | StateHasChanged(); | ||||
| } | } | ||||
| FoundReport report = ReportDataProvider.GetFoundReport(); | FoundReport report = ReportDataProvider.GetFoundReport(); | ||||
| MouseEvent mouseEvent = new MouseEvent(); | MouseEvent mouseEvent = new MouseEvent(); | ||||
| LatLng coordinates; | LatLng coordinates; | ||||
| LatLng devicePosition = await InitializeDeviceMapPosition(); | |||||
| if (report != null && ((report.GeographicInfo.Latitude != 0 && report.GeographicInfo.Longitude != 0) || | if (report != null && ((report.GeographicInfo.Latitude != 0 && report.GeographicInfo.Longitude != 0) || | ||||
| (ReportDataProvider.ReportRepositoryItem != null && ReportDataProvider.ReportRepositoryItem.ID != 0))) { | (ReportDataProvider.ReportRepositoryItem != null && ReportDataProvider.ReportRepositoryItem.ID != 0))) { | ||||
| bicycleGeoPosition.DisplayCity = await GetFormattedAddressZipAndTown(ReportDataProvider); | bicycleGeoPosition.DisplayCity = await GetFormattedAddressZipAndTown(ReportDataProvider); | ||||
| coordinates = new LatLng(report.GeographicInfo.Latitude, report.GeographicInfo.Longitude); | coordinates = new LatLng(report.GeographicInfo.Latitude, report.GeographicInfo.Longitude); | ||||
| mouseEvent.LatLng = coordinates; | mouseEvent.LatLng = coordinates; | ||||
| if (PermissionsProvider.IsGeoLocationAllowed) { | |||||
| await InitializeDeviceMapPosition(); | |||||
| } | |||||
| await InitializeBicycleMapPosition(); | await InitializeBicycleMapPosition(); | ||||
| await AddBicycleMarkerOnClickPosition(mouseEvent); | await AddBicycleMarkerOnClickPosition(mouseEvent); | ||||
| StateHasChanged(); | StateHasChanged(); | ||||
| } else { // Set bicycle postion equal to device position | } else { // Set bicycle postion equal to device position | ||||
| coordinates = new LatLng(devicePosition.Lat, devicePosition.Lng); | |||||
| mouseEvent.LatLng = coordinates; | |||||
| await OnMouseMapClicked(mouseEvent); | |||||
| await PermissionsProvider.GeoLocationAllowed(); | |||||
| } | } | ||||
| StateHasChanged(); | StateHasChanged(); | ||||
| } | } |
| namespace cwebplusApp.Pages { | namespace cwebplusApp.Pages { | ||||
| public class CaritasServiceFundVeloKeyDataPageBase : ComponentBase { | public class CaritasServiceFundVeloKeyDataPageBase : ComponentBase { | ||||
| protected readonly LatLng center; | |||||
| protected readonly LatLng geoPositionCenterSwiss; | |||||
| protected Map mapRef; | protected Map mapRef; | ||||
| protected MapOptions mapOptions; | protected MapOptions mapOptions; | ||||
| protected BicycleGeoPosition bicycleGeoPosition; | protected BicycleGeoPosition bicycleGeoPosition; | ||||
| private NominatimReverseAddress addressDto; | private NominatimReverseAddress addressDto; | ||||
| private Marker devicePositionMarker; | private Marker devicePositionMarker; | ||||
| [Inject] | [Inject] | ||||
| private IMarkerFactory MarkerFactory { get; init; } | private IMarkerFactory MarkerFactory { get; init; } | ||||
| public CaritasServiceFundVeloKeyDataPageBase() : base() { | public CaritasServiceFundVeloKeyDataPageBase() : base() { | ||||
| this.center = new LatLng(46.80121, 8.22669); // Centered on Swiss | |||||
| this.geoPositionCenterSwiss = new LatLng(46.80121, 8.22669); // Centered on Swiss | |||||
| this.mapOptions = new MapOptions() { | this.mapOptions = new MapOptions() { | ||||
| DivId = "bicycleLocationMap", | DivId = "bicycleLocationMap", | ||||
| Center = center, | |||||
| Center = this.geoPositionCenterSwiss, | |||||
| Zoom = 6, | Zoom = 6, | ||||
| UrlTileLayer = "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", | UrlTileLayer = "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", | ||||
| SubOptions = new MapSubOptions() { | SubOptions = new MapSubOptions() { | ||||
| } | } | ||||
| protected async Task<LatLng> GetDeviceGeoLocation() { | protected async Task<LatLng> GetDeviceGeoLocation() { | ||||
| LatLng geoPosition = new(46.80121, 8.22669); // Centered on Swiss | |||||
| LatLng geoPosition = geoPositionCenterSwiss; // Centered on Swiss | |||||
| if (PermissionsProvider.IsGeoLocationAllowed) { | if (PermissionsProvider.IsGeoLocationAllowed) { | ||||
| BlazorGeolocationPosition position = await this.BlazorGeolocationService.GetPositionAsync(); | BlazorGeolocationPosition position = await this.BlazorGeolocationService.GetPositionAsync(); | ||||
| if (position.ErrorCode != null) { | if (position.ErrorCode != null) { | ||||
| }) | }) | ||||
| }; | }; | ||||
| } | } | ||||
| private async Task<LatLng> ShowDeviceGeoLocation() { | |||||
| LatLng geoPosition = await GetDeviceGeoLocation(); | |||||
| protected async Task RemoveDeviceLocationMarker() { | |||||
| if (this.devicePositionMarker != null) { | if (this.devicePositionMarker != null) { | ||||
| await devicePositionMarker.Remove(); | await devicePositionMarker.Remove(); | ||||
| } | } | ||||
| } | |||||
| private async Task<LatLng> ShowDeviceGeoLocation() { | |||||
| LatLng geoPosition = await GetDeviceGeoLocation(); | |||||
| await RemoveDeviceLocationMarker(); | |||||
| if (PermissionsProvider.IsGeoLocationAllowed) { | if (PermissionsProvider.IsGeoLocationAllowed) { | ||||
| this.devicePositionMarker = await this.MarkerFactory.CreateAndAddToMap(geoPosition, this.mapRef); | this.devicePositionMarker = await this.MarkerFactory.CreateAndAddToMap(geoPosition, this.mapRef); | ||||
| return geoPosition; | return geoPosition; | ||||
| } | } | ||||
| protected async Task ShowBicycleGeoLocation(bool forceRecalc) { | protected async Task ShowBicycleGeoLocation(bool forceRecalc) { | ||||
| if (this.bicycleGeoPosition.Latitude != 0 && this.bicycleGeoPosition.Longitude != 0 && !forceRecalc) { | if (this.bicycleGeoPosition.Latitude != 0 && this.bicycleGeoPosition.Longitude != 0 && !forceRecalc) { | ||||
| LatLng geoPosition = new(this.bicycleGeoPosition.Latitude, this.bicycleGeoPosition.Longitude); | LatLng geoPosition = new(this.bicycleGeoPosition.Latitude, this.bicycleGeoPosition.Longitude); | ||||
| mouseEvent.LatLng = geoPosition; | mouseEvent.LatLng = geoPosition; | ||||
| await AddBicycleMarkerOnClickPosition(mouseEvent); | await AddBicycleMarkerOnClickPosition(mouseEvent); | ||||
| } | } | ||||
| } else { | |||||
| MouseEvent mouseEvent = new MouseEvent(); | |||||
| mouseEvent.LatLng = geoPositionCenterSwiss; | |||||
| await AddBicycleMarkerOnClickPosition(mouseEvent); | |||||
| } | } | ||||
| } | } | ||||
| public PermissionsProvider(IJSRuntime _jSRuntime) { | public PermissionsProvider(IJSRuntime _jSRuntime) { | ||||
| this.JSRuntime = _jSRuntime; | this.JSRuntime = _jSRuntime; | ||||
| initialize(); | |||||
| } | } | ||||
| [JSInvokable] | [JSInvokable] | ||||
| _geoLocationPermissionChangeCallBack = null; | _geoLocationPermissionChangeCallBack = null; | ||||
| } | } | ||||
| private async void initialize() { | |||||
| await GeoLocationAllowed(); | |||||
| } | |||||
| private async Task<bool> GeoLocationAllowed() { | |||||
| public async Task<bool> GeoLocationAllowed() { | |||||
| var dotNetObjRef = DotNetObjectReference.Create(this); | var dotNetObjRef = DotNetObjectReference.Create(this); | ||||
| string result = await JSRuntime.InvokeAsync<string>("IsGeoLocationAllowed", dotNetObjRef); | string result = await JSRuntime.InvokeAsync<string>("IsGeoLocationAllowed", dotNetObjRef); | ||||
| this.IsGeoLocationAllowed = result.Equals("granted") || result.Equals("prompt"); | this.IsGeoLocationAllowed = result.Equals("granted") || result.Equals("prompt"); |
| } | } | ||||
| function IsGeoLocationAllowed(dotNetObjRef) { | function IsGeoLocationAllowed(dotNetObjRef) { | ||||
| if (isSafari()) { | |||||
| navigator.geolocation.getCurrentPosition( | |||||
| position => { | |||||
| // this function is called only if the user grant permission so here you can handle the granted state | |||||
| dotNetObjRef.invokeMethodAsync("GeoLocationPermissionChanged", "granted"); | |||||
| return "granted"; | |||||
| }, | |||||
| error => { | |||||
| // this function is called if the user denied permission or some other errors occurs | |||||
| dotNetObjRef.invokeMethodAsync("GeoLocationPermissionChanged", "denied"); | |||||
| return "denied"; | |||||
| } | |||||
| ); | |||||
| } else { | |||||
| navigator.permissions.query({ name: 'geolocation' }).then(function (result) { | |||||
| result.onchange = function () { | |||||
| dotNetObjRef.invokeMethodAsync("GeoLocationPermissionChanged", result.state); | |||||
| } | |||||
| dotNetObjRef.invokeMethodAsync("GeoLocationPermissionChanged", result.state); | |||||
| return result.state; | |||||
| }); | |||||
| } | |||||
| navigator.geolocation.getCurrentPosition( | |||||
| position => { | |||||
| // this function is called only if the user grant permission so here you can handle the granted state | |||||
| dotNetObjRef.invokeMethodAsync("GeoLocationPermissionChanged", "granted"); | |||||
| return "granted"; | |||||
| }, | |||||
| error => { | |||||
| // this function is called if the user denied permission or some other errors occurs | |||||
| dotNetObjRef.invokeMethodAsync("GeoLocationPermissionChanged", "denied"); | |||||
| return "denied"; | |||||
| } | |||||
| ); | |||||
| return ""; | return ""; | ||||
| } | } | ||||
| }); | }); | ||||
| } | } | ||||
| function isSafari() { | |||||
| var is_safari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent); | |||||
| return is_safari; | |||||
| } | |||||
| function isIOS() { | function isIOS() { | ||||
| // Detects if device is on iOS | // Detects if device is on iOS | ||||
| const userAgent = window.navigator.userAgent.toLowerCase(); | const userAgent = window.navigator.userAgent.toLowerCase(); |