Randroid
Randroid

Reputation: 3698

Accessing latitude and longitude from one class to another

I want to access the values of latitude and longitude of one class in another class. I have a class named Mylocationlistener.java and the code is given below. I want to access the current latitude and longitude from register.java.

How can we do this?

    public class MyLocationListener extends Activity {   
       TextView tv;
       public static double latitude;
       public static double longitude;
        public static int latitude1;
         public static int longitude1;
        @Override 
          public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
            setContentView(R.layout.main);         
           // tv = (TextView)this.findViewById(R.id.txtLocation);
           LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
           LocationListener ll = new mylocationlistener();
          lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, ll);    
           }  
             private class mylocationlistener implements LocationListener {
                @Override 
               public void onLocationChanged(Location location) {    
               Date today = new Date();  
          Timestamp currentTimeStamp = new Timestamp(today.getTime());
                 if (location != null) {
              Log.d("LOCATION CHANGED", location.getLatitude() + "");
                Log.d("LOCATION CHANGED", location.getLongitude() + "");
                 latitude =(int) location.getLatitude();
                 longitude = (int) location.getLongitude();

              String str = "\n CurrentLocation: "+
                 "\n Latitude: "+ location.getLatitude() + 
              "\n Longitude: " + location.getLongitude() + 
                   "\n Accuracy: " + location.getAccuracy() + 
    "\n CurrentTimeStamp "+ currentTimeStamp; 

      Toast.makeText(MyLocationListener.this,str,Toast.LENGTH_LONG).show();
      tv.append(str); 

    } 
} 
       @Override
         public void onProviderDisabled(String provider) {
                // Toast.makeText(MyLocationListener.this,"Error on   ProviderDisabled",Toast.LENGTH_LONG).show();
       }    
         @Override
          public void onProviderEnabled(String provider) {
         //       Toast.makeText(MyLocationListener.this,"onProviderEnabled",Toast.LENGTH_LONG).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
     //  Toast.makeText(MyLocationListener.this,"onStatusChanged",Toast.LENGTH_LONG).show();
       }
              }
        }

from the above class I want to access latitude and longitude by converting them as integer values in order access in Register .java class

I have these statements in Register.java

         request.addProperty("latitude", ****latitude**** );
         request.addProperty("longitude",****Longitude**** );

        // here I want to send the values to server by accessing from above class

Upvotes: 0

Views: 871

Answers (1)

Chirag
Chirag

Reputation: 56925

As per me there are two options for that .

Option-1

You can store latitude and longitude into static double variable in MyLocationListner class and you can acess that variable using MyLocationListener.Variable name in register class.

Option-2

You can store that latitude and longitude into sharedPreference and access that sharedPreference in register class.

Upvotes: 1

Related Questions