gps approximate alters on bunch of locations stored in sqlite database - android

im new in android im stuck at a point, i have stored locations in sqlite database and retrieve using a cursor.i want to use these locations for proximity alert/notification.eg
i have stored a shopping mall when i will be near enough of this mall a notification pop up on screen. unbale to start how to get alters on multiple locations and how to keep alive tracking for updated location and get notify after application is closed.please give me any start.
i have got all stored locations in variable :
private void refreshLocations() {
try{
String pla_key = String.valueOf(listforkey2);
locationcursor=sql.ListWithPlaceonmap(pla_key);
if (locationcursor.moveToFirst())
do {
Lati = (int) (locationcursor.getDouble(locationcursor.getColumnIndex("Place_liti")));
Longi = (int) (locationcursor.getDouble(locationcursor.getColumnIndex("Place_longi")));
} while(locationcursor.moveToNext());
}
catch(Exception ex){
ex.toString();
}
i m also getting locations updated continually :
public void startButton(View view) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
i have an other method in my activity for updated locations:
public void recordLocation(Location loc) {
Toast.makeText(tracking_to_do.this,"Lat: " + String.valueOf(loc.getLatitude()) + " Long: " + String.valueOf(loc.getLongitude()),Toast.LENGTH_SHORT).show();
}
how i can arrange things for setting the alerts .

Related

Real Time OBD data between 2 activities Android

I have been using https://github.com/pires/android-obd-reader to use OBD data capture in realtime on the obd activity.
Basically, the information is updated every few seconds on this activity.
All I want is to use this information (fuel level for example) in real time with another activity and update the text not once but in real time.
public void onLocationChanged(Location location){
latitude = location.getLatitude();
longitude = location.getLongitude();
drawMyTrack(latitude, longitude, prevLatitude, prevLongitude);
prevLatitude = latitude;
prevLongitude = longitude;
sendLocationDataToWebsite(location);
text= currentTrip.getFuelLevel();
TextView txtChanged = (TextView)findViewById(R.id.textView2);
txtChanged.setText(text);
}

Android: How can I get the traveled distance while I'm running?

I'm wrinting a application and I have to show the distance covered while I'm running.
I use the function "public void onLocationChanged" of the LocationListener. When the user tap a botton and start running I want to show the distance he covered updated to the point in which he is located.
I've written this code:
public void onLocationChanged(Location location) {
if(location != null) {
if(location.hasSpeed()){
if(latitude1 == 0 && longitude1 == 0){
latitude1 = (location.getLatitude()*Math.PI)/180;
longitude1 = (location.getLongitude()*Math.PI)/180;
} else{
latitude2 = (location.getLatitude()*Math.PI)/180;
longitude2 = (location.getLongitude()*Math.PI)/180;
distance = (6372.795477598)*Math.acos(Math.sin(latitude1)
*Math.sin(latitude2)+Math.cos(latitude1)
*Math.cos(latitude2)*Math.cos(longitude1-longitude2));
sumDistance += distance;
latitude1 = latitude2;
longitude1 = longitude2;
}
tv.setText("Distance covered=" + sumDistance + " m");
}
}
}
Is it accurated?
Just a Suggestion:
Store the Latitude and Longitude of the start location and the end location when the user clicks the appropriate button.
and then you could use distanceBetween or distanceTo to get the distance between those two geoPoints.
P.S: This may not work if the user will start and end his run at the same point ;)
Addition:
Check this tutorial:
Recently google has improved its location based API's. They have fused the sensors with the location based api's to share examples of how location can be made more accurate (about 4 times more effective) and consume much lesser power (10 times lesser).
Some interesting links for you to go through will be this and video google io.
SO link for how to use the API's here

How do I switch providers after waiting for 'x' amount of time when attempting to retrieve a location fix?

I am building a GPS Android application whereby it retrieves the nearest places based on the user's current location. At first, I detect both GPS and network to see if they are enabled. If both are enabled I would use GPS first because it is the most accurate, and for my application it is safe to assume they are outside, therefore, retrieving GPS should not take too long. Nevertheless, there are always situations when GPS takes a long time. How do I therefore implement a way to switch over to NETWORK_PROVIDER if GPS takes over, for example, 2 minutes?
This is my code right now:
I check if GPS or internet is enabled.
if(!GPSEnabled && !networkEnabled)
{
Toast.makeText(this, "Error: This application requires a GPS or network connection",
Toast.LENGTH_SHORT).show();
}
else
{
if(GPSEnabled)
{
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
else if(networkEnabled)
{
System.out.println("Getting updates from network provider");
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
}
}
This is the onLocationChanged method. I get the lat/lng values and then send them off to my server and then do appropriate stuff with it.
public void onLocationChanged(Location location)
{
//Get coordinates
double lat = (location.getLatitude());
double lng = (location.getLongitude());
Log.d("MainActivity", "got location: " + lat + ": " + lng);
//get nearest locations
new GetLocations().execute(SharedVariables.root + SharedVariables.locationsController + SharedVariables.getNearestMethod + lat + "/" + lng);
// Zoom in, animating the camera after the markers have been placed
map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), 10));
System.out.println("lat = " + lat + ", lng = " + lng);
//Stop listening for updates. We only want to do this once.
locManager.removeUpdates(this);
}
What would I need to add to switch over to Network or GPS if either one takes too long?
I'd recommend to use both providers at the same time and determine more accurate location using, for example, isBetterLocation() function from this article: http://developer.android.com/guide/topics/location/strategies.html. In this case users won't have to wait 2 minutes to use your app, if GPS is slow. At first, you'll use network updates, and then, when GPS fixes are obtained, more accurate locations.

