我正在尝试制作地图应用程序。
有两个活动需要获取当前位置。 第一个活动是MainActivity,所有获取当前位置的代码都在其中。 第二个是用户可以设置出发地的活动。 当用户单击按钮将出发点设置为当前位置时,我需要从MainActivity获取值。
我制作了CurrentLocationDTO来解决此问题。但是以某种方式,当我在第二个活动中调用currentDTO.getLatitude()时,它仅返回0.0。
我不知道为什么即使在MainActivity中完成Jackson
之后调用它也会返回初始化值。
我通过在每个类中编写currentDTO.setLatitude(value);
来创建DTO对象。像这样做错了吗?还是有其他解决此问题的想法,请与我分享。
这是MainActivity.java
CurrentLocationDTO currentDTO = new CurrentLocationDTO();
这是CurrentLocationDTO.java
public class MainActivity extends AppCompatActivity {
private final String TAG = this.getClass().getSimpleName();
private final String TMAP_API_KEY = "10062d5b-d3d3-4b7d-8977-acd3ac2dc390";
LocationManager lm;
TMapView tMapView;
EditText Departure, Arrival;
Button Search;
LinearLayout Layout;
FloatingActionButton Current, Navigate;
String D_lon;
String D_lat;
String A_lon;
String A_lat;
//floatingActionButton 활성화/비활성화
Boolean FabActive = false; //navigate 버튼용
private ArrayList<TMapPoint> arPoint;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout linearLayoutTmap = (LinearLayout) findViewById(R.id.linearLayoutTmap);
tMapView = new TMapView(this);
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); //위치 관리자 객체 참조
Departure = findViewById(R.id.departure);
Arrival = findViewById(R.id.arrival);
Search = findViewById(R.id.search);
Layout = findViewById(R.id.search_layout);
Current = findViewById(R.id.current_location);
Navigate = findViewById(R.id.navigate);
tMapView.setSKTMapApiKey(TMAP_API_KEY);
linearLayoutTmap.addView(tMapView);
tMapView.setIconVisibility(true);
setGps();
Departure.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivityForResult(new Intent(MainActivity.this, MapListActivity.class), 200);
}
});
Arrival.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivityForResult(new Intent(MainActivity.this, MapListActivity.class), 300);
}
});
}
public void setGps() {
//final LocationManager lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION, android.Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, // 등록할 위치제공자(실내에선 NETWORK_PROVIDER 권장)
1000, // 통지사이의 최소 시간간격 (miliSecond)
1, // 통지사이의 최소 변경거리 (m)
mLocationListener);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, // 등록할 위치제공자(실내에선 NETWORK_PROVIDER 권장)
1000, // 통지사이의 최소 시간간격 (miliSecond)
1, // 통지사이의 최소 변경거리 (m)
mLocationListener);
}
private LocationListener mLocationListener = new LocationListener() {
public void onLocationChanged(Location location) {
double latitude;
double longitude;
if (location != null) {
CurrentLocationDTO currentLocationDTO = new CurrentLocationDTO();
//I get values from location object here, and use setter to store it
latitude = location.getLatitude();
longitude = location.getLongitude();
currentLocationDTO.setLatitude(latitude);
currentLocationDTO.setLongitude(longitude);
Log.e("Test", currentLocationDTO.getLatitude() + ", " + currentLocationDTO.getLongitude()); //I can get proper values here
Log.e(TAG, latitude + " , " + longitude);
tMapView.setLocationPoint(longitude, latitude);
tMapView.setCenterPoint(longitude, latitude);
}
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (requestCode == 200) {
if (resultCode == RESULT_OK) {
String D_str = data.getStringExtra("selected");
D_lat = data.getStringExtra("lat");
D_lon = data.getStringExtra("lon");
Departure.setText(D_str);
// Toast.makeText(getApplicationContext(), "requestCode= " + requestCode, Toast.LENGTH_SHORT).show();
// Toast.makeText(this, D_str, Toast.LENGTH_SHORT).show();
Toast.makeText(this, "lat : " + D_lat + " lon : " + D_lon, Toast.LENGTH_SHORT).show();
}
}
if (requestCode == 300) {
if (resultCode == RESULT_OK) {
String A_str = data.getStringExtra("selected");
A_lat = data.getStringExtra("lat");
A_lon = data.getStringExtra("lon");
Arrival.setText(A_str);
// Toast.makeText(getApplicationContext(), "requestCode= " + requestCode, Toast.LENGTH_SHORT).show();
Toast.makeText(this, A_str, Toast.LENGTH_SHORT).show();
}
}
}
}
}
MapListActivity.java-我尝试使用package com.example.gpgpt.myapplication;
public class CurrentLocationDTO {
private double latitude;
private double longitude;
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
,但它返回DTO的初始值
getter
谢谢