AspNetMonsters / Blazor.Geolocation

Blazor interop for browers Geolocation apis
MIT License
91 stars 25 forks source link

Nothing is returned when the user does not give the browser permission to see the geo location #24

Open csk1nner opened 4 years ago

csk1nner commented 4 years ago

Hello there,

Thanks for writing this great tool! I'm having a problem when the user decides not to grant permissions. The call to LocationService.GetLocationAsync() hangs. I'm guessing this has to do with the location.js file; most likely the GetLocation function. Anyways. My JavaScript skills aren't that great... But it looks like the DnotNet.invokeMethodAsync function is not getting called.

Let me know your thoughts!

ViRuSTriNiTy commented 4 years ago

The javascript code simply has no handling of error situations hence a Task created for calling JS code hangs indefinitely because no value is returned when an error occurs.

You can fix this yourself by changing the Location.js file.

For example, search for the following section

window['AspNetMonsters']['Blazor']['Geolocation']['GetLocation'] = function (requestId) {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function (position) {
            dispatchResponse(requestId, position.coords);
        });
    }
    else {
        return "No location finding";
    }
}; 

Here getCurrentPosition is called which itself calls the error callback function given via the second parameter but this parameter is missing e.g. when the Geolocation API is blocked via browser settings. To fix this add the second parameter as you seem fit like

window['AspNetMonsters']['Blazor']['Geolocation']['GetLocation'] = function (requestId) {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function (position) {
            dispatchResponse(requestId, position.coords);
        },

        //
        // pass error callback function to return a value to Blazor when something goes wrong
        //
        function (positionError) {
                dispatchResponse(requestId, {
                    latitude: 0,
                    longitude: 0,
                    altitude: 0,
                    accuracy: 0,
                    altitudeAccuracy: 0,
                    heading: 0,
                    speed: 0});
        });
    }
    else {
        return "No location finding";
    }
};
sengiv commented 2 years ago

Is there a reason why this fix has not been implemented in release yet? It looks to me like @ViRuSTriNiTy example should be default behavior.