Not getting Lat/Long on Emulator as well as on device - android

i am done code for getting current lat/long as follow but i always get null from lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); method.
i have tried by 2 ways.
the code is below.
first way,
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location != null)
{
lat = location.getLatitude();
lon = location.getLongitude();
}
and second way,
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Log.d("GPS_PROVIDER","GPS_PROVIDER = " + lm.isProviderEnabled(LocationManager.GPS_PROVIDER));
Log.d("NW_PROVIDER","NW_PROVIDER = " + lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER));
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListenerAdapter()
{
#Override
public void onLocationChanged(Location location)
{
if(location != null)
{
lat = location.getLatitude();
lon = location.getLongitude();
}
}
});
LocationListenerAdapter class is implements the method of LocationListener interface and i keep all method blank
i.e no code written into that methods.
i also use gpx and kml file for emulator to change lat/long but i didn't get yet.
can any one provide best answer.
Thanks in advance.

Related

Android Location Manager returning NULL

I have a simple location manager that normally works, however when the Android device has been turned off and then turned back on, the android location manager returns Null even when I have it requesting updates. I am aware that getLastKnownLocation can return null, however I believe I am handling that in my code. All suggestions Appreciated.
Apparently lon = location.getLongitude(); is crashing it.
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location == null)
{
// request location update!!
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
lon = location.getLongitude();
lat = location.getLatitude();
}
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//Get last known location
location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
//Update if not null
if (location != null)
{
lat = location.getLatitude();
lon = location.getLongitude();
}
//Request update as location manager can return null otherwise
else
{
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
lat = location.getLatitude();
lon = location.getLongitude();
}
}
the android location manager returns Null even when I have it requesting updates
Correct. Requesting updates requests that Android start trying to find out where the device is. That will take a while. In the meantime, getLastKnownLocation() can very well return null.
getLastKnownLocation() is generally only useful if you would like to know the location, but if that data is not ready, you can move on without it. If you need the location, and getLastKnownLocation() returns null, you will to wait to use the Location until onLocationChanged() is called. Note that onLocationChanged() may never be called, as there is no requirement that the user have any location providers enabled, and there is no requirement that the device be in a position to actually get location fixes.
I saw your reply on the airplane mode but you do however have bad practices in your code.
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location == null)
{
// request location update!!
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
lon = location.getLongitude();
lat = location.getLatitude();
}
you already saw here that location in null, that means that you can't access the inner properties such as latitude and longitude. In any case, location requests are asynchronous and it takes a short amount of time for them to start.
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//Get last known location
location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
//Update if not null
if (location != null)
{
lat = location.getLatitude();
lon = location.getLongitude();
}
//Request update as location manager can return null otherwise
else
{
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
lat = location.getLatitude();
lon = location.getLongitude();
}
}
Even here, in the "else" clouse, you can't access the location right away. that means that you can't immediately get the location (it will just hold the last value).
What you should do is implement the onLocationChanges method and hadle the location there in asynchronous way. like this:
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null)
{
lat = location.getLatitude();
lon = location.getLongitude();
}
else
{
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
}
#Override
public void onLocationChanged(Location location) {
lat = location.getLatitude();
lon = location.getLongitude();
}
The only problem was that I had airplane mode enabled on the Android Phone. I'm a bit embarrassed now!

App force closes only sometimes when I get user location

Every once in a while my app will crash when it attempts to get the user location. Unfortunetley I fo not have the error, because it has never happened when I am plugged into my computer...
I am using this to get my user location:
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
I am thinking sometimes this might be null? If so how can I fix this force close?
Its not force closing while getting location.. its getting force closed while assigning longitude and latitude.. put an if(location!=null) before asigning the values
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
//location might be null so
if(location != null)
{
double longitude = location.getLongitude();
double latitude = location.getLatitude();
}
You have no guarantee that location won't be null (there might not be a last known location), so you'll have a NullPointerException in
location.getLongitude()
Check that location != null before proceeding.
Always use Try & catch block when there's a chance of Exception.You will get NullPointerException if location returns null.
try {
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
} catch (Exception e) {
}
I would recommend not rolling your own GPS.. Use Fused Location Provider:
Here is an example :
http://www.kpbird.com/2013/06/fused-location-provider-example.html?m=1

Android LocationManager.getLastKnownLocation() returns null on Android 4.1.1

I am working on android application. I am stuck in problem.
getLastKnownLocation(provider) returns null for Android version 4.1.1, while for other versions it is fine. Provider is enabled and the rest is ok. I don't know where is the problem. Here is the code.
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
location = locationManager.getLastKnownLocation(provider);
double latitude = location.getLatitude();
double longitude = location.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
mMap.clear();
System.out.println("Current Location = "+latLng);
locationManager.getLastKnownLocation will return null if the provider is disabled, and here is the documentation
http://developer.android.com/reference/android/location/LocationManager.html#getLastKnownLocation(java.lang.String)
try this its working for me...
mGoogleMap.setOnMyLocationButtonClickListener(new OnMyLocationButtonClickListener() {
#Override
public boolean onMyLocationButtonClick()
{
Location myLocation = mGoogleMap.getMyLocation();
onLocationChanged(myLocation);
return false;
}
});

issues to get the Latitude using network and GPS provider

Hi I have implemented a location listener in my app which uses the Network Provider to get the GPS values. It's working fine, but now I want to get GPS using network provider and GPS provider. am trying but am getting the same value.
here my code
Location networkLoc = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Location gpsLoc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Where i have to use networkLoc and gpsLoc?
#Override
public void onLocationChanged(Location location) {
double lat1 = (double) (location.getLatitude());//here i get the latitude value..how to know these values are from different provider
double lng1= (double) (location.getLongitude());
latituteField.setText(Double.toString(lat));
longitudeField.setText(Double.toString(lng));
}
It may be the case, that if the user has GPS Enabled your line
provider = locationManager.getBestProvider(criteria, false);
is returning LocationManager.GPS_PROVIDER already, so you just query getLastKnownLocation(provider) twice with the same Provider. Try this instead:
Location networkLoc = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Location gpsLoc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
or if you really don't care which one to use
List<String> providers = locationManager.getProviders(false);
Location[] loc = new Location[providers.size];
int i = 0;
for (String provider: providers){
loc[i++] = locationManager.getLastKnownLocation(provider);
}
EDIT: due to refactoring of the question
//query provider from Location
public void onLocationChanged(Location loc){
String provider = loc.getProvider();
if (provider.equals(LocationManager.GPS_PROVIDER)){
//GPS Location
} else if (provider.equals(LocationManager.NETWORK_PROVIDER)){
//Network Location
}
....
}

Trying to obtain updated locations of android phone but code giving me problems

I am trying to update the location of the phone periodically, but I am getting some errors with my code. Here it is:
LocationManager locm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = locm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
longitude = location.getLongitude();
latitude = location.getLatitude();
}
locm.requestLocationUpdates(LocationManager.GPS, 2000, 10, locationListener);
// error here : Syntax error on token(s), misplaced constructs
The thing is, when I take out the last line of code, there are no errors in the code. Can somebody help me please?? Any help would be much appreciated.
You might want to try this nice library project I found. It's nicely documented and is Apache License 2.0.
http://code.google.com/p/little-fluffy-location-library/
You miss the closing bracket in the new location listener.
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
longitude = location.getLongitude();
latitude = location.getLatitude();
}
}; <--
Your last line ("locm.resquestLocationUpdates...") needs to be within a method. It looks like it's directly in a class.

Categories

Resources