瀏覽代碼

check permission and show centered swiss map if Geolocation permission is denied

master
Flo Smilari 4 年之前
父節點
當前提交
582d2b2845

+ 18
- 8
Pages/Fundvelo/CaritasServiceFundVeloFoundKeyDataPage.razor.cs 查看文件

@@ -39,6 +39,9 @@ namespace cwebplusApp.Pages {
[Inject]
private OnlineStatusProvider OnlineStatusProvider { get; init; }
[Inject]
private PermissionsProvider PermissionsProvider { get; init; }
private NominatimService NominatimService { get; set; }
@@ -106,13 +109,16 @@ namespace cwebplusApp.Pages {
}
protected async Task<LatLng> GetDeviceGeoLocation() {
LatLng geoPosition;
BlazorGeolocationPosition position = await this.BlazorGeolocationService.GetPositionAsync();
if (position.ErrorCode != null) {
Toaster.ShowError(I18n.GetString("Error.GeoLocation.Title", position.ErrorCode), I18n.GetString("Error.GeoLocation.Msg", position.ErrorMessage));
geoPosition = new(0, 0);
LatLng geoPosition = new(46.80121, 8.22669); // Centered on Swiss
if (PermissionsProvider.IsGeoLocationAllowed) {
BlazorGeolocationPosition position = await this.BlazorGeolocationService.GetPositionAsync();
if (position.ErrorCode != null) {
Toaster.ShowError(I18n.GetString("Error.GeoLocation.Title", position.ErrorCode), I18n.GetString("Error.GeoLocation.Msg", position.ErrorMessage));
} else {
geoPosition = new((double)position.Latitude, (double)position.Longitude);
}
} else {
geoPosition = new((double)position.Latitude, (double)position.Longitude);
Toaster.ShowWarning(I18n.GetString("Warning.Permission.GeoLocation.Title"), I18n.GetString("Warning.Permission.GeoLocation.Msg"));
}
return geoPosition;
}
@@ -136,9 +142,13 @@ namespace cwebplusApp.Pages {
if (this.devicePositionMarker != null) {
await devicePositionMarker.Remove();
}
this.devicePositionMarker = await this.MarkerFactory.CreateAndAddToMap(geoPosition, this.mapRef);
await this.mapRef.SetZoom(16);
if (PermissionsProvider.IsGeoLocationAllowed) {
this.devicePositionMarker = await this.MarkerFactory.CreateAndAddToMap(geoPosition, this.mapRef);
await this.mapRef.SetZoom(16);
} else {
await this.mapRef.SetZoom(6);
}
await this.mapRef.SetView(geoPosition);
return geoPosition;
}

+ 1
- 0
Program.cs 查看文件

@@ -25,6 +25,7 @@ namespace CaritasPWA {
builder.Services.AddSingleton<PageHistoryManager>();
builder.Services.AddSingleton<ReportDataProvider>();
builder.Services.AddSingleton<OnlineStatusProvider>();
builder.Services.AddSingleton<PermissionsProvider>();
builder.Services.AddSingleton<ScrollLockRemover>();
builder.Services.AddSingleton<InputCursorHandler>();
builder.Services.AddScoped<ReportRepositoryService>();

+ 6
- 0
Shared/ResourceFiles/Resources.de.resx 查看文件

@@ -564,6 +564,12 @@ Die KulturLegi wirkt dem entgegen indem sie Kultur-, Bildungs- und Sportangebote
<data name="Warning.Nominatim.Title" xml:space="preserve">
<value>Fehler im Nominatim-Dienst!</value>
</data>
<data name="Warning.Permission.GeoLocation.Msg" xml:space="preserve">
<value>Die Berechtigung für die GeoLocation ist verweigert. Ändern Sie die Browsereinstellungen, um sie zuzulassen.</value>
</data>
<data name="Warning.Permission.GeoLocation.Title" xml:space="preserve">
<value>Berechtigung für GeoLocation verweigert.</value>
</data>
<data name="Welcome" xml:space="preserve">
<value>Willkommen bei Caritas!</value>
</data>

+ 6
- 0
Shared/ResourceFiles/Resources.fr.resx 查看文件

@@ -564,6 +564,12 @@ La KulturLegi s'attaque à ce problème en rendant les activités culturelles,
<data name="Warning.Nominatim.Title" xml:space="preserve">
<value>Erreur du service Nominatim!</value>
</data>
<data name="Warning.Permission.GeoLocation.Msg" xml:space="preserve">
<value>L'accès à la géolocalisation est refusé. Modifiez les paramètres du navigateur pour l'autoriser.</value>
</data>
<data name="Warning.Permission.GeoLocation.Title" xml:space="preserve">
<value>Autorisation refusée pour la GeoLocation</value>
</data>
<data name="Welcome" xml:space="preserve">
<value>Bienvenue chez Caritas!</value>
</data>

+ 6
- 0
Shared/ResourceFiles/Resources.it.resx 查看文件

@@ -564,6 +564,12 @@ La KulturLegi si oppone a questo rendendo le attività culturali, educative e sp
<data name="Warning.Nominatim.Title" xml:space="preserve">
<value>Errore del servizio Nominatim!</value>
</data>
<data name="Warning.Permission.GeoLocation.Msg" xml:space="preserve">
<value>Il permesso per la GeoLocation è negato. Cambia le impostazioni del browser per consentirlo.</value>
</data>
<data name="Warning.Permission.GeoLocation.Title" xml:space="preserve">
<value>Permesso di GeoLocation negato</value>
</data>
<data name="Welcome" xml:space="preserve">
<value>Benvenuti alla Caritas!</value>
</data>

+ 6
- 0
Shared/ResourceFiles/Resources.resx 查看文件

@@ -564,6 +564,12 @@ The KulturLegi counteracts this by making cultural, educational and sporting act
<data name="Warning.Nominatim.Title" xml:space="preserve">
<value>Nominatim service error!</value>
</data>
<data name="Warning.Permission.GeoLocation.Msg" xml:space="preserve">
<value>The permission to the GeoLocation is denied. Change the browser settings to allow it.</value>
</data>
<data name="Warning.Permission.GeoLocation.Title" xml:space="preserve">
<value>GeoLocation Permission Denied</value>
</data>
<data name="Welcome" xml:space="preserve">
<value>Welcome to Caritas!</value>
</data>

+ 37
- 0
Shared/Services/PermissionsProvider.cs 查看文件

@@ -0,0 +1,37 @@
using Microsoft.JSInterop;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace cwebplusApp.Shared.Services {
public class PermissionsProvider {
public IJSRuntime JSRuntime { get; set; }
public bool IsGeoLocationAllowed { get; set; }
public PermissionsProvider(IJSRuntime _jSRuntime) {
this.JSRuntime = _jSRuntime;
initialize();
}
[JSInvokable]
public void GeoLocationPermissionChanged(string geoLocationStatus) {
IsGeoLocationAllowed = (geoLocationStatus.Equals("granted") || geoLocationStatus.Equals("prompt")) ? true : false;
}
private async void initialize() {
await GeoLocationAllowed();
}
private async Task<bool> GeoLocationAllowed() {
string result = "";
var dotNetObjRef = DotNetObjectReference.Create(this);
result = await JSRuntime.InvokeAsync<string>("IsGeoLocationAllowed", dotNetObjRef);
this.IsGeoLocationAllowed = result.Equals("granted") || result.Equals("prompt");
return IsGeoLocationAllowed;
}
}
}

+ 14
- 0
wwwroot/index.html 查看文件

@@ -118,6 +118,20 @@
window.addEventListener("offline", onlineStatusHandler);
}
function IsGeoLocationAllowed(dotNetObjRef) {
navigator.permissions.query({ name: 'geolocation' }).then(function (result) {
console.log("GeoLocation permission: " + result.state);
result.onchange = function () {
console.log("GeoLocation permission: " + result.state);
dotNetObjRef.invokeMethodAsync("GeoLocationPermissionChanged", result.state);
}
dotNetObjRef.invokeMethodAsync("GeoLocationPermissionChanged", result.state);
return result.state;
});
return "";
}
function RemoveScrollLock(dotNetObjRef) {
document.querySelector("body.mdc-dialog-scroll-lock")?.classList.remove("mdc-dialog-scroll-lock");
}

Loading…
取消
儲存