Reputation: 617
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);
}
Upvotes: 3
Views: 580
Reputation: 989
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
Upvotes: 0
Reputation: 533
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
}
Upvotes: 1
Reputation: 1451
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...
Upvotes: 0