访问从一个类到另一个类的纬度和经度

时间:2011-07-05 06:37:42

标签: android object scope

我想访问另一个类中一个类的纬度和经度值。我有一个名为Mylocationlistener.java的类,代码如下。我想从register.java访问当前的纬度和经度。

我们怎么做?

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

从上面的类我想通过将它们转换为整数值来访问纬度和经度,以便在Register .java类中访问

我在Register.java中有这些陈述

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

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

1 个答案:

答案 0 :(得分:1)

据我所知,有两种选择。

选项-1

您可以将纬度和经度存储到MyLocationListner类中的静态双变量中,并且可以使用寄存器类中的MyLocationListener.Variable名称来访问该变量。

选项-2

您可以将该纬度和经度存储到sharedPreference中,并在寄存器类中访问该sharedPreference。