Getting GPS data from Android Phone - android

i try to get gps data. But gps data return null value. When i looked, following 'location' is return null, so i could not get gps data.
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
My code is following;
protected LocationManager locationManager;
public GPSTracker(Context context) {
this.mContext = context;
getLocation();
}
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)
{
}
else if (!isGPSEnabled && !isNetworkEnabled)
{
}
else
{
this.canGetLocation = true;
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
i wait your help. Thanks

Have you set these in your manifest?
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

I changed like following code, then worked successfully.
String bestProvider = locationManager.getBestProvider(criteria, false);
locationManager.requestLocationUpdates(bestProvider, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(bestProvider);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}

Related

How can i get last location without enabling location service on device?

I want to get last location (With any way) without enabling location services on device , is this possible?
How should i do this?
there are three ways to find your location,refer this
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocation cellLocation = (GsmCellLocation)telephonyManager.getCellLocation();
int cellid= cellLocation.getCid();
int celllac = cellLocation.getLac();
If you want to get location without GPS then you should use NETWORK_PROVIDER to get location :
I have taken this module from AndroidHive website You can refer more there.
Your query will be solved with this method getLocation()
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;
// First get location from Network Provider
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
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 Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
You can use NETWORK_PROVIDER as service parameter in the method getLastKnownLocation.

LocationManager, GPS and Network providers.. are triggered both?

This code:
ServiceListener mlistener = new SosServiceListener(getApplicationContext());
manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
manager.requestSingleUpdate(LocationManager.GPS_PROVIDER, mlistener, null);
manager.requestSingleUpdate(LocationManager.NETWORK_PROVIDER, mlistener, null);
Attach a listener once to two events. The fact is: if both events are triggered, the listener is called twice? If yes, how would you prevent it?
Thanks!
This is what I used for one of my projects and is perfectly working:
public Location getLocation() {
try {
mLocationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
// getting GPS status
boolean isGPSEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
boolean isNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
// First get location from Network Provider
if (isNetworkEnabled) {
mLocationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (mLocationManager != null) {
location = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
lat = location.getLatitude();
lng = location.getLongitude();
}
}
}
//get the location by gps
if (isGPSEnabled) {
if (location == null) {
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (mLocationManager != null) {location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
lat = location.getLatitude();
lng = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
If you can assume that devices will be running on device with Google Play installed - much better way is to use this service: https://developer.android.com/training/location/retrieve-current.html

Not able to get location using GPS

I am developing one app. In which, I need to get the current location of the user. I am getting current location using Network but when I start the GPS its not showing me current location. What coubld be the problem?
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
String bestProvider = locationManager.getBestProvider(criteria, true);
System.out.println("Best Provider:"+bestProvider);
// 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
Toast.makeText(mContext, "GPS and Network not enabled!", Toast.LENGTH_SHORT).show();
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
//LocationManager.NETWORK_PROVIDER,
bestProvider,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
//.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
.getLastKnownLocation(bestProvider);
if (location != null) {
startLatitude = location.getLatitude();
startLongitude = location.getLongitude();
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,
bestProvider,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
//.getLastKnownLocation(LocationManager.GPS_PROVIDER);
.getLastKnownLocation(bestProvider);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
I have added permissions:
<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" />
google has released a nice API on their recent Google IO 2013 event:
https://developers.google.com/events/io/sessions/324498944
you should check it out and see how you can minimize your code.
do note that it requires that the device would have the play store app for this to work.
this method has many advantages over using the normal location sensors (battery, speed , accuracy,...) .

Android GPS bearing and speed always zero

I am testing my app on emulator with a gpx file.
Coordinates change, but bearing and speed are always zero.
I've checked my LocationManager settings, and both .supportsSpeed() and .supportsBearing() return true.
My GPS code is almost one-to-one with this one http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/ but this part is most important so I paste it here:
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
} 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");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
Log.d("GPS support",((Boolean)locationManager.getProvider(locationManager.GPS_PROVIDER).supportsBearing()).toString());
Log.d("GPS support",((Boolean)locationManager.getProvider(locationManager.GPS_PROVIDER).supportsSpeed()).toString());
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
I've only added this code
#Override
public void onLocationChanged(Location location) {
this.location = location;
}
I instantiate GPSTracker object in activity's onCreate method, and then use gps.getLocation() where needed.
My .gpx file used for testing is available here: http://snk.to/f-cdzj45ic
Tried it on a physical device, and worked well.
I just thought that gpx with list of waypoints is enough, and bearing and speed will be calculated from it.

getlastknownlocation always return null after I re-install the apk file via eclipse

Recently, I created a simple application to get the GPS location and display it on an Android phone.
In the beginning I was able to get the location after a few tries. But, after I reinstalled the APK file, the getLastKnownLocation() always returns a null value.
The development environment:
API 10 Gingerbread 2.3.6
GPS provider is used
Below is the code I applied to my Android project:
public class MyActivity extends MapActivity {
protected void onCreate(Bundle savedInstanceState) {
mapView = (MapView) findViewById(R.id.myTripMap);
mapController = mapView.getController();
mapView.setSatellite(false);
mapView.setStreetView(true);
mapView.displayZoomControls(false);
mapView.setBuiltInZoomControls(true);//
mapView.setClickable(true);
mapController.setZoom(14);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
provider = locationManager.getBestProvider(criteria, true);
location = locationManager.getLastKnownLocation(provider);
updateMyCurrentLoc(location);
locationManager.requestLocationUpdates(provider, 2, 1, locationListener);
}
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateMyCurrentLoc(location);
}
public void onProviderDisabled(String provider) {
updateMyCurrentLoc(null);
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
};
private void updateMyCurrentLoc(Location location) {
if (location != null) {
// other codes to get the address and display
Toast.makeText(getBaseContext(), "provider used : " + provider).show(); //testing purpose
} else {
str = "No location found";
Toast.makeText(getBaseContext(), str, Toast.LENGTH_SHORT).show();
}
}
}
Can anyone suggest a possible solution to solve the null value returned by getLastKnownLocation()?
Try Following Code
LocationManager locationManager = (LocationManager) getActivity()
.getSystemService(Context.LOCATION_SERVICE);
String locationProvider = LocationManager.NETWORK_PROVIDER;
Location lastlocation = locationManager.getLastKnownLocation(locationProvider);
String CurLat = String.valueOf(lastlocation.getLatitude());
String Curlongi= String.valueOf(lastlocation.getLongitude());
This solution is my little improvement for code by Ravi Tamada.
public Location getLocation() {
int MIN_TIME_BW_UPDATES = 10000;
int MIN_DISTANCE_CHANGE_FOR_UPDATES = 10000;
try {
locationManager = (LocationManager) getApplicationContext()
.getSystemService(LOCATION_SERVICE);
boolean isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean isPassiveEnabled = locationManager
.isProviderEnabled(LocationManager.PASSIVE_PROVIDER);
boolean isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (isGPSEnabled || isNetworkEnabled || isPassiveEnabled) {
this.canGetLocation = true;
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled && 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 (isPassiveEnabled && location == null) {
locationManager.requestLocationUpdates(
LocationManager.PASSIVE_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
}
}
if (isNetworkEnabled && location == null) {
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);
}
}
}else{
return null;
}
} catch (Exception e) {
e.printStackTrace();
}
You just need to add one more line into the code
if (locationManager != null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
} else {
Toast.makeText(
mContext,
"Location Null", Toast.LENGTH_SHORT).show();
}
}
I got the same issue after re-install via eclipse.
I solved going to Android settings / Security / app permissions and confirm location permissions to the application.

Categories

Resources