Click here to Skip to main content
14,667,264 members
Rate this:
Please Sign up or sign in to vote.
I tried to use "navigator.geolocation.getCurrentPosition()" in my ASP MVC site but it doesn't work on Safari. It's working perfectly on IE, Chrome, Opera or Mozilla. This is what I tried:

<div id="content"></div>

<script type="text/javascript">
    $(document).ready(function () {

        function showPosition(position) {
            alert("showPosition");
            console.log("Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude);

            var latitude = position.coords.latitude;
            var longitude = position.coords.longitude;

            $.ajax({
                type: "POST",
                url: '/Home/Coordinates',
                data:
                {
                    latitude: latitude,
                    longitude: longitude
                },
                success: function (data) {
                    $("#content").html(data);
                },
                error: function () {
                    alert("Error");
                }
            });
        }

        if (navigator.geolocation) {
            alert("support geolocation");
            navigator.geolocation.getCurrentPosition(showPosition,function(error) {
                alert("error");
            },
            { maximumAge: 12000000, timeout: 30000 });
        }
        else {
            alert("not suported");
            console.log("Geolocation is not supported by this browser.");
        }

    });
</script>


On Safari is it dows not call the showposition() function but it always end up in alert("error"). Does anyone knows why or what I'm doing wrong here ? Thanks.
Posted
Comments
calincoosmin 20-Mar-15 8:34am
   
I didn't find any solution but I did an workaround. On the error function from the getCurrentPosition call I did an ajax request to controller and there I check for the current location(in c#). It's not the best solution because the ip does not generate always the current location but it is a fix until the safari will have again geolocaion. Maybe this will help somebody :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100