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()));
Related
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);
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
}
}
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.
The application doesnt point to where I am at..here is what i have done:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mv=(MapView) findViewById (R.id.mapView);
mv.setBuiltInZoomControls(true);
// mv.setSatellite(true);
mv.setStreetView(true);
lmanager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
llistener=new MyLocationListener();
lmanager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, llistener);
}
private class MyLocationListener implements LocationListener
{
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
if(location!=null)
{
Toast.makeText(getBaseContext(),"Location changed: "+location.getLatitude()+" lang: "+ location.getLongitude() , Toast.LENGTH_SHORT).show(); // no toast is shown
}
p=new GeoPoint((int)(location.getLatitude()*1E6), (int)(location.getLongitude()*1E6));
mapC.animateTo(p);
mapC.setZoom(18);//no zooming happens
}
#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
}
To my manifest I added both of these:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
first of all sorry for poor english. when the location was changed and you set the value into the map you need to invalidate the map using the invalidate() method of MapView class so you can get the updated value on the map
mapC.animateTo(p);
mapC.setZoom(18);
mv.invalidate();
as well as add after you set the value at initial time set into the map like
mv.setBuiltInZoomControls(true);
mv.setStreetView(true);
mv.invalidate();
I am writing app, that collects location data, based on GPS. And i have next problem:
when i try to get GPS data and GPS is turned off, i show notification that ask for turning on GPS, and it starts(on click of course) intent with "ACTION_LOCATION_SOURCE_SETTINGS". Question: how can i know that user turned it on? is there some broadcasted actions, or i can set some listener, or something else?
public class MyLocationListener implements LocationListener
{
#Override
public void onLocationChanged(Location loc) {
GlobalHelper.handler.post(GlobalHelper.update_location);
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
GlobalHelper.system_message(provider + " Ausgeschaltet", 0,false);
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
GlobalHelper.system_message(provider + " Eingeschaltet",0,false);
}
#Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}
}
You can monitor android system settings, see Monitor Android system settings values