Gps android same point after moving - android

Gps is working fine but when i am idle at one place for about 1 or 2 hours and if i start moving but the gps is taking the previous location where am before 1 hour its not changing.
Question when i am moving without any stops gps is locating correct but if i stop for some time and move... then the location doesn't change.
Please help me out of this.Thanks in advance.
if(isGPSEnabled)
{
try
{
if (location == null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
ll = new LocationListener() {
public void onLocationChanged(Location loc)
{
try {
location = loc;
latitude = location.getLatitude();
longitude = location.getLongitude();
logFile log=new logFile();
dt = location.getTime();
log.createFile("Location Changed GPS:"+latitude+""+longitude+"time:"+dt);
setdateAndtime(dt);
} catch (Exception e) {
String errMsg= e.getClass().getName() + " " + e.getMessage();
logFile log=new logFile();
log.createFile("Error in GPS TRACKER When Location Changed"+errMsg+"\n");
}
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status,
Bundle extras) {}
});
if(locationManager!=null)
{
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
dt = location.getTime();
setdateAndtime(dt);
logFile log=new logFile();
log.createFile("Location From GPS:"+latitude+" "+longitude+"time:"+dt+":"+ location.getProvider()+
location);
stopUsingGPS();
}
else
{
logFile log=new logFile();
log.createFile("GPS NULL");
}
}

when you stop some where check the GPS icon in notification bar whether its blinking or steady fixed.
if its blinking that means your GPS signal is lost and trying to get
location from satellite.
if its fixed means you are getting updated location.
condition 1 can be occurred when you are under high-tension voltage line or inside building or you are underground or no satellite in reach.
to boost up your gps search always try to use A-GPS.

Related

Get Location without using GPS and without Google Services via official API

I wonder if it's possible to get a location without Google Services and without using GPS (and thus using NETWORK_PROVIDER) on an Android device. My current code looks like this:
public void onButtonTap(View view) {
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// we're done here
printLocation(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) { }
public void onProviderEnabled(String provider) { }
public void onProviderDisabled(String provider) { }
};
String provider = LocationManager.NETWORK_PROVIDER;
LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
Location location = null;
try {
location = locationManager.getLastKnownLocation(provider);
} catch (SecurityException se) {
}
if(location != null) {
// we're done here
printLocation(location);
}
if(location == null) {
try {
locationManager.requestSingleUpdate(provider, locationListener, null);
} catch (SecurityException se) {
}
} // end of onButtonTab (formatting issues)
private void printLocation(Location location) {
String latitude = String.format(Locale.US, "%.4f", location.getLatitude());
String longitude = String.format(Locale.US, "%.4f", location.getLongitude());
String slocation = "lat: " + latitude + ", lon: " + longitude;
Toast.makeText(getApplicationContext(), slocation, Toast.LENGTH_LONG).show();
}
So I basically query for the last location and if there's none I request an update. This works for PASSIVE_PROVIDER (if there's GPS activity) and of course for GPS_PROVIDER but I don't get a location with NETWORK_PROVIDER. The listeners method onLocationChanged never gets called. The app has the right permissions and locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) is true.
Is this functionality just not working because there's no database (owned by Google and provided by Google Services) to compare against? Or does AOSP build it's own database over time (learning from GPS Data)? I doubt the later.
Little extra question: I can't use Fused Location Provider API as I don't have Google Services installed, can I?

How to get GPS location and send in SMS android

