I have a web application build for my client and a mobile application for their clients. I have developed an application that can track their location at set intervals using a background service. However, i was asked to add on to it and track location on request. This means that on my web app if i click get location of a user , i will be sent back a lat and lng. Any tips on how to start this function will be great.
Asked
Active
Viewed 2,609 times
1
-
`function getLocation(){}` seems a good start – Weedoze Apr 26 '17 at 07:36
-
As told by @Weedoze , create a javascript interface function . Check this : http://stackoverflow.com/questions/39657311/using-javascript-bridge-in-android – Vikrant Apr 26 '17 at 07:41
-
thanks for the link, will try it out, seems like this just might work. But for my case i will write it inside my service rather than my activity? @Vikrant – Yu Kai Ku Apr 26 '17 at 07:49
-
I feel you can define a generic `function` rather then a `service` , from `webview` trigger this `function` on request (may be a button click ?) fetch the precise location(lat,long) and send the co-ordinates back to respective `webview` . Are you trying to get location from JavaScript or Java ? – Vikrant Apr 26 '17 at 08:02
-
@Vikrant , Im trying to get the location of a specific android device with my java app installed from a web app developed using javascript and php. – Yu Kai Ku Apr 26 '17 at 08:19
3 Answers
1
you can always use native JavaScript method navigator.geolocation
it will work in both, mobile and web. refer to the below example.
var options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
};
function success(pos) {
var crd = pos.coords;
console.log('Your current position is:');
console.log(`Latitude : ${crd.latitude}`);
console.log(`Longitude: ${crd.longitude}`);
console.log(`More or less ${crd.accuracy} meters.`);
};
function error(err) {
console.warn(`ERROR(${err.code}): ${err.message}`);
};
navigator.geolocation.getCurrentPosition(success, error, options);

imixtron
- 183
- 10
-
For this scenario, im guessing the end user will get send the location with the web app? What i am aiming for is, on a laptop i click a button and get the location of my end user mobile device. – Yu Kai Ku Apr 26 '17 at 07:55
-
then you must keep a event on which the location should be updated/received. - you can have a service that would update geolocation if the device moves a certain distance. _(this is what i used in iOS once)_ - you can use socket.io (if ur using nodejs) to send/recieve events, or any other alternative. (_http://nkzawa.tumblr.com/post/46850605422/connecting-to-a-socketio-server-from-android_) - you can also achieve this by firebase cloud messaging – imixtron Apr 26 '17 at 09:03
0
In your App(Background service):
-Write your lat and lng as the "last known position" into a database
In your Webapp:
-Query the database and get the last position

schaffioverflow
- 510
- 3
- 14
-
As of right now my interval i set is at an hour so, getting my last known location through db would not be that good of an option for my client, is there a way to get the location of my client's client at the point of request? – Yu Kai Ku Apr 26 '17 at 07:41
0
You can create GCM service, when someone click a button on web as server, send a message through GCM to the android client. And on receive message event, get your current location and send that to server. Maybe this site can help you with creating GCM service.

Randyka Yudhistira
- 3,612
- 1
- 26
- 41
-
For this case wise, it seems like it could work, however i foresee some issue with the sending to server. What i would like to have is an onclick on webApp and get a bootstrap popup modal of a map with the lat lng inserted into it or an error message if the user has no wifi or GPS disabled, do you think that the GCM would work? As from what i can see sending to server might be an issue as on click i perform the send function to my app, but how would i display the lat lng from there? – Yu Kai Ku Apr 26 '17 at 09:12