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);
}
Related
in my android app i calculate gps position and show the lat log value into TextView, and i use this lat long value for a query on SQLITE DB.
Now i'm trying to show into TextView the name of Location calculated by lat and log. I have try geocodeby i have soma error.
this is my code for calculate the lat and long value:
ImageView bt_get_mygpspos = (ImageView)getView().findViewById(R.id.bt_get_mygpspos);
edittext_mygpspos = (EditText)getView().findViewById(R.id.edittext_mygpspos);
bt_get_mygpspos.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
GPSTracker mGPS = new GPSTracker(getActivity());
if(mGPS.canGetLocation() ){
query.curPos = new LatLng(mGPS.getLatitude(),mGPS.getLongitude());
edittext_mygpspos.setText("lat:"+query.curPos.latitude+" lon:"+query.curPos.longitude);
Log.e("test", "lat:"+query.curPos.latitude+" lon:"+query.curPos.longitude);
}else{
mGPS.showSettingsAlert();
}
}
});
how i can trasform the mGPS.Latitude and Longitude in a name of Location to show into TextView?
Thans
You could use any public web-based API for geocoding like:
Google API;
Yandex API;
Yandex is better in ex-USSR space and have better daily free limits: 25 000 request when compare to 2 500 in Google. In any other detail Google is better.
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
Currently I am working on an application, where I want to calculate distance a vehicle travels. My requirement is while driving car, my Android device should calculate total distance I traveled and send this information to server. In order to do this, I used Android's location manager api, set criteria and used getBestProvider. This way we can either use GPS or Network to get latitude and longitude. The following is the code snippet of this:
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.POWER_HIGH);
provider = locationManager.getBestProvider(criteria, false);
locationManager.requestLocationUpdates(provider,30000,5, this);
onLocationChanged() call back method provide latitude and longitude every time. We always store previous coordinates and when we get new coordinates we find the distance between the two using distanceBetween() api. The following is the code snippet of this:
public void onLocationChanged(Location location) {
findLatLongDistance(location);
}
private void findLatLongDistance(Location location) {
// TODO Auto-generated method stub
try{
Date date = new Date();
TimeStamp2 = sdf.format(date);
getSavedLatLong();//get lat and long from preference
Location locationB = new Location("point B");
locationB.setLatitude(location.getLatitude());
locationB.setLongitude(location.getLongitude());
Location locationA = new Location("point A");
locationA.setLatitude(prelat_val); //lat from pref locationA.setLongitude(prelong_val); //long from pref
if(prelat_val>0.0 && prelong_val>0.0){
Toast.makeText(LocationService.this,"Location Odometer Sum "+odometer_sum, Toast.LENGTH_LONG).show();
float distance2 = getDistance(prelat_val,prelong_val,location.getLatitude(),location.getLongitude());
odometer_sum = odometer_sum + (distance2/1000);
Toast.makeText(LocationService.this,"Lat "+prelat_val+"Long "+prelong_val+"Sum "+odometer_sum, Toast.LENGTH_LONG).show();
}
saveData(lat,lng,odometer_sum);
}catch(Exception e){
e.printStackTrace();
}
}
public float getDistance(double d, double e, double f, double g) {
float [] dist = new float[2];
Location.distanceBetween(d,e,f, g, dist);
return dist[0] ;
}
This is the list of issue that we face here:
The location it provide is not accurate. There is a difference of about 300-400 metres while we testing this app in 5 km distance
When the mobile is in same location for long time, it always provide different latitude and longitude. If you check above code snippet, in requestLocationUpdates(), we are setting 30 seconds time interval and 5m distance. Here what we thought is, if my mobile device move 5m distance AND if it cross 30 seconds interval, it will provide new latitude and longitude. But what really happens is, it provide coordinates every 30 seconds irrespective of device movement. I am not sure how to fix this issue.
While device is moving, how to get accurate value. Do I need to do some more things in the code?
I really spend so many hours trying various options. But I feel I am missing something here. Please help me on this. Thanks in advance..
Thanks,
Your minTime value in requestLocationUpdates() is 30seconds. Thats too long for an app that tries to calculate accurate distances. I have used locationManager.requestLocationUpdates(provider, 0, 5, locationListener);
in my code for long and I get accurate updates. Though this drains the battery very fast. So you would have to try different values to strike a balance between accuracy and battery life and see what suits your app
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());
}
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 .