I can not get the GPS to work. I am saving it in a SharedPreference called gps and it will print in a Toast as "http://maps.google.com/?q=" + lat + " , " + lon
this is so when it is sent in a SMS they receiver can just push the link and it opens the map. But when the toast comes up it is blank. Below is my code.
private void showLocation() {
LocationManager locationManager = (LocationManager)
getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(bestProvider);
LocationListener loc_listener = new LocationListener() {
public void onLocationChanged(Location l) {}
public void onProviderEnabled(String p) {}
public void onProviderDisabled(String p) {}
public void onStatusChanged(String p, int status, Bundle extras) {}
};
locationManager
.requestLocationUpdates(bestProvider, 0, 0, loc_listener);
location = locationManager.getLastKnownLocation(bestProvider);
double lat, lon;
try {
lat = location.getLatitude();
lon = location.getLongitude();
} catch (NullPointerException e) {
lat = -1.0;
lon = -1.0;
}
prefs.setPreferenceString(getApplicationContext(),"gps" ,"http://maps.google.com/?q=" + lat + " , " + lon);
}
to find your location try to use GPSTracker. link
And use this code.
gps = new GPSTracker(LauncherActivity.this);
latitude = gps.getLatitude();
longitude = gps.getLongitude();
And one more things, check your device, is it support GPS? And is it (GPS) enable? If your device's GPS is not enable, you will get null....
For more help... check my github repo
I am trying to make such app, here are the code for toast that works well:
Handler handler3 = new Handler(Looper.getMainLooper());
handler3.post(new Runnable(){
#Override
public void run(){
Toast.makeText(context,"mensagem",Toast.LENGTH_LONG).show();
}
});

GPS Locations only from network

I am getting locations from network only not gps.i need gps locations not network provider..but as i see in my logfile..i am only getting network location which are not accurate.so i need to have gps locations..please help me out where i am doing wrong.thanks in advance..
if(isNetworkEnabled)
{
try
{
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0,
ll = new LocationListener() {
public void onLocationChanged(Location loc)
{
try {
location = loc;
latitude = location.getLatitude();
longitude = location.getLongitude();
logFile log=new logFile();
dt = location.getTime();
log.createFile("Location Changed NETWORK:"+latitude+""+longitude+"time:"+dt);
setdateAndtime(dt);
} catch (Exception e) {
String errMsg= e.getClass().getName() + " " + e.getMessage();
logFile log=new logFile();
log.createFile("Error in GPS TRACKER When Location Changed"+errMsg+"\n");
}
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status,
Bundle extras) {}
});
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
dt = location.getTime();
setdateAndtime(dt);
logFile log=new logFile();
log.createFile("Location From NETWORK:" + latitude + " " + longitude +
"time:" + dt + ":" + location.getProvider() + location);
}
}
catch(Exception e)
{
System.out.println(e+"Error MSg");
}
}
if(isGPSEnabled)
{
try
{
if (location != null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0,
ll = new LocationListener() {
public void onLocationChanged(Location loc)
{
try {
location = loc;
latitude = location.getLatitude();
longitude = location.getLongitude();
logFile log=new logFile();
dt = location.getTime();
log.createFile("Location Changed GPS:"+latitude+""+longitude+"time:"+dt);
setdateAndtime(dt);
} catch (Exception e) {
String errMsg= e.getClass().getName() + " " + e.getMessage();
logFile log=new logFile();
log.createFile("Error in GPS TRACKER When Location Changed"+errMsg+"\n");
}
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status,
Bundle extras) {}
});
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
dt = location.getTime();
setdateAndtime(dt);
logFile log=new logFile();
log.createFile("Location From GPS:" + latitude + " " + longitude +
"time:" + dt + ":" + location.getProvider() + location);
}
Change order and conditions.
Your code is..
if (enable_network)
{
// get location from network provider
}
if (enable_gps)
{
// get location from gps provider
}
Your code always receive location from network provider(if it was on).
So, you should check gps provider condition first and check network provider next.
Change code like this
if (enable_gps)
{
// get location from gps provider
}
else (enable_network)
{
// get location from network provider
}
else
{
// can not receive location data
}
I things your testing your application in the Indoor,As the GPS does not work in indoor and that why it is working with your network...try to check the application by moving outside...
Avinash I believe you are from India. Why is that important? because in India even on outside I was not able to get the location from GPS via LocationManager I have ran so many tests on GPS from LocationManager. It seems to return location very rarely from GPS provider.
Solution
Use LocationClient (Google Play services) to retrieve the current location. Google play services are very accurate and retrieves location from Network, GPS and Wifi, If available.
And there are more chances that you can Location. Follow this guide

Get current latitude and longitude in the string

I am trying to get latitude and longitude but sometime network is available but I am not getting value of latitude and longitude. I am using MyLocationListener class and put condition all but some time value is not getting.
protected void showCurrentLocation()
{
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null)
{
counter++;
latitude=location.getLatitude();
longitude=location.getLongitude();
altitude=location.getAltitude();
}
}
private class MyLocationListener implements LocationListener
{
#Override
public void onLocationChanged(Location location)
{
counter++;
latitude=location.getLatitude();
longitude=location.getLongitude();
altitude=location.getAltitude();
}
#Override
public void onStatusChanged(String s, int i, Bundle b)
{
}
#Override
public void onProviderDisabled(String s)
{
}
#Override
public void onProviderEnabled(String s)
{
}
}
Here Best way to get Longitude Latitude:
/** PROCESS for Get Longitude and Latitude **/
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
longitude = String.valueOf(location.getLongitude());
latitude = String.valueOf(location.getLatitude());
Log.d("msg", "changed Loc : "+longitude + ":"+latitude);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// check if GPS enabled
if(isGPSEnabled){
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location != null)
{
longitude = String.valueOf(location.getLongitude());
latitude = String.valueOf(location.getLatitude());
Log.d("msg", "Loc : "+longitude + ":"+latitude);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}else
{
/*
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setCostAllowed(true);
String provider = locationManager.getBestProvider(criteria, true);
*/
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location != null)
{
longitude = String.valueOf(location.getLongitude());
latitude = String.valueOf(location.getLatitude());
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}else
{
longitude = "0.00";
latitude = "0.00";
}
}
}
if device is not able to get currentLocation using GPS then it'll be take fron NetworkProvider or else it'll take 0,0 as my requirement but you can modify as per your requirement
May it'll helpful to you.
Happy Coding :D
See, this is a very common problem. In order to have exact Latitude and Longitude ,You must use GPS also it must me activated on the mobile device but GPS too have some limitations. GPS works most efficiently under open sky. you will not have a clear range inside a building. So try your code under open sky and check the response.

