Why my GPS does not give me the exact location? - android

I'm trying to get the exact longitude and latitude of a android device. The problem is that when setting data in google maps, I realize that the position is not correct. I think the device is getting any previously saved position
LocationManager locationManager;
String svcName = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(svcName);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.POWER_HIGH);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setSpeedRequired(false);
criteria.setCostAllowed(true);
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
updateWithNewLocation(location);
locationManager.requestLocationUpdates(provider, 2000, 10, locationListener);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
Log.i("lon", longitude+"" );
Log.i("lat", latitude+"");
}else{
Log.i("Null", "Es nulo");
}
LocationListener
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
public void onProviderDisabled(String provider) {
updateWithNewLocation(null);
}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider,int status,Bundle extras){}
};
xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_GPS"/>
<uses-permission android:name="android.permission.ACCESS_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
How can I fix it?? I need the current position of teh devices

The line:
Location location = locationManager.getLastKnownLocation(provider);
delivers the last know, even if that location is from yesterday.
Remove the line, cnage the code appropriate, and wait till you get an location event in onLocationChanged().
Further you may check: the timestamp of the location: caluclate the difference to current time and you know if it is a recent location.
Further check location getHoricontalAccuracy() (or named similar) if it is above 40m ignore the location.

Related

How to find last location using android

I'm using the following code to get my last know location, but it is not working
LocationManager locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteia();
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
if(location!=null){
onLocationChanged(location);
}
locationManager.requestLocationUpdates(provider, 20000, 0, this);
Hard to say given the lack of error information. Just a guess ... do you have these in AndroidManifest.xml:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

Why I am Getting different Gps coordinates in same location from NETWORK_PROVIDER?

i am getting different Gps coordinates in same location why its happening. But i need to change gps coordinates only moves after 10 meters . How can i do that.
Here is my code.
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000;
private static final float MIN_GEOGRAPHIC_POOLING_DISTANCE = (float)10.0;
double myLat_Values;
double myLog_Vaues;
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MINIMUM_TIME_BETWEEN_UPDATES, MIN_GEOGRAPHIC_POOLING_DISTANCE, new MyLocationListener());
private class MyLocationListener implements LocationListener {
#Override
public void onLocationChanged(Location location) {
longitude_str = String.valueOf(location.getLongitude());
latitude_str = String.valueOf(location.getLatitude());
Double longitude_number = Double.valueOf(longitude_str);
DecimalFormat dec = new DecimalFormat("#.000000");
longitude_message = dec.format(longitude_number);
Double latitude_number = Double.valueOf(latitude_str);
DecimalFormat decim = new DecimalFormat("#.000000");
latitude_message = decim.format(latitude_number);
try
{
myLat_Values = Double.valueOf(latitude_message);
myLog_Vaues = Double.valueOf(longitude_message);
}
catch(NullPointerException ex)
{
myLat_Values = 0;
myLog_Vaues = 0;
}
System.out.println("GPS Values Clockout:"+myLat_Values+"-"+myLog_Vaues);
}
public void onProviderDisabled(String s) {}
#Override
public void onProviderEnabled(String provider) {}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
}
also i used these into manifestfile :
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
I used this link
please anybody give a suggestion how to solve my problem.
Thank you in advanced.
When you use NETWORK_PROVIDER ,it returns GPS co-ordinates based on the Network Service Provider you are using. But when you are using GPS_PROVIDER it returns GPS co-ordinates based on the satelites.
For Accuracy Criteria, add following code,
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_HIGH);
locationManager.getBestProvider(criteria, true);
ACCURACY_HIGH less than 100 meters
ACCURACY_MEDIUM between 100 - 500 meters
ACCURACY_LOW greater than 500 meters
Also, you need to declare following GPS Permission in AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_GPS"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>

How to get current location when proximity alert fires

I am using the following code to determine the current location when a proximity alert fires:
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location == null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location == null) {
// We'll just write an empty location
location = new Location("");
}
}
When I look at the locations that I get back I get entering alerts in locations where I should't get them. I was under the impression since proximity alerts internally poll the GPS and network provider - thus updating the LastKnownLocation - that this code would yield the current location. Is this assumption correct?
I am using the following code to determine the current location and its working too. Check out dis code...
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager) getSystemService(context);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(criteria, true);
if (null != provider)
{
Location location = locationManager.getLastKnownLocation(provider);
if (null != location) {
GpsFound=true;
currentLat = location.getLatitude();
currentLon = location.getLongitude();
System.out.println("Current Lat,Long :"+currentLat+" ,"+currentLon);
}
else
{
//GpsFound=false;
gpsMsg="Current Location can not be resolved!";
}
}
else
{
gpsMsg="Provider is not available!";
//GpsFound=false;
}

java.lang.NullPointerException about gps api?

as the title i have a question about the gps api
follow is my code
LocationManager loctionManager;
String contextService=Context.LOCATION_SERVICE;
loctionManager=(LocationManager) getSystemService(contextService);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = loctionManager.getBestProvider(criteria, true);
Location location = loctionManager.getLastKnownLocation(provider);
double a=location.getLatitude();
double b=location.getLongitude();
Log.d(""+a,""+b);
then the error is java.lang.NullPointerException
anyone can help me?thx
getLastKnownLocation() can return null you need to ensure it does not.
In order to get a Location you need at first to set permission in your Manifest.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Then in your code you need to listen for location avalability. You do that by registering a LocationListener. The activity will display, and when Android will have a location for you it will call you back.
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
//Do what you want with your coordinates
}
loctionManager.requestLocationUpdates(LocationManager.GPS, 100, 1, locationListener);
If you are using the emulator and not a real device you need to force a location, you can use DDMS inside Eclipse to send a fake location:

Getting null from 'getLastKnownLocation' on SDK

I have a problem related to the Location API.
I tried the following code:
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location loc = getLastKnownLocation(LocationManager.GPS_PROVIDER);
loc is always null, when getLastKnownLocation() is called.
What is wrong?
Along with the permissions in your AndroidManifest.xml file, have you registered a location listener?
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location loc = getLastKnownLocation(LocationManager.GPS_PROVIDER);
lm.requestLocationUpdates(LocationManager.GPS, 100, 1, locationListener);
Then have a method, in this case locationListener, to complete your task
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
If you're running the code in the emulator, any calls to get the GPS location will return null until you explicitly update the location (via Eclipse or ADB).
I had the same problem as you, I always receive a null Location object, But finally it was solved in an easy way. You must have a valid GPS location, so, if the GPS is not enabled and you dont't have enough signal, the Location object will be ALWAYS null.
Have you set the permissions in your AndroidManifest.xml? You need these permissions in order to access the user's location with an application:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
Here's something you can use:
LocationManager lm = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = lm.getBestProvider(criteria, false);
Location loc = lm.getLastKnownLocation(bestProvider);
last_lat = loc.getLatitude();
last_lng = loc.getLongitude();
You need the instance Of LocationManager like this:
First Instance:
LocationManager lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
Wrong:
Location loc = getLastKnownLocation(LocationManager.GPS_PROVIDER);
Correct:
Location loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

Categories

Resources