5

I'm attempting to send lat and lon along with a webcam image and some other data using PHP and javascript- don't ask, it's just a small project I started, hoping to learn something. In order for the lat and lon to become available I have to call the webcam function after lat and lon have been retrieved. Here is the javascript I'm working with ( geolocation portion is from Lynda.com. I combined that with JpegCam ). I added lat and lon divs to hold the values. Then I call the webcam function now_go() which gets the lat and long using getElementById(). This works great as long as the user shares their location. If they don't, the now_go() function is not called. But if I call it any earlier, the lat and lon are not available even if the user has decided to share their location. So at what stage in the game can I tell if the user has chosen not to share?

 <script type="text/javascript">
    var t = new bwTable();
    var geo;

    function getGeoLocation() {
        try {
            if( !! navigator.geolocation ) return navigator.geolocation;
            else return undefined;
        } catch(e) {
            return undefined;
        }
    }

    function show_coords(position) {
        var lat = position.coords.latitude;
        var lon = position.coords.longitude;
        element('lat').innerHTML = lat;
        element('lon').innerHTML = lon;
        t.updateRow(0, [ lat.toString(), lon.toString() ] );
        dispResults();
        now_go();
    }

    function dispResults() {
        element('results').innerHTML = t.getTableHTML();

    }

    function init() {
        if((geo = getGeoLocation())) {
            statusMessage('Using HTML5 Geolocation')
            t.setHeader( [ 'Latitude', 'Longitude' ] );
            t.addRow( [ '&nbsp;', '&nbsp;' ] );
        } else {
            statusMessage('HTML5 Geolocation is not supported.')
        }
        geo.getCurrentPosition(show_coords);
    }

    window.onload = function() {
        init();
    }
</script>

This is the part of the webcam script that I turned into a function so I could call it after lat and lon have been fetched:

 <script language="JavaScript">
        function now_go(){
            var lat = null;
            var lon = null;
     if ($("#lat").length > 0){          
      var lat = document.getElementById('lat').innerHTML;
         }

          if ($("#lon").length > 0){          
      var lon = document.getElementById('lon').innerHTML;
          }
            webcam.set_api_url( 'webcam/test/' + lat + '/' + lon);
    webcam.set_quality( 90 ); // JPEG quality (1 - 100)
    webcam.set_shutter_sound( true, '/mark/js/webcam/shutter.mp3' );

       }    

</script>

I'm sure this code has other issues, but this is what it looks like right now. And being five a.m. I think it's going to stay that way for a while.

Any advice or suggestions would be terrific.

Thanks, Mark

Pavlo
  • 43,301
  • 14
  • 77
  • 113
Mark
  • 467
  • 1
  • 6
  • 21
  • I'm not sure why you're using a double not `!! navigator.geolocation` to test this condition. If you're testing its existence a single not `if(!navigator.geolocation) { alert('Your browser does not support Geolocation.') }` – Russell Dias Dec 11 '11 at 13:18
  • Not directly helpful for your question, but shouldn't any use of the geo variable be inside the if((geo = getGeoLocation())) bit? Using it after the else block is going to fail if the assignment fails. – Vala Dec 11 '11 at 13:20
  • See here: http://stackoverflow.com/questions/7463367/how-to-call-function-when-user-allows-or-denies-access-to-physical-location – Rob Flaherty Dec 11 '11 at 13:21
  • @RussellDias Actually the way he uses it that whole if/else block can be replaced with `return navigator.geolocation` since he's returning `undefined` if it's not defined.. – Vala Dec 11 '11 at 13:23

1 Answers1

8

See this link. There is a second argument to the navigator.geolocation.getCurrentPosition() method that lets you pass in another function:

navigator.geolocation.getCurrentPosition(show_coords, function(error) {
    switch(error.code)  
    {  
        case error.PERMISSION_DENIED: alert("user did not share geolocation data");  
        break;  

        case error.POSITION_UNAVAILABLE: alert("could not detect current position");  
        break;  

        case error.TIMEOUT: alert("retrieving position timedout");  
        break;  

        default: alert("unknown error");  
        break;  
    }  
});
danludwig
  • 46,965
  • 25
  • 159
  • 237