Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

HTML
<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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900