gpt4 book ai didi

java - ANDROID:在其他类中获取经纬度,但值变为0.0

转载 作者:行者123 更新时间:2023-12-01 22:47:24 24 4
gpt4 key购买 nike

我有两个类..First.java 和 Second.java,

在第一类中.. 纬度、经度和地址在 TextView 中正确显示。但在第二类中,纬度和经度变为 0.0 ,地址变为 null。

谁能帮我解释一下为什么?

这是 First.java 的代码

public class First extends Activity {
private Context context=null;
AppLocationService appLocationService;


String address;
double latitude;
double longitude;


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

context=this;
appLocationService = new AppLocationService(First.this);

btngetLocation=(Button)findViewById(R.id.button_getLocation);

btngetLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {

// getting GPS status
boolean isGPSEnabled = appLocationService
.getStatus(LocationManager.GPS_PROVIDER);
// getting network status
boolean isNetworkEnabled = appLocationService
.getStatus(LocationManager.NETWORK_PROVIDER);

if( isGPSEnabled==false && isNetworkEnabled==false){
showSettingsAlert("Location Service");
}
else {
Toast.makeText(
getApplicationContext(),
"Attempt to get location...Please Wait.. ",
Toast.LENGTH_LONG).show();
if( isNetworkEnabled ==true ){
Location nwLocation = appLocationService
.getLocation(LocationManager.NETWORK_PROVIDER);
if (nwLocation == null){
Toast.makeText(
getApplicationContext(),
"Network bermasalah.. Pastikan Anda terkoneksi Internet, dan coba get Location lagi",
Toast.LENGTH_LONG).show();
}
else {
latitude = nwLocation.getLatitude();
longitude = nwLocation.getLongitude();

Toast.makeText(
getApplicationContext(),
"Mobile Location (NW): \nLatitude: " + latitude
+ "\nLongitude: " + longitude,
Toast.LENGTH_LONG).show();
}
}
else {
Location gpsLocation = appLocationService
.getLocation(LocationManager.GPS_PROVIDER);
if (gpsLocation == null){
Toast.makeText(
getApplicationContext(),
"GPS bermasalah.. Silahkan di ruangan terbuka atau aktifkan network provider Anda, dan coba get Location lagi",
Toast.LENGTH_LONG).show();
}
else {
latitude = gpsLocation.getLatitude();
longitude = gpsLocation.getLongitude();
Toast.makeText(
getApplicationContext(),
"Mobile Location (GPS): \nLatitude: " + latitude
+ "\nLongitude: " + longitude,
Toast.LENGTH_LONG).show();
}
}
}

}
});

}


public class AppLocationService extends Service implements LocationListener{
protected LocationManager locationManager;
Location location;


private static final long MIN_DISTANCE_FOR_UPDATE = 10; //10 meter
private static final long MIN_TIME_FOR_UPDATE = 1000 * 60 * 3; //3 menit

public AppLocationService(Context context) {
locationManager = (LocationManager) context
.getSystemService(LOCATION_SERVICE);
}

public boolean getStatus(String provider){
return locationManager.isProviderEnabled(provider); /
}

public Location getLocation(String provider) {
MIN_TIME_FOR_UPDATE, MIN_DISTANCE_FOR_UPDATE, this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(provider);
locationManager.removeUpdates(this);
return location;
}

return null;
}

@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}

@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
}

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}

public String getAddress(Context ctx, double latitude, double longitude) {
// StringBuilder result = new StringBuilder();
String alamat=null;
try {
Geocoder geocoder = new Geocoder(ctx, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
if (addresses.size() > 0) {
Address address = addresses.get(0);
String jalan = (address.getAddressLine(0));
String locality=address.getLocality();
String country=address.getCountryName();
String country_code=address.getCountryCode();
String zipcode=address.getPostalCode();

alamat = jalan +", "+ locality +", "+ country+", " + country_code+", " + zipcode;

}
} catch (IOException e) {
Log.e("tag", e.getMessage());
}

return alamat;
}



public double getLatitude(){
return latitude;
}

public double getLongitude(){
return longitude;
}


public String getAlamat(){
return address;
}

这是第二个。 java

public class Second extends Activity{

AddLocation data_dr_location;

Double latitude;
Double longitude;
String address;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_description);

data_dr_location = new AddLocation();

address= data_dr_location.getAlamat();
latitude = data_dr_location.getLatitude();
longitude = data_dr_location.getLongitude(); }

最佳答案

在你的第一个java中,只有getter方法,没有setter方法!

private double latitude;
private double longitude;
private String address;

public double getLatitude(){
return latitude;
}

public double getLongitude(){
return longitude;
}


public String getAlamat(){
return address;
}

/* THE BELOW SETTER METHODS ARE MISSING IN YOUR CODE.*/
public void setLatitude(double latitude){
this.latitude = latitude;
}

public void setLongitude(double longitude){
this.longitude=longitude;
}


public setAlamat(String address){
this.address=address;
}

在适当的位置(例如您获得纬度/经度/地址的任何位置),添加这些行。

 latitude = nwLocation.getLatitude();
setLatitude (latitude);
longitude = nwLocation.getLongitude();
setLongitude (longitude);

关于java - ANDROID:在其他类中获取经纬度,但值变为0.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25131743/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com