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( [ ' ', ' ' ] );
} 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