Tuesday 22 April 2014

Latitude and longitude finder


Java Class to Access longitude and latitude of device.


package com.example.locationmanager;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
double latitude = 0, longitude = 0;
LocationManager lman;
String provider =null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button traceme =(Button)findViewById(R.id.Trackme);
traceme.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {

lman =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
if(lman.isProviderEnabled(LocationManager.GPS_PROVIDER))
provider=LocationManager.GPS_PROVIDER;

if (lman.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
provider=LocationManager.NETWORK_PROVIDER;

if (provider!=null) {
lman.requestLocationUpdates(provider, 10000, 10, listener);
Log.v(provider, "This is current provider");

} else {
Log.v(provider, "No  provider");
}

}
});

}


@Override
protected void onDestroy() {
lman.removeUpdates(listener);
// TODO Auto-generated method stub
super.onDestroy();
}


LocationListener listener =new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onLocationChanged(Location location) {
longitude =location.getLatitude();
latitude =location.getLongitude();
Toast.makeText(MainActivity.this,
                        "longitude: "+longitude + " \n  latitude:  " +        latitude,
                       Toast.LENGTH_SHORT).
                       show();
Log.v(longitude +"= Longitude" +latitude +" = latitude", "result");
}
};

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

No comments:

Post a Comment

How to do text writing animation?  In Android Development we do not have the option to use any other custom font in our default Tex...