1

I ran into a rather bizarre issue today with the HTML5 Geolocation API. I'm working one a Visualforce page in Salesforce1 that grabs the user's current location and passes it up to an Apex Controller to be inserted into a custom Object. The getCurrentPosition call seems to be skipped entirely. Hopefully someone can shed some light on this problem. The offending script is here:

<script>
    function setNickname(nickname){
        alert('setting variable');
        var lat = 0;
        var long = 0;
        if (navigator.geolocation) {
            alert('we are golden');
            navigator.geolocation.getCurrentPosition(function(position){
                    alert('alert 0');
                    lat = position.coords.latitude;
                    long = position.coords.longitude;
                    alert('alert 1: ' + lat + ', ' + long);
            });
        }else{
            alert('geolocation is unsupported?');
        }
        alert('alert 2: ' + lat + ', ' + long);
        jQuery('[id$=myHiddenField]').val(nickname);
        jQuery('[id$=latField]').val(lat);
        jQuery('[id$=longField]').val(long);
        passStringToController();
    }
</script>

When pulled out into JSFiddle, however, the geolocation code works fine: link

Any help is greatly appreciated.

Edit:

Full Visualforce page code can be found here

  • 1
    What's the VF page code? – Rob Aug 28 '15 at 15:26
  • Im struggling to get it all to appear as a solid code block. The page keeps rendering parts of it. Would it be better to link a pastebin? – paul jerman Aug 28 '15 at 15:33
  • It should render fine if you wrap it in a code block - select the text and click the { } button – Rob Aug 28 '15 at 15:46
  • Unfortunately it still renders it for some reason. Added a link with the full page code. – paul jerman Aug 28 '15 at 15:51
  • Does setNickname() get called? Which alert() boxes appear? – Rob Aug 28 '15 at 15:56
1

Following our discussion, I just set up the following page in a sandbox:

<apex:page showHeader="true" sidebar="true">

       <script>
       setNickname('blah');

        function setNickname(nickname){
            alert('setting variable');
            var lat = 0;
            var long = 0;
            if (navigator.geolocation) {
                alert('we are golden');
                navigator.geolocation.getCurrentPosition(function(position){
                        alert('alert 0');
                        lat = position.coords.latitude;
                        long = position.coords.longitude;
                        alert('alert 1: ' + lat + ', ' + long);
                }, function(error) { alert('ERROR(' + error.code + '): ' + error.message); });
            }else{
                alert('geolocation is unsupported?');
            }
            alert('alert 2: ' + lat + ', ' + long);
        }
    </script>

</apex:page>

And found that alert('alert 2: ' + lat + ', ' + long); would appear quickly because it doesn't need to wait for the browser to provide the geoposition.

It looks like it can block the other alerts, so try removing that line and see if it makes any difference. When I removed it, the geolocation worked consistently in Chrome.

  • Glad to hear it! – Rob Aug 28 '15 at 18:34
  • Can you also share the details of the controller class? Thanks! – D.O Jan 19 '18 at 4:24
  • @D.O this doesn't use a controller - it's pure JavaScript. – Rob Feb 6 '18 at 15:40
1

There are many services (like http://ip-api.com) available from where you can get the geolocation by calling there APIs.

You can try to call this api via javascript at front end and pass the geolocation to controller. The javascript is as below. Also I have tested it in jsfiddle (http://jsfiddle.net/5zokh6zx/) and it is giving proper result. Hope this helps.

function myGeoLocation() {
    if (window.XMLHttpRequest) xmlhttp = new XMLHttpRequest();
    else xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

    xmlhttp.open("GET","http://ip-api.com/json",false);
    xmlhttp.send();

    var obj = JSON.parse(xmlhttp.responseText);

    return obj;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.