I'm working on GPS in android and I want to show the number of Satellite available at that time for current location and I am getting 3 at all the time in my app while in other app they showing 6 to 9 at same location.I don't know what is the problem.
I am able to get Lat & Long current location.
I'm getting no. of satellite from locationManager.getAllProviders().size() in LocationListener
I also tried
int satellite = 0;
GpsStatus status = locationManager.getGpsStatus(null);
Iterable<GpsSatellite> sat = status.getSatellites();
int i=0;
while (sat.iterator().hasNext())
{
GpsSatellite gpsSatellite=(GpsSatellite)sats.iterator().next();
if(gpsSatellite.usedInFix())
{
satellite++;
}
}
but it showing me 0;
please help me.
Thanks in advanced.
You can check out the GpsStatus class methods.
You can use getSatellies() method to get a list of satellites. It is an iterable so you can traverse it and get the number of satellites.
Related
I'm using flex 4.6 mobile application my issue when i get the current location using gps sensors it take alot of time Within the limits of 35 sec to take 2 updates my code is
protected function GetGPSposition():void
{
//Initialize the location sensor.
geo = new Geolocation();
if(! geo.muted){ //gps is enabled
geo = new Geolocation();
geo.setRequestedUpdateInterval(10);
geo.addEventListener(GeolocationEvent.UPDATE, handleLocationRequest);
geo.addEventListener(StatusEvent.STATUS,handleErrorGPS);
}
else if(geo.muted){ //gps is disabled
(new GPS2()).open(this, false) ;
internetGpsEnable = false ;
}
}
protected function handleLocationRequest(event:GeolocationEvent):void
{
counter++ ;
if (counter > 1)
{
long = event.longitude;
lat = event.latitude;
internetGpsEnable =true ;
lblGPS.text = null;
}
}
Note : GetGPSposition() worked in creationComplete
Can any one to advice me if any method to get the longitude and latitude fast as possible .
Thanks in advance for help.
The GPS of an android device runs in an idle state until requested to save battery, once requested it takes time to communicate and calculate, you can't make this process faster.
If you have both fine and coarse position permissions the Geolocation API should report the Radio/GPRS location while the GPS resolves, but this isn't exact.
I believe your method is as fast as it is possible to go, short of calling into Geolocation when the app starts, to ensure its always polling.
I'm implementing an app that gets your location when you open it. It only gets it when the activity is created, so I don't need a LocationListener. I'm testing it in two real devices.
First I try to get the location using GPS_PROVIDER. If the result is NULL, I use NETWORK_PROVIDER. Since I'm testing it inside a building is to be expected that I won't be able to get the location using the GPS provider.
In one of the devices the location using GPS_PROVIDER is NULL, and the NETWORK_PROVIDER returns the correct location. But in the other device the location using GPS_PROVIDER is not NULL, but it's wrong (it's in a different city than the one I'm currently am!). This is the code I'm using:
private void getPosition() {
locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
myloc = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(myloc == null)
myloc = locManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(myloc != null){
Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
try{
List<Address> addresses = geoCoder.getFromLocation(myloc.getLatitude(), myloc.getLongitude(), 1);
if(addresses.size()>0){
String add = "";
for(int i=0; i<addresses.get(0).getMaxAddressLineIndex(); i++)
add += addresses.get(0).getAddressLine(i)+"\n";
placeEdit.setText(add);
}
}catch(IOException e){
e.printStackTrace();
}
}
}
How can I know the location returned by the GPS provider is wrong? Should I look the network provider first and if it's null use the gps? This app is supposed to be used outside, so I'd like to use the gps first since it's more accurate.
Thanks!
As state in Android Documentation
getLastKnownLocation(String provider) Returns a Location
indicating the data from the last known location fix obtained from the
given provider.
So when your device obtained position 2h earlier and in the meantime never get fresh fix, Location object return that old position. I suggest you to use LocationListener but with thread that will stop listening after some period of time - this approach prevent device from long fix-searching in case of bad view to satellites or when you are inside a building. If LocationListener obtain position use it, if not - use NETWORK_PROVIDER.
I am trying to find distance between two locations in android using the LocationListener and related classes and methods.
I was able to get the coordinates of the given address by using geo coding class. And also was able to get distance between two addresses using the distanceto method.
2.Now I want to give address and get coordinates and then get the distance between previous given address location and the current location (which changes) in real time. since there is no current location method in android i got to use Location Listener.
the want the distance between the given address location coordinate and the real time changing location when moving using locationlistener and other location classes and methods. I wrote and deployed on a device with all permissions in manifest, but the gps gets disabled as soon as i start the app. Below is my code
//below code to get the address location coordinates which is succesful
int i=1;
Geocoder geo=new Geocoder(this);
List<Address> addressList = geo.getFromLocationName(adrs,i);
Address address = addressList.get(0);
if(address.hasLatitude() && address.hasLongitude()){
lat = address.getLatitude();
lon = address.getLongitude();
String adlalo= "Latitude of above address: "+lat +" " +
" "+"Longitude of above address: "+lon;
t2.setText(adlalo);
}
// and below is the location listener class
public class LoctListner implements LocationListener
{
public void onLocationChanged(Location loc)
{
t3 = (TextView) findViewById(R.id.textView3);
l1.setLatitude(lat);
l1.setLongitude(lon);
float d=l1.distanceTo(loc);
String s="the distance between starting and ending point is "+d;
t3.setText(s);
}
}
help me where i made the mistake. Unable to see the log since i got to take the device out to test.since this app needs a moving location.Thanks in advance
Unable to see the log since i got to take the device out to test.since this app needs a moving location
Perhaps you forgot to call requestLocationUpdates() ?
Unable to see the log
You have not put any Logs . Use Logs.D("YOURTAG", "Logs statements");
Location l2 = new Location("");
l2.setLatitude(loc.getLatitude());
l2.setLongitude(loc.getLongitude());
^ This is not required
float door = l1.distanceTo(loc);
You have to wait for a few minutes till the GPS Icon stops animating. This means that a fix has been attained. So check if loc is null
The Geocoder is useless.You can not get address by latlng with the Geocoder.
You can use Gson to parse a json doc from Google maps Api.The json contains your address info.
Greetings,
Does anyone know how I can get coarse GPS coordinates when I don't have a fix and get fine GPS coordinates when I have a fix?
I've tried googling for some sample code to no avail.
I did find this: http://www.android10.org/index.php/articleslocationmaps/226-android-location-providers-gps-network-passive
But I don't know how to implement the fallback to coarse/upgrade to fine.
I hope someone can help. Thanks in advance,
You can find an excellent introduction to the subject in the documentation. The basic idea is that you enable listening for updates from different providers. When a new location is received, you compare it to the previous stored location (a sample function is provided in the above link).
A location object has an getAccuracy that you can use to measure its accuracy. You should also set up a timer so that you know how long has passed after a location provider has provided an update. If more than two minutes have passed after GPS provider has given you an update, then start listening for network updates. While listening for network updates, if GPS gives you a new update, then switch to fine location.
You can get the last know location using the code below. It gets the location providers and loops over the array backwards. i.e starts with GPS, if no GPS then gets network location. You can call this method whenever you need to get the location.
private double[] getGPS() {
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
List<String> providers = lm.getProviders(true);
/* Loop over the array backwards, and if you get an accurate location, then break out the loop*/
Location l = null;
for (int i=providers.size()-1; i>=0; i--) {
l = lm.getLastKnownLocation(providers.get(i));
if (l != null) break;
}
double[] gps = new double[2];
if (l != null) {
gps[0] = l.getLatitude();
gps[1] = l.getLongitude();
}
return gps;
}
I am usin location service for getting latitude and longitude of my location.While running my program it will give me wrong latitude and longitude.How can i get my current location details accurately? Is there any settings in mobile /device ?
String locationProvider = LocationManager.GPS_PROVIDER;
if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER )) {
locationProvider = LocationManager.NETWORK_PROVIDER;
}
Log.w(TAG,"CurrentLocProvider :"+locationProvider);
location=locationManager.getLastKnownLocation(locationProvider);
Log.i(TAG,"Location :"+location);
if(location!=null){
Log.i(TAG,"Location Lat :"+location.getLatitude());
latitude = Double.toString(location.getLatitude());
longitude = Double.toString(location.getLongitude());
txtView.append("\nLatitude : "+latitude+"\n Longitude : "+longitude);
}
After some time
public void onLocationChanged(Location location) {
Log.i(TAG,"Lat :"+location.getLatitude();
Log.i(TAG,"Long :"+location.getLongitude();
}
Also giving me wrong location.Some times USA,In another device china.In Different device it is giving different results?
How can i get the currect result?
Thanks,
aare.
Hm, it is very unlikely (but possible) to give you wrong coordinates - if the device GPS is not working properly.
BTW - the method getLastKnownLocation may return completely wrong data, as described in another stackoverflow question:
this location can be "out-of-date", since the method returns the location when the GPS was used the last time.
Give a look at onStatusChanged, also. You may output the status changes of the GPS to get a better vision of what's going on.
You should also check if network location provider is enabled (users can have this disabled in settings):
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
Or even better: use LocationManager.PASSIVE_PROVIDER.