android - GPS not accurate - android

i am trying to show on the map where i am.
I am in Israel and it shows me i am in Egypt.
I tried implementing all of the suggestions of different posts and non helped me solve my problem.
I turned on GPS.
I am connected to the Internet.
When i launch MAPS default android App it shows my real location!
MANIFEST PERMISSIONS:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
I also use the uses-library android:name="com.google.android.maps".
This is the relevant code from the MapActivity which extends LocationListener:
lm = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean enabled = lm
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// Check if enabled and if not send user to the GSP settings
// Better solution would be to display a dialog and suggesting to
// go to the settings
if (!enabled) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
if(came_from.matches("FollowLocation"))
{
Location location;
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String providerName = lm.getBestProvider(criteria, true);
// If no suitable provider is found, null is returned.
if (providerName == null)
{
// reflecting changes if distance travel by
// user is greater than 20m from current location
// and every 1 minute
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
location=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
else
{
// reflecting changes if distance travel by
// user is greater than 20m from current location
// and every 1 minute
lm.requestLocationUpdates(providerName, 1*60*1000, 20, this);
location=lm.getLastKnownLocation(providerName);
}
gMapView = (MapView) findViewById(R.id.myGMap);
gMapView.setStreetView(true);
mc = gMapView.getController();
if (location != null)
{
lat = location.getLatitude();
lng = location.getLongitude();
mc.setZoom(14);
}
else //in case we didn't get the location yet
{
lat = 32.08;
lng = 35.84;
mc.setZoom(9);
}
p = new GeoPoint((int) lat * 1000000, (int) lng * 1000000);
mc.animateTo(p);
// Adding zoom controls to Map
gMapView.setBuiltInZoomControls(true);
// Add a location mark
MyLocationOverlay myLocationOverlay = new MyLocationOverlay();
List<Overlay> list = gMapView.getOverlays();
list.add(myLocationOverlay);
}
}
/* This method is called when use position will get changed */
public void onLocationChanged(Location location) {
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
p = new GeoPoint((int) lat * 1000000, (int) lng * 1000000);
mc.animateTo(p);
}
}
public void onProviderDisabled(String provider) {
// required for interface, not used
}
public void onProviderEnabled(String provider) {
// required for interface, not used
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// required for interface, not used
}
protected boolean isRouteDisplayed() {
return false;
}
These coordinates: lat = 32.08 lng = 35.84 are some spot in israel...
So..... What am i doing wrong ? Or what am i doing not right ? :P
Thanks!

I'd say that getLastKnownLocation is giving you that erroneous position for some reason. Your default of 32.08, 35.84 won't ever get reached because location will never be null. I'd suggest using the debugger to see which of your calls to getLastKnownLocation is getting hit, and what the value returned is.

I took my code and compared it to a working code from an example in the internet.
Not sure what solved the problem, but i definetly know that you should requestLocationUpdates after you finish to configure everything else.
Other people who encounter this problem - try taking a working code from the internet and comparing it step by step until it works on your app.

Related

Why my GPS does not give me the exact location?

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.

Unable to get Latitude/Longitude to change on Android app

Seem to be having some issues getting the longitude and latitude to change on my gps app I am making. I am new to Android, and am not 100% sure I did it right. I also want to note that I do NOT want to use listners for only when the location changes. I specifically want it to update every 5 seconds as I have it in the thread. I also don't seem to be retrieving any satellite data from the stuff I used below either. I just keep writing an empty list to my log file. Am I missing a permission or is the code just wrong? All of the needed information is provided below. Thanks.
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize various GPS things
location = new Location("now");
lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
gps = lm.getGpsStatus(null);
// Create Entry List for later use
entries = new ArrayList<Entry>();
rows = new ArrayList<TableRow>();
// Turn off Stop by default
Button button = (Button)findViewById(R.id.stopButton);
button.setEnabled(false);
// GPS enabled?
final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
buildAlertMessageNoGps();
}
criteria = new Criteria();
criteria.setBearingAccuracy(Criteria.ACCURACY_HIGH);
criteria.setAltitudeRequired(true);
criteria.setHorizontalAccuracy(Criteria.ACCURACY_HIGH);
criteria.setAccuracy(Criteria.ACCURACY_FINE);
backgroundThread.start();
}
Thread thread = new Thread()
{
#Override
public void run() {
while (keepGoing) {
// timestamp
long ts = System.currentTimeMillis() / 1000L;
// set location
location = lm.getLastKnownLocation(lm.getBestProvider(criteria, true));
// location stuff
float ac = location.getAccuracy();
double al = location.getAltitude();
double lat = location.getLatitude();
double lon = location.getLongitude();
DataEntry d = new DataEntry(ac, al, lat, lon);
// satellite stuff
List<GPSSatelliteEntry> GPSentries = new ArrayList<GPSSatelliteEntry>();
Iterable<GpsSatellite> sats = gps.getSatellites();
Iterator<GpsSatellite> satI = sats.iterator();
while (satI.hasNext()) {
GpsSatellite item = (GpsSatellite) satI.next();
float az = item.getAzimuth();
float el = item.getElevation();
int p = item.getPrn();
float s = item.getSnr();
GPSSatelliteEntry temp = new GPSSatelliteEntry(az, el, p, s);
GPSentries.add(temp);
}
Entry en = new Entry(ts, d, GPSentries);
entries.add(en);
try {
Thread.sleep(5000); // 5000 ms
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
writeData();
}
};
getLastKnownLocation only works if something on the device is generating the location updates. If nothing on your phone has called requestLocationUpdates, then thye will never update in getLastKnownLocation. The fix is to call requestLocationUpdates and either move your logic into those functions, or even ignore the calls to those functions but have them going off anyway. Remember to unregister when you no longer want updates, like in onStop.

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>

android locationlistener

I am tring to get the location from a service and its returning null, i dont know what could be the problem. Here my sample code below.
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
locListener = new GpsLocationListener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0,
locListener);
loc = locListener.getLocation();
showMessage("Service started");
if (loc != null) {
latitude = String.valueOf(loc.getLatitude());
longitude = String.valueOf(loc.getLongitude());
}
if (latitude != null && longitude != null) {
connectWebservice();
}
Log.i("trackmeservice", "service started");
}
here my loc is null. Any help will be apprecited thanks;
just check, whether your gps and internet working properly or not.
Add permissions to your manifest file
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES"/>
above the Application tag

GPS Coordinates on Test Developer Phone

My code is taking coordinates of a position at one given time, and then displaying Lat/Long coordinates. I think my code is right, but where the problem lies is giving GPS permission to the phone. I am using a Samsung Nexus S, and I have all proper drivers installed, but there is no network on the phone. I was told that GPS should still work and should be able to retrieve coordinates. I have the, "Use GPS Satellites" setting enabled, and have given the permission in the manifest, as seen here in the manifest.xml
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"></uses-permission>
I threw in all possible location permissions just in case I was missing something, hopefully that isn't a problem.
As for my code I have this in the onCreate method (class? I'm not sure of its proper name)
double[] gps = getGPS();
TextView tv = (TextView) this.findViewById(R.id.gpsCoordinates);
tv.setText("Latitude: " + gps[0] + "\nLongitude: " + gps[1]);
And then a private method in the GPS class to actually get the coordinates
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 believe I pulled this code off of stackoverflow and a few other forums and then tweaked it a bit.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.LOCATION"></uses-permission>
Add above permissions.May be this will help you.
You need to put the following permission
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
Ref:
http://developer.android.com/reference/android/Manifest.permission.html#ACCESS_FINE_LOCATION

Categories

Resources