device location is always null

I have gone through many posts on SO regarding this issue:
Tried everything in Here,
Here and Here Nothing works. Everytime location is null. On device and on emulator :(. My GPS is on,internet is on and my manifest has following permissions:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
I use following code to get the location i.e the longitude and latitude of the device...however in every case I get location as null
public void find_Location() {
Log.d("Find Location", "in find_location");
String location_context = Context.LOCATION_SERVICE;
locationManager = (LocationManager) getApplicationContext()
.getSystemService(location_context);
List<String> providers = locationManager.getProviders(true);
for (String provider : providers) {
locationManager.requestLocationUpdates(provider, 1000, 0,
new LocationListener() {
public void onLocationChanged(Location location) {}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider,
int status, Bundle extras) {
}
});
Location location = locationManager.getLastKnownLocation(provider);
if (location == null) {
Toast.makeText(LbsGeocodingActivity.this, "Location Not found",
Toast.LENGTH_LONG).show();
} else {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Toast.makeText(LbsGeocodingActivity.this,
"LAT..." + latitude + "\nLONG..." + longitude,
Toast.LENGTH_LONG).show();
// addr=ConvertPointToLocation(latitude,longitude);
// String temp_c=SendToUrl(addr);
}
}
}
It takes some time before the position is determined, and it can change over time. It's probably just not available yet when you check it.
When a position fix becomes available, you will be notified in onLocationChanged.
How could I be so stupid..My bad....In settings Use Wireless networks was not selected!....That cause all the problems !!
Now I am able to get a location.
It took me 16 hrs to get to this...my productivity going down for sure! :(

Categories

Resources