android:implement altitude of device - android

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.

Related

Android - Lat long values are 0.0 and 0.0

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

android gps location listener not working in some device

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

find lat and lon in android app

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);

Fix decimal numbers

Now I;m receiving the GPS location data from android application, and send it to Labview.
However, the decimal number of gps location value changes due to the location.
I want to fix the decimal number such as 7
For example, what I get is 32.33333 but I need 32.333330
How can I declare the value?
Here is the gps value part
private class MyLocationListener implements LocationListener
{
private String gps2;
#Override
public void onLocationChanged(Location loc) {
if (loc != null) {
/* 화면의 상단에 위치한 TextView에 위도, 경도를 출력함*/
gps.setText("La:"+loc.getLatitude()+", Lo:"+loc.getLongitude());
gps2 = gps.getText().toString();
Main.getInstance().sendMessage(gps2);
}
}
#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
}
}
try doing this way
NumberFormat numberFormat = new DecimalFormat("#.000000");
gps.setText("La:"+numberFormat.format(loc.getLatitude())+", Lo:"+numberFormat.format(loc.getLongitude()));

How to send updated long/lat to server in android?

In my application i want to get the updated location of the mobile user and i want to send it to the server continuously after periodic interval of certain time or after the user travels certain(say 500 meter) distance.I need these things to be done in backgroung.I know for this i have to implement service class. But I am not getting exactly how to do this.I did some work on that.
Can anybody please help me in this issue.
I did following things in the service class.
public class BackGroundService extends Service implements LocationListener{
public static final String Tag = BackGroundService.class.getName();
LocationManager myLocationManager;
Location myLocation;
LocationListener myLocationListener;
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public void OnCreate()
{
super.onCreate();
Log.d(Tag, "Service Started");
android.os.Debug.waitForDebugger();
Criteria criteria = new Criteria();
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAccuracy(Criteria.ACCURACY_LOW);
String locationProvider = myLocationManager.getBestProvider(criteria, true);
myLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
myLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000*60*5, 500, myLocationListener);
myLocation = myLocationManager.getLastKnownLocation(locationProvider);
}
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
longitude = location.getLongitude();
latitude = location.getLatitude();
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Log.d(Tag, "Provider is disabled");
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Log.d(Tag, "Location Provider is enabled");
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
From here I want know how can i get current lat/long of user and send it to the server.
Answer for my question is.......
public class LocationService extends Service{
#Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
final LocationManager mlocmag = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
final LocationListener mlocList = new MyLocationList();
final Location loc = mlocmag.getLastKnownLocation(LocationManager.GPS_PROVIDER);
UpdateWithNewLocation(loc); // This method is used to get updated location.
mlocmag.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocList);
}
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
#Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
}
private void UpdateWithNewLocation(final Location loc) {
// TODO Auto-generated method stub
if(loc!= null)
{
final double lat =loc.getLatitude(); // Updated lat
final double Long = loc.getLongitude(); // Updated long
ConnectMySQL obj = new ConnectMySQL();
obj.call(lat,Long); // Call this method when location is updated and save the data.
}
else
{
String latLongStr = "No lat and longitude found";
Toast.makeText(this, "Your location is "+latLongStr ,Toast.LENGTH_LONG).show();
}
}
public class MyLocationList implements LocationListener
{
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
UpdateWithNewLocation(arg0);
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),"GPS Disable ", Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),"GPS enabled", Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}

Categories

Resources