Calculate minimum Traveled distance from L Latlng in T time - android

I want some functionality in my Android/iOS application i.e. U user is travelling to L location, So when the U is 15 minute away from the L location he/she will get some Notification.
What i tried so far:
Calling google API and get travel time from the current location to the L location on a particular time interval, when i found it to very close from the 15 minute, i triggered notification to the user(U) and stop calling the API.
Geo-fence L location in such a way that travel time from enter the fence to the L location, should be very close to 15 minutes. When user(U) enter into the Geo-fence i triggered notification to the user(U) and remove the Geo-fence.
But for 1st methods Calling google API is not the right way to achieve that.
In second method the problem is My locations are not fixed they may vary. So the radius of Geo-fence should be different for every location because travel time is different for the location.
According to to me Geo-fence is a good solution but its dynamic radius are creating the problem.
So is there any other way to achieve that? Or we can fix the dynamic radius issue of Geo-fence in any way?

For geofence "dynamic radius" you can use workaround like that: set geofence radius to maximum possible, than test user location on GEOFENCE_TRANSITION_ENTER handler and do what you need only if actual radius less than you need. Something like that:
...
// Get the transition type.
int geofenceTransition = geofencingEvent.getGeofenceTransition();
// Test that the reported transition was of interest.
if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER) {
// start continuously calculate actual radius here
// actualRadius = calcActualRadius();
// if (actualRadius < RADIUS_FOR_YOUR_CONDITION) {
// // do what you need
// }
}
...

Related

How to track facebook current user's location IOS/Android

Is there any way I can track a facebook's user's location in real time who is logged in my mobile app and has location services enabled and also has granted access to my app so as to make use of its location?
Assuming a user X, and 3 linear points in space: A,B,C.
X is traveling from A to C.
Is there an SDK that would enable me to check X's real time location ( latitude + longitude ) at any given time while X is moving from A to C so as to create a dotted map(by dropping a pin on the map) with the user's location at every 10ms?
Is this feasible given the fact that my device has a 4G internet connection?
I think you can use CLLocationManager to update user location after traveling a number of meters. I presume you are already having a CLLocationManager to update your location? you can save the dots in an array (starting with the location of A, ending with location of C). You can then draw a line using the dots. I believe Google Map API has a method for drawing line. There's an answer for that here:
Here is a link from SO
But for the sake of providing code, I will provide it for you in Swift 3.0 (the code in the link is in ObjC):
override func viewDidLoad() {
super.viewDidLoad()
//This is a dummy location, you'd add locations to it using the
// func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
let location:CLLocation = CLLocation(latitude: 200, longitude: 100)
let locationArray:Array<CLLocation> = [location]
let camera:GMSCameraPosition = GMSCameraPosition.camera(withLatitude: (locationArray.first?.coordinate.latitude)!, longitude: (locationArray.first?.coordinate.longitude)!, zoom: 2)
//You can obtain the Lat and Long for above from the list of arrays of locations you saved
//You can use the .first or .last on the array (I used first)
let mapview:GMSMapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
let path:GMSMutablePath = GMSMutablePath()
for nextLocation in locationArray {
if locationArray.index(of: nextLocation) != 0 {
//You dont want to use the first one as you've already done it
//so you start with 1
path.addLatitude(nextLocation.coordinate.latitude, longitude: nextLocation.coordinate.longitude)
}
}
let polyline:GMSPolyline = GMSPolyline(path: path)
polyline.strokeColor = UIColor.red
polyline.strokeWidth = 2
polyline.map = mapview
self.view = mapview
//I personally prefer view.addSubview(mapview)
}

Google Map V2 Android and iOS place latlng decimal places

