Is there a way to continue background processes even when device is locked? - javascript

I am currently developing app that is 2 part:
1st part is truck driving app that drivers take with them when making deliveries to read order info's and such (they are using tablets that run android)
2nd part is monitoring app for managers to monitor statuses of deliveries and such (they are using computers)
I want to implement GPS tracking now on both sides. I know I can get Lat and Len of drivers and wrap it into setInterval to get (for example) every 3 secs, like so:
setInterval(function(){
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
}, 3000);
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
My only issue here is will this process persist if drivers lock their phones or even minimize browser.
Over all, is this good solution?

Related

Why is JavaScript geolocation only Approximate?

No matter how I use the JavaScript:
position.coords.latitude;
position.coords.longitude;
The returned coordinates are always at least a few city blocks off.
So - it will show the "start point" or "Your location" as a few streets over?
This approximate result seems new, as the code I used before produced a more accurate result.
I have tested on all browser's from a "https" web page.
<script>
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude + '/' + position.coords.longitude;
}
</script>
Has something changed?
Why is the produced coordinates only approximate?
This is a tradeoff between speed and accuracy. Gelocation.getCurrentPosition() at MDN details the optional parameter enableHighAccuracy
JavaScript geolocation is only an approximation of the device's actual location because it relies on the device's built-in GPS, Wi-Fi, IP address lookup and/or network triangulation to determine your location. Additionally, the Geolocation API provides an estimated location, not an exact location. The accuracy of the location information can vary greatly and can be influenced by various factors, including the type of device, its settings, and the environment (buildings, trees, etc) in which it is used.
I used your function and indeed the latitude and longitude coordinates were not exact, there is a difference of a few meters (more than 30-40).

Redirection to different landing page according to city - Wordpress Site

I am new to Website development so I want to redirect people from specific cities to the specific landing page and all the rest to normal homepage in a WordPress site. I have to do this for a client. is there a sample blog or code that can help me I got the longitude and latitude via script I want to detect the location automatically and then redirect according to the city.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
document.getElementById("demo").innerHTML =
"Geolocation is not supported by this browser.";
}
function showPosition(position) {
document.getElementById("demo").innerHTML =
"Latitude: " + position.coords.latitude + "<br>" +
"Longitude: " + position.coords.longitude;
}
however, I don't know how to proceed from here. (TT)

To detect current geo location in mobile device

I've used below code to detect the current location in angular app. this is working fine with all the desktop browser but it not working in mobile device please find below code for more understanding.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(onPositionUpdate,locNotFound,{frequency:5000,maximumAge: 0, timeout: 100, enableHighAccuracy:true});
} else {
// nothing
}
function locNotFound () {
console.log('location not found');
}
function onPositionUpdate(position) {
var lat = position.coords.latitude;
var lng = position.coords.longitude;
var url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + lat + "," + lng + "&sensor=true";
$http.get(url)
.then(function(result) {
console.log(result);
});
}
As you can see that locNotFound() function is called in mobile device. which it should not do because GPS on in mobile device.
I just can give you a partial answer for Android.
Please open the Chrome Browser at Android, click at the icon top right (the tree dots), click at Settings, click at Site Settings and please check the settings for Location. As far as I know this is disabled per default.
So I think your problem is related to security restrictions
Regards
Michael

I want to get my accurate latitude longitude of my linux device on command line. Like Geolocation in HTML5. I dont have access to browser

I want to send email of current Lat Long from my Linux device. I tried geo location in HTML5 browsers, it works great. But i want it on command line. I tried so many options such as curl, geoip to some websites by IP, but they all show my ISP's position, not mine.
I prefer using it on command line or python etc tools.
I could successfully write a python program which opens a locally saved page of HTML5 geolocation code and shows accurate lat long also. Then automatically python fetches lat long from browser and shows on terminal.
File: test.py
from splinter.browser import Browser
import os.path
import time
browser = Browser()
browser.visit('file://' + os.path.realpath('geo.html'))
time.sleep(5)
elements = browser.find_by_css("#demo")
div = elements[0]
print div.value
browser.quit()
File: geo.html
<html>
<head>
<title>Test</title>
<p id="demo"></p>
<script type="text/javascript">
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
</script>
</head>
<body>
<p>The element below will receive content</p>
<div id="div" />
<script type="text/javascript">getLocation()</script>
</body>
</html>
But a bug is there, that every time python opens the browser, i have to click "Share location: Yes" in browser. Because the page is running on local server, not on any webserver. So this solution was not applicable.
Can anyone suggest me reliable solution to get my current lat long in linux in command line?
On Linux you need to talk to gpsd.
You could talk to libgps: http://manpages.ubuntu.com/manpages/wily/en/man3/libgps.3.html
You could use the DBUS interface.
Or you could use the python interface: how to use/install gps python library

Get latitude and longitude of user in PHP or JavaScript for use in PHP

I have an Android app which gets the user's location (latitude and longitude). The latitude and longitude is used to query a database via a web service.
I am in the process of developing a website which will be another gateway into the backend. In order to make the website useful I need to get the latitude and longitude of the user, as the results are sorted by distance (i.e. distance from user's location to location of returned data which is a bar/restaurant).
So how do I get the latitude and longitude of the user?
It is not possible via PHP directly although if you are creating a website you can use the javascript geolocation API to query the browser about what it knows about the users current location:
<script>
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude;
}
</script>
I have simply copied the code example from w3 schools vertabrim.
Source:
http://www.w3schools.com/html/html5_geolocation.asp

Categories