I need just rough data about a location on Android, so when getting it the main consideration is to keep the battery power. I consider this code:
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10 * 60 * 1000, 1000, this);
Is it right strategy? Or is it better to check, something like every 10 minutes for new location and disable listener, which seems closer to code proposed on Google's site, though less logical to me?
Instead of mentioning provider name use criteria to get get provider. And, use criteria to set your requirement.
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
final String bestProvider = manager.getBestProvider(criteria, true);
And then finally,
locationManager.requestLocationUpdates(bestProvider, 0,1000, this);
Alternatively you can request for single location update or disable listener as soon as you done with task
Related
I am trying to build a an android application that can determine the type of highway the user is on by latitude and longitude coordinates. Is there a way to query overpass turbo to retrieve the type or classification of highway ?(whether it is motorway, trunk, primary, secondary, tertiary or residential) I was not able to find any relevant tutorials or documentation to do so.
Can I implement a query in a Java class method as described below:
LocationManager lm(LocationManager)getSystemService(Context.LOCATION_SERVICE); Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
#Override public void onLocationChanged(Location location)
{
double longitude = location.getLongitude();
double latitude = location.getLatitude();
/*query overpass turbo to classify highway*/
/*store retrieved classification type as a string*/
}
And instead of fetching all ways tagged as highways around the given coordinate, can the current position be identified as to whether the way is which type of highway?
You need to look for ways with a highway tag. You can use the following Overpass API query to locate all ways tagged as highway within 100 meters of 51.033,13.749 (lat, lon):
[out:xml][timeout:25];
way(around:100,51.033,13.749)[highway];
out body;
>;
out skel qt;
You can see the result on overpass turbo or download the results directly via Overpass API.
You can use the last link for building a simple HTTP request from within your application. Note that Overpass API supports different output formats such as XML and JSON, depending on your query.
I am pretty new to Android Development, and I've tried to make a method in my Android Application, where you press the button and get coordinates (Longitude and Latitude). But the program stops working on the emulator when I press the button.
I am probably just doing something wrong here. Looking through the Callstack didn't help me. It was simply too cluttered with...a lot of useless information.
How do I fix this?
public void onLocateByGMapButtonClick() {
LocationManager mloc = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
List<String> providers = mloc.getAllProviders();
Location loc = new Location(providers.get(0));
double loTude = loc.getLongitude();
double laTude = loc.getLatitude();
String newCoords = loTude + "," + laTude;
location.setText(newCoords);
Toast.makeText(this.getBaseContext(),"Location have been updated!",5);
}
The reason you application is crashing is probably because you are receiving a null pointer exception.
You have to understand that a GPS fix is not an immediate process, it might take time and in this time you don't have a location to work on unless you use the getLaskKnownLocation method (which maybe return null as well).
So what you need to to is or use:
Location loc = lm.getLastKnownLocation(provider);
or implement a LocationListener that will fire as soon as a new location update arrives.
Tutorial: http://www.vogella.com/articles/AndroidLocationAPI/article.html
I'm doing my project in android..
I want to get location for certain time interval (for fixed time) when button is clicked
for ex: each 10 min for 4hrs I want to get current location address.
how to get this? is there any timer control like visual basic? or any other method for doing it.
thax in advance
you can use LocationListener which gets called when your location get changed and moreover you can provide time interval too
public void requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener, Looper looper)
refer http://www.firstdroid.com/2010/04/29/android-development-using-gps-to-get-current-location-2/
LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener(context);
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1,mlocL istener);
// syntax: mlocManager.requestLocationUpdates(provider, minTime, minDistance, listener)
you can set time and distance both in this method see ths syntax
you can see this link get current device location after specific interval there is a working example of how to get Location information at certain time period
Working with the Location Manager in Android, nce you first call the requestLocationUpdates, the value you pass it to select the refresh time can be changed?
Let me explain. Here we have what I'm doing (and works perfect):
(...)
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, GPSrefresh, GPSminDistance, locationListener);
(...)
public void onLocationChanged(Location location) {
// Do what you want to do
}
If, after some time, I change the value of GPSrefresh, Is it going to affect to the locationUpdates? Is it always checking the value of GPSrefresh or it just did it the first time it was called?
Thank you very much.
you are passing values to a method requestLocationUpdates of LocationManager calss and then updateing that value..so it will not affect the previous method call..
Like,
String tmp="Mango";
Fruits.add(tmp);
tmp="Apple";
in above only Mango will be added..to add Apple you will have to call Fruits.add(tmp) again,
Same way..IF you cange value of GPSRefresh you will need to call the method requestLocationUpdates again with the new updated parameters.
I'm not sure if there is an Android way to do this or if this is just a general Java question. What is the best design for a feature detection mechanism? I want to be able to ask if the runtime environment supports feature foo (this may be determined at compile time as well, so not just runtime stuff).
UPDATE
I'm thinking of using the following (naive?) approach:
public enum Feautures {
GPS, DockToKeyboard;
public boolean isSupported() {
//switch statement
}
}
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(
RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
if (activities.size()>0); // then you have SPEECH RECOGNIZER.
or
final LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER)); // then you have GPS.
http://developer.android.com/reference/android/content/pm/PackageManager.html#hasSystemFeature(java.lang.String)
http://developer.android.com/reference/android/content/pm/PackageManager.html#FEATURE_AUDIO_LOW_LATENCY