I have an app where i wanted to locate the exact location of the device. Like if you take taxi, i wanted to locate where exactly the taxi. Taxi driver will be using android device. So from that device i wanted to locate the cab location for other users who wants to see where the cab is.
I have tried uploading current location of cab driver to server as cab moves for few meters by using requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener)
and I have done this updating location of cab when the cab press some start button or something it keeps sending the current location of the device for X meters.
From that updated location other users will call the service and find the current location of cab.
At user side there will be a map where he can see the current location of cab. User calls the service to see updated location. I am calling the service by setting the TIMER on that activity so it refreshes and calls the server again and again.(Setting a timer is it best approach or any other ways of doing it).
But this approach takes lot of data and battery and it is not even realtime. Is there any best approach to locate the android device so that it seems to be realtime atleast.
Is there any best approach to locate the android device
You can use GCM service.
how it will help you?
It can send push messages with current latlng (for each location update) when the cab moves.
EX:
@Override
public void onLocationChanged(Location location) {
String latitude = String.valueOf(location.getLatitude());
String longitude= String.valueOf(location.getLongitude());
//Send push messages with current location
// sendpushMessage(from_Number, to_Number/s, "Location "+ latitude +" "+ longitude;
}
OR
You can start repetitively an IntentService followed by AlarmManager and send messages(location) in each 5/10 seconds and stop whenever you like (e.g when cab is in range of user's location or something like this).
In user's device you can get messages with cab driver's changing location. Just put these updated locations in map and show to the user. So the user can see the running cab perfectly.
In User's Device.. EX:
Get all messages in your GCMIntentService's onMessage() method
like:
@Override
protected void onMessage(Context context, Intent intent) {
String message = intent.getExtras().getString("message");
//tokenize the string and get driver's location(latitude, longitude)
//then update each latlng and show cab marker in user's map with a static method
}
NOTE: we always start notification from here(onMessage()). So make sure you have not use any notifications , otherwise it disturbs the user through multiple notifications with new location message.
This approach is very helpful for battery. Also you don;t have to check the server each time for continue update. it will automatically push messages to user's device whenever you want and the receiver can able to receive each message perfectly.
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