访问经纬度从一类到另一个 [英] Accessing latitude and longitude from one class to another
问题描述
我要访问在另一个类中纬度和一类经度的值。我有一个名为 Mylocationlistener.java
A类和code如下。我想访问从 register.java
当前的经度和纬度。
我们怎样才能做到这一点?
公共类MyLocationListener延伸活动{
TextView的电视;
公共静态双重纬度;
公共静态双经度;
公共静态INT latitude1;
公共静态INT longitude1;
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
//电视=(TextView中)this.findViewById(R.id.txtLocation);
LM的LocationManager =(的LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener的LL =新mylocationlistener();
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,LL);
}
私有类mylocationlistener实现LocationListener的{
@覆盖
公共无效onLocationChanged(地点){
今天的日期=新的日期();
时间戳currentTimeStamp =新的时间戳(today.getTime());
如果(位置!= NULL){
Log.d(位置发生改变,location.getLatitude()+);
Log.d(位置发生改变,location.getLongitude()+);
纬度=(int)的location.getLatitude();
经度=(INT)location.getLongitude(); 字符串str =\\ n CurrentLocation:+
\\北纬:+ location.getLatitude()+
\\ n经度:+ location.getLongitude()+
\\ n准确:+ location.getAccuracy()+
\\ n CurrentTimeStamp+ currentTimeStamp; Toast.makeText(MyLocationListener.this,STR,Toast.LENGTH_LONG).show();
tv.append(STR); }
}
@覆盖
公共无效onProviderDisabled(字符串提供商){
// Toast.makeText(MyLocationListener.this,关于ProviderDisabled错误,Toast.LENGTH_LONG).show();
}
@覆盖
公共无效onProviderEnabled(字符串提供商){
// Toast.makeText(MyLocationListener.thisonProviderEnabled,Toast.LENGTH_LONG).show();
}
@覆盖
公共无效onStatusChanged(字符串提供商,INT地位,捆绑演员){
// Toast.makeText(MyLocationListener.thisonStatusChanged,Toast.LENGTH_LONG).show();
}
}
}
从上面的类我想通过注册的.java类将其转换为整数值,以获得访问经纬度
我在Register.java这些语句
request.addProperty(纬度,北纬**** ****);
request.addProperty(经度,**** ****经度); //这里我想从上面类访问发送值服务器
按照我也有对两个选项。
选项1
您可以存储纬度和经度到MyLocationListner类的静态变量加倍,你可以注册使用类名MyLocationListener.Variable接取该变量。
选项2
您可以存储纬度和经度到共享preference和访问,在寄存器级共享preference。
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
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.
这篇关于访问经纬度从一类到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!