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
The most likely problem, for example, is that the other iPhone might not have cellular or WiFi reception. In addition, that other phone could be turned off, or your friend might have turned off location services in Settings.
Yes, there are ways to stop sharing the location without people knowing. By disabling various features like “Share My Location” or “Find My iPhone”, you can easily prevent people from tracking your location. Another advantage of disabling them is that nobody will receive any kind of notification.
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;
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With