Selaa lähdekoodia

Fixed Geolocation Issue on iPhones (Safari)

master
Flo Smilari 3 vuotta sitten
vanhempi
commit
db3b41ef5e

+ 11
- 5
Pages/Fundvelo/CaritasServiceFundVeloFoundKeyDataPage.razor Näytä tiedosto

@@ -284,10 +284,16 @@
}
private async void OnGeoLocationPermissionChanged() {
MouseEvent mouseEvent = new MouseEvent();
if (PermissionsProvider.IsGeoLocationAllowed) {
MouseEvent mouseEvent = new MouseEvent();
mouseEvent.LatLng = await InitializeDeviceMapPosition();
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();
}
@@ -297,19 +303,19 @@
FoundReport report = ReportDataProvider.GetFoundReport();
MouseEvent mouseEvent = new MouseEvent();
LatLng coordinates;
LatLng devicePosition = await InitializeDeviceMapPosition();
if (report != null && ((report.GeographicInfo.Latitude != 0 && report.GeographicInfo.Longitude != 0) ||
(ReportDataProvider.ReportRepositoryItem != null && ReportDataProvider.ReportRepositoryItem.ID != 0))) {
bicycleGeoPosition.DisplayCity = await GetFormattedAddressZipAndTown(ReportDataProvider);
coordinates = new LatLng(report.GeographicInfo.Latitude, report.GeographicInfo.Longitude);
mouseEvent.LatLng = coordinates;
if (PermissionsProvider.IsGeoLocationAllowed) {
await InitializeDeviceMapPosition();
}
await InitializeBicycleMapPosition();
await AddBicycleMarkerOnClickPosition(mouseEvent);
StateHasChanged();
} 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();
}

+ 16
- 8
Pages/Fundvelo/CaritasServiceFundVeloFoundKeyDataPage.razor.cs Näytä tiedosto

@@ -11,7 +11,7 @@ using System.Threading.Tasks;
namespace cwebplusApp.Pages {
public class CaritasServiceFundVeloKeyDataPageBase : ComponentBase {
protected readonly LatLng center;
protected readonly LatLng geoPositionCenterSwiss;
protected Map mapRef;
protected MapOptions mapOptions;
protected BicycleGeoPosition bicycleGeoPosition;
@@ -21,6 +21,7 @@ namespace cwebplusApp.Pages {
private NominatimReverseAddress addressDto;
private Marker devicePositionMarker;
[Inject]
private IMarkerFactory MarkerFactory { get; init; }
@@ -47,11 +48,11 @@ namespace cwebplusApp.Pages {
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() {
DivId = "bicycleLocationMap",
Center = center,
Center = this.geoPositionCenterSwiss,
Zoom = 6,
UrlTileLayer = "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
SubOptions = new MapSubOptions() {
@@ -107,7 +108,7 @@ namespace cwebplusApp.Pages {
}
protected async Task<LatLng> GetDeviceGeoLocation() {
LatLng geoPosition = new(46.80121, 8.22669); // Centered on Swiss
LatLng geoPosition = geoPositionCenterSwiss; // Centered on Swiss
if (PermissionsProvider.IsGeoLocationAllowed) {
BlazorGeolocationPosition position = await this.BlazorGeolocationService.GetPositionAsync();
if (position.ErrorCode != null) {
@@ -133,13 +134,15 @@ namespace cwebplusApp.Pages {
})
};
}
private async Task<LatLng> ShowDeviceGeoLocation() {
LatLng geoPosition = await GetDeviceGeoLocation();
protected async Task RemoveDeviceLocationMarker() {
if (this.devicePositionMarker != null) {
await devicePositionMarker.Remove();
}
}
private async Task<LatLng> ShowDeviceGeoLocation() {
LatLng geoPosition = await GetDeviceGeoLocation();
await RemoveDeviceLocationMarker();
if (PermissionsProvider.IsGeoLocationAllowed) {
this.devicePositionMarker = await this.MarkerFactory.CreateAndAddToMap(geoPosition, this.mapRef);
@@ -151,6 +154,7 @@ namespace cwebplusApp.Pages {
return geoPosition;
}
protected async Task ShowBicycleGeoLocation(bool forceRecalc) {
if (this.bicycleGeoPosition.Latitude != 0 && this.bicycleGeoPosition.Longitude != 0 && !forceRecalc) {
LatLng geoPosition = new(this.bicycleGeoPosition.Latitude, this.bicycleGeoPosition.Longitude);
@@ -169,6 +173,10 @@ namespace cwebplusApp.Pages {
mouseEvent.LatLng = geoPosition;
await AddBicycleMarkerOnClickPosition(mouseEvent);
}
} else {
MouseEvent mouseEvent = new MouseEvent();
mouseEvent.LatLng = geoPositionCenterSwiss;
await AddBicycleMarkerOnClickPosition(mouseEvent);
}
}

+ 1
- 7
Shared/Services/PermissionsProvider.cs Näytä tiedosto

@@ -20,7 +20,6 @@ namespace cwebplusApp.Shared.Services {
public PermissionsProvider(IJSRuntime _jSRuntime) {
this.JSRuntime = _jSRuntime;
initialize();
}
[JSInvokable]
@@ -37,12 +36,7 @@ namespace cwebplusApp.Shared.Services {
_geoLocationPermissionChangeCallBack = null;
}
private async void initialize() {
await GeoLocationAllowed();
}
private async Task<bool> GeoLocationAllowed() {
public async Task<bool> GeoLocationAllowed() {
var dotNetObjRef = DotNetObjectReference.Create(this);
string result = await JSRuntime.InvokeAsync<string>("IsGeoLocationAllowed", dotNetObjRef);
this.IsGeoLocationAllowed = result.Equals("granted") || result.Equals("prompt");

+ 12
- 27
wwwroot/index.html Näytä tiedosto

@@ -126,28 +126,18 @@
}
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 "";
}
@@ -241,11 +231,6 @@
});
}
function isSafari() {
var is_safari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
return is_safari;
}
function isIOS() {
// Detects if device is on iOS
const userAgent = window.navigator.userAgent.toLowerCase();

Loading…
Peruuta
Tallenna