Reputation: 5674
I'm trying to get GPS Longitude & Latitude. Everything is working in case that the GPS is turning on while using any other program that turning it on.
But when i trying to use only my app , the GPS is not turning on.
here is my code:
m_locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
String provider = m_locationManager.getBestProvider(m_c, false);
Location location = m_locationManager.getLastKnownLocation(provider);
if (location != null)
{
double lng = location.getLongitude();
double lat = location.getLatitude();
gpsLocationLon.setText("" + lng);
gpsLocationLat.setText("" + lat);
}
else
{
gpsLocationLon.setText("No Provider");
gpsLocationLat.setText("No Provider");
}
public void onLocationChanged(Location location) {
double lng = location.getLongitude();
double lat = location.getLatitude();
if(null != gpsLocationLat && null != gpsLocationLon)
{
gpsLocationLon.setText("" + lng);
gpsLocationLon.setText("" + lat);
}
}
what did i miss?
Upvotes: 0
Views: 128
Reputation: 8023
You need to start listening to location updates once you've found the best provider.
if (locationManager.isProviderEnabled(provider)) {
locationManager.requestLocationUpdates(provider, 0, 0, this);
// ^^ This will start listening for location updates
// depending on your provider.
} else {
Log.d(LOGTAG, provider + " not enabled");
}
Remember that there are two types of providers, gps and network. So it depends on the criteria(m_c
) which one is selected.
If you want to make sure that you want to listen to GPS as well as Network Updates, remove the Criteria variable and try this :
if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
} else {
Log.d(LOGTAG, "network provider not enabled");
}
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
} else {
Log.d(LOGTAG, gps provider not enabled");
}
Edit 1:
m_locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (m_locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
m_locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
Location location = m_locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
else {
Log.d(LOGTAG, "network provider not enabled");
}
if (m_locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
m_locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
else {
Log.d(LOGTAG, "gps provider not enabled");
}
if (location != null)
{
double lng = location.getLongitude();
double lat = location.getLatitude();
gpsLocationLon.setText("" + lng);
gpsLocationLat.setText("" + lat);
}
else
{
gpsLocationLon.setText("No Provider");
gpsLocationLat.setText("No Provider");
}
public void onLocationChanged(Location location) {
double lng = location.getLongitude();
double lat = location.getLatitude();
if(null != gpsLocationLat && null != gpsLocationLon)
{
gpsLocationLon.setText("" + lng);
gpsLocationLon.setText("" + lat);
}
}
Upvotes: 1