Android Google Maps - getLastKnownLocation returns inaccurate latitude and longitude

I am developing an app where I need to calculate the distance from the current position and some other locations. I am using the GPS to access the users current location and the other locations coordinates are stored in a database. The problem occurs in the following snippet:
#Override
public void onLocationChanged(Location arg0) {
Log.v("LOCATION LAT", String.valueOf(arg0.getLatitude()));
currentLocation = arg0; //currentLocation is a global class variable
}
The problem is when I feed the DDMS with coordinates such as:
Latitude: 62.639579
Longitude: 17.909689 and log these values I get Latitude: 62.0 and Longitude 17.0 .
If I create a location object and set the lat and lng values myself it works. Like this:
#Override
public void onLocationChanged(Location arg0) {
Location current = new Location("Current location");
current.setLatitude(62.639579);
current.setLongitude(17.909689);
Log.v("Current LAT", "" + current.getLatitude());
}
EDIT SOLVED:
Found the problem. I was feeding the the DDMS with faulty formatting. Apparently this should be delimited with a comma sign, not a dot...
Have you used the permissions specified in this post? Else it kicks back to using cell tower triangulation.
Other question
Found the problem. I was feeding the the DDMS with faulty formatting. Apparently the coordinates should be delimited with a comma sign, not a dot...
you can do something like as below. Create a location variable in that you have to assign location change var
#Override
public void onLocationChanged(Location arg0) {
Location current = new Location("Current location");
current=arg0;
Log.v("Current LAT", "" + current.getLatitude());
}

Android number of satellites problem [duplicate]

I'm using a GPS provider and LocationListener.onLocationChanged(Location location) to receive location fixes.
Documentation says, that Location.getExtras() contains next key/value pair:
satellites - the number of satellites used to derive the fix
but on practice I'm getting an empty extra object - there is no any data there.
Does it means that I'm getting the A-GPS fixes and not GPS?
To get the number of satellites used by the GPS engine you need to implement android.location.GpsStatus.Listener and implement its method onGpsStatusChanged().
Example...
public void onGpsStatusChanged(int event) {
int satellites = 0;
int satellitesInFix = 0;
int timetofix = locationManager.getGpsStatus(null).getTimeToFirstFix();
Log.i(TAG, "Time to first fix = " + timetofix);
for (GpsSatellite sat : locationManager.getGpsStatus(null).getSatellites()) {
if(sat.usedInFix()) {
satellitesInFix++;
}
satellites++;
}
Log.i(TAG, satellites + " Used In Last Fix ("+satellitesInFix+")");
}
I use Location.getExtras().getInt("satellites"), and it give the number of satellites in use.
Since Android API 24 GpsStatus is deprecated and one should use GnssStatus. Let us have an activity or a service processing Gps data and a LocationManager already created.
private GnssStatus.Callback gnssCallback;
public void initCallbacks() {
....
gnssCallback = new GnssStatus.Callback() {
#Override
public void onSatelliteStatusChanged(#NonNull GnssStatus status) {
final int satelliteCount = status.getSatelliteCount();
int usedCount = 0;
for (int i = 0; i < satelliteCount; ++i)
if (status.usedInFix(i))
++usedCount;
Log.d("MyServiceTag", "satellites count = " + satelliteCount + ", used = " + usedCount);
}
};
locationManager.registerGnssStatusCallback(gnssCallback, new Handler(Looper.myLooper()));
....
}
public void deinitCallbacks() {
....
locationManager.unregisterGnssStatusCallback(gnssCallback);
....
}
initCallbacks() should be called after locationManager initialization. deinitCallbacks() should be called when information on the number of satellites is no longer needed, e.g. in onDestroy(). GnssStatus.getSatelliteCount() returns total number of known satellites, GnssStatus.usedInFix(int i) tells whether i-th satellite had been used in the most actual location capture.
Nope it means that your phone manufacturer decided not to implement this. (Or you could be using the NETWORK_PROVIDER which does not use satellites)
Use a NmeaListener and parse the sentences to know the number of satellites visible or used.

Categories

Resources