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?
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);
}
}
Related
I have an app that is already asking for runtime permissions to activate the localization permission and I have all ready to receive longitude and latitude. My problem now is only to get the latitude and longitude from the device itself.
This is my method, assume that the method goes to the else statement because I already have the permissions handling working
if (!checkPermissions()) {
requestPermissions()
} else {
//get latitude and long here
}
Here is a sample code
1.LocationManager & Listener
private LocationManager locationManager;
private LocationListener locationListener;
2.Setting manager & Listener
locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
}
}
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
String locationProvider = LocationManager.GPS_PROVIDER;
currentLocation = locationManager.getLastKnownLocation(locationProvider);
if (currentLocation != null) {
double lng = currentLocation.getLongitude();
double lat = currentLocation.getLatitude();
Log.d("Log", "longtitude=" + lng + ", latitude=" + lat);
}
I have the following function for retrieving latitude and longitude of the current location of the mobile.
private void getLocation() {
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission
(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
} else {
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
double latti = location.getLatitude();
double longi = location.getLongitude();
lattitude = String.valueOf(latti);
longitude = String.valueOf(longi);
textView.setText("Your current location is" + "\n" + "Lattitude = " + lattitude
+ "\n" + "Longitude = " + longitude);
} else {
Toast.makeText(this, "Unable to Trace your location", Toast.LENGTH_SHORT).show();
}
}
}
This function is being called through a button. The problem is that by clicking the button the function doesn't seem to find the location and keep on toasting the same message which is toasted in the else statement. Can someone please point out that what is it that i am doing wrong.
First you need to define a LocationListener
private final LocationListener mLocationListener = new LocationListener() {
#Override
public void onLocationChanged(final Location location) {
//your code here to get lat lon from location
}
And in the activity
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, LOCATION_REFRESH_TIME,
LOCATION_REFRESH_DISTANCE, mLocationListener);
I have followed this tutorial http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/ for showing popup if GPS in not enabled and getting users current location.
And here is the broadcast receiver which listens to the GPS enabled disabled condition
private BroadcastReceiver mGpsSwitchStateReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {
gps = new GPSTracker(getActivity());
if(gps.canGetLocation()){
Log.d("Your Location", "latitude:" + gps.getLatitude() + ", longitude: " + gps.getLongitude());
Toast.makeText(getActivity(), "GPS Switch", Toast.LENGTH_SHORT).show();
if(gps.getLatitude() != 0 && gps.getLongitude() != 0 ){
final CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(gps.getLatitude(),gps.getLongitude())) // Sets the center of the map to selected place
.zoom(15) // Sets the zoom
.build(); //
gMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
Toast.makeText(getActivity(), "GPS Switch Lat: "+gps.getLatitude()+" Long: "+gps.getLongitude(), Toast.LENGTH_SHORT).show();
}
} else {
if(!gps.isSettingDialogShown())
gps.showSettingsAlert();
}
}
}
};
When I enable the GPS via settings dialog or from drop down menu. GPSTracker class gets the location service and checks for the user's current location in this code fragment
public Location getLocation() {
try {
locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network Enabled");
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS", "GPS Enabled");
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (SecurityException e) {
e.printStackTrace();
}
return location;
}
Problem:
Firstly I don't know why broadcast receiver, receives the action two times and secondly when in normal mode lat lng value in receiver remains zero but in debugging sometimes it returns the value of users current location.
Can you please help what I am doing wrong?
If you don't need to work with Gps and Network Locations separately. I recommend to use FusedLocationProvider. BroadcastReceiver is probably working twice because it works for both gps and network location services
I use these code to get location
But sometime I cannot get the location inside building, and that time the WIFI is connected, Location is enabled.
How can I make sure getLastKnownLocation will return location when it is able to get location?
LocationManager locationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE);
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location == null)
{
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
if(location==null)
{
Toast.makeText(getApplicationContext(),"no location", Toast.LENGTH_SHORT).show();
return;
}
This works for me...
public void updateLoction(View view){
locationManger = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = locationManger.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
txtLat.setText("" + location.getLatitude());
txtLon.setText("" + location.getLongitude());
}
locationManger.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
}
private void setLocation(Location location) {
if (location != null) {
txtLat.setText("" + location.getLatitude());
txtLon.setText("" + location.getLongitude());
}
locationManger.removeUpdates(this);
}
This is the code i use to find my current location but it is not pointing the location accurately, it returns a location as 9.920473,78.102423 instead of 9.909076, 78.100758
I could not figure it out where i am going wrong , please help me with suggestions.
try {
locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
}
else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
Log.e("latitude", ""+latitude);
Log.e("longitude", ""+longitude);
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
Log.e("latitude", ""+latitude);
Log.e("longitude", ""+longitude);
}
}
}
}
}
You are using Network Provider first, which is not very accurate. If you get a location fix from Network your code wont use GPS location. In this case, this much variation in the location is to be expected. If you need exact location, use GPS Provider.
Try with this code:
public void onLocationChanged(Location location) {
if (location != null) {
GeoPoint point = new GeoPoint(
(int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
Toast.makeText(getBaseContext(),
"Latitude: " + location.getLatitude() +
" Longitude: " + location.getLongitude(),
Toast.LENGTH_SHORT).show();
}