I am trying to fetch my device's location for an application. The code returns 0.0 for latitude and longitude. What do I need to change? Stack Overflow wouldn't let me post this question without another sentence. Hopefully, this much text is enough.
//Getting the location
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
if(location !=null){
plat = location.getLatitude();
plong = location.getLongitude();
}
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
};
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
Log.d("lat: ",Double.toString(plat));
Log.d("long: ",Double.toString(plong));
RequestLocationUpdates is asynchronous- it takes time. The value of plat and plong will not be ready until onLocationChanged is called. SO you can't use them until then.
move
Log.d("lat: ",Double.toString(plat));
Log.d("long: ",Double.toString(plong));
to
public void onLocationChanged(Location location)
and make sure that GPS is enabled
Related
I have written a GPS code using location listener for capturing latitude and longitude of a current location and it is working fine for most of my devices.
But some devices( particularly Samsung ) is not able to load this latitude longitude from location listener.
This is the code i have used continuously changing latitude and longitude.
public class MyLocationListner implements LocationListener {
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
String getLatitude = "" + location.getLatitude();
String getLongitude = "" + location.getLongitude();
double x = location.getLatitude();
double y = location.getLongitude();
}
#Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
}
To call above location listener i am using below piece of code in my activity.
emphasized text
{LocationManager location_manager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
LocationListener listner = new MyLocationListner();
location_manager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0, listner);
}
Please give the solution why it is not working.
Thanks in advance
i need to get the lat and lon in a activity. In my oncreate method I tried the code below but im not getting anything when i try to print the Lat. What am I doing wrong. I have included in the manifest file too. I need two strings with the value of Lat and lon
LocationManager manager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocationListener lis = new LocationListener(){
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
Log.d("", "location " + location.getLatitude());
}
#Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
};
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, lis);
I am making an app in which i have to get altitude of device and my code is as follows:
public class MyLocationListener implements LocationListener
{
public void onLocationChanged(Location loc)
{
try
{
System.out.println("............ ..............................Location changedin 11");
latitude = loc.getLatitude();
longitude = loc.getLongitude();
altitude=loc.getAltitude();
System.out.println("alt"+altitude);
System.out.println("latitude123 "+latitude);
System.out.println("longi: "+longitude);
System.out.println("longitude curr_lon");
loc.getAccuracy();
System.out.println("loc"+loc.getAccuracy());
latitudee=(TextView)findViewById(R.id.latitudee);
latitudee.setText(""+latitude);
longitudee=(TextView)findViewById(R.id.longitudee);
longitudee.setText(""+longitude);
accuracyy=(TextView)findViewById(R.id.accuracyy);
accuracyy.setText(""+loc.getAccuracy());
System.out.println("alti:"+altitude); // alti is coming out to be 0.0
}
catch (Exception e1) {
e1.printStackTrace();
}
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
The value of altitude is coming to be0.0 .Any help will be appreciated.
Wait for more than 1 minute GPS takes some time to take altitude from satellite after one minute it will show altitude.
i'm new in java/android app and i'm creating an app that uses user-location. I want the location to be updated at the begining of the app.
The problem is that my location class is an activity and i don't want to show another contentview for this class.
Actually, i want the location thing to be done in background, without changing the UI, in a separated class.
Is it possible? How?
Thanks :P
There is no need to put the location in a different activity, the LocationManager already does it in the background:
public void getLocation(){
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
gpsLocationListener = new LocationListener() {
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onLocationChanged(Location location) {
//do something with the new location
if (location != null)
gpsLocation = location;
}
};
gpsLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 0, gpsLocationListener);
}
Using the LocationManager you should be able to use what ever kind of activity (or service) you want.
I have a service that runs this thread to get GPS coordinates
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
GpsListener gpsLocationListener = new GpsListener();
long minTime = 5000; // 5 sec for test purposes...
float minDistance = 1;
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime,
minDistance, gpsLocationListener);
and of course my gpsLocationListener looks like this:
class GpsListener implements LocationListener{
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
latitude = location.getLatitude();
longitude = location.getLongitude();
//float speed = location.getSpeed();
checkin();
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
}
where the onLocationChanged method sends the latitude and longitude to a server once it is obtained.
Unfortunately this method doesn't run as reliably as I'd like in my testing, where I basically get up and walk around to see if it registers my location has changed.
How do I make a different function run if it hasn't updated in X amount of seconds. This different function will simply send the last known coordinates?
Insight appreciated!
You would need to set up a recurring timer. Android - Reliably getting the current location might help you in what you are trying to do.