There is an Android as well as iOS application that I am working on.
Both the applications use google's PlaceAutocomplete controller to get a location's lat-long. In iOS we get lat-long upto 6 decimal places sometimes 5 also. Where as in android we get more than 6 decimal places. The precision of the same location coordinate for Android and iOS differs.
For example consider location Pune
Android Latlng: 18.520431,73.856744
iOS Latlng: 18.52043,73.856744
So as you can see there is difference between the precision of latitudes of the same location.
Is there a way to avoid this as my application needs comparison of these lat-longs?
You should not rely on the precision of the coordinates to compare them because they can change or, as you experiment, vary between platforms.
Instead, you can set a tolerance to determine if two locations are the same. For example:
float YOUR_TOLERANCE = 1; // 1 meter
if (location.distanceTo(otherLocation) < YOUR_TOLERANCE) {
// Both locations are considered the same
}
In android also there is three type of location :
GPS_PROVIDER
NETWORK_PROVIDER
PASSIVE_PROVIDER
So, as per my coding experience i come to know that if you use :
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, new MyLocationListener());
you will get long precision of decimal like more then 14+ and if you will you use fusion of them with this :
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, my_google_listener);
then you will get 6 to 7 digit of precision. try it !!!

Android Google Maps | Get only directions route angle and distance

I'm need to use GoogleMaps for an android app
I need to build an android app that sends request to GoogleMaps API and recive only the list of directions (Turn right in 400meters at the next..) without showing any map
How to I update the list by user movement ?
(You currently passed first turn, heading to second turn.. 800 meters to next turn.. 400 meters to next turn)
If in answer 2 I get only String with "Left,Right,North.." How do I get the turn angle ?
(45-125 will be right, 90 will be absolute right)
I would like to hear about how to optimize the update ( There are 4 ways to use threads in android so I've heard )
Listen for Location Updates using requestLocationUpdates().
http://developer.android.com/training/location/receive-location-updates.html#StartUpdates

Using Phonegap to store traveled distance

I am writing an application using phonegap to store an update lat/lon every 5 seconds to a mysql database. I would like to be able to allow my users to see the total distance traveled since starting the app.
I've taken a look at the Phonegap geolocation API and cannot see a way to calculate total distance traveled based upon lat / lon updates. Is there a way to accomplish this?
EDIT: # Drew thanks for the link. I have looked it over and the JS version of Haversine looks straight forward. the difficult part will be the way phonegap pulls and stores lat/lon. Currently my function to get and send the location to MySQL is
function geo_success(position) {
$("#status p").text("Tracking active");
$('#status').removeClass("stopped").addClass("active");
$('button').text("Stop tracking");
latlon.lat = position.coords.latitude;
latlon.lon = position.coords.longitude;
latlon.alt = position.coords.altitude;
if(!position.coords.speed) { latlon.speed = 0; }
else{ latlon.speed = position.coords.speed }
if(first) {
intervalId = setInterval(send, 5000);
}
first = false;
}
Is there a way you can think of to store the latest value for lat1 lon1 and use the previous for lat2 lon2 and cycle the newest incoming coordinates through those 2 sets of variables? That way i can take the returned variable d from the haversine and store it in the db (to be able to sum it up later). Many thanks.
You would have to create an algorithm yourself that take those coordinates every 5 seconds, do some algebra on them to determine the distance between the two, and add it to the total distance somewhere, then repeat for the next 5 seconds.
For the actual algorithm of calculating the distance, look at this answer.

Do some actions only when current location is acquired

I am designing the map-based app and I want to do certain set of steps when user's location has been acquired (blue dot has been shown on the map). On some of the devices it may take up to 1 min due to location services ramp-up process.. So the question is how do I know when Android Map managed to acquire my location, so I can start doing the rest of initialisation process?
I was thinking about implementing my own LocationListener and trigger callback when I receive location, but this solution seems worthless since I need to only know the point when current location has been acquired and blue dot has been shown. I don't care about location updates.
If you are talking about the blue dot on a map, I suppose you add the MyLocationOverlay to your map. If it is so, there is an easy way to do what you want - MyLocationOverlay has runOnFirstFix(Rannable) interface, so provided Runnable will be run as soon as the map acquires your current location:
this.currentLocationOverlay.runOnFirstFix(new Runnable() {
#Override
public void run() {
//do your magic here
}
});

Categories

Resources