我有两个简单的课程。主要和某种gps帮手。当gps posiotion发生变化时,我正试图达到距离(在位置变化时)。它工作和Toast好,但当我尝试使用setter保存变量dis,并在我的MainActivity(最后一个方法称为ObliczanieOdl)中达到它时,总是有0.0。为什么?有什么建议吗?
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
@BindView(R.id.butStop)
Button butStop;
@BindView(R.id.butWynik)
Button butKoniec;
private GoogleMap mMap;
private String dlugosc;
private String szerokosc;
private LatLng StartP, StopP;
private Date czasS;
private DatabaseHelper mDatabaseHelper;
private String adres;
private int Dystans;
private boolean oneStop = false;
private GPStracker gpStracker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
ButterKnife.bind(this);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
ActivityCompat.requestPermissions(MapsActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 123);
Intent intent = getIntent();
gpStracker = new GPStracker(getApplicationContext());
dlugosc = intent.getStringExtra("dlugosc");
szerokosc = intent.getStringExtra("szerokosc");
StartP = new LatLng(Double.parseDouble(szerokosc), Double.parseDouble(dlugosc));
czasS = new Date();
mDatabaseHelper = new DatabaseHelper(this);
mDatabaseHelper.setStartP(getAdres(StartP));//wyciągnięcie adresu rozpoczęcia podróży
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
addMarker(StartP);
}
private void addMarker(LatLng pozycja) {
mMap.addMarker(new MarkerOptions().position(pozycja));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(pozycja)
.zoom(10).build();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
@OnClick(R.id.butStop)
public void butStop() {
if (oneStop == false) {
oneStop = true;
Location location = gpStracker.getlocation();
if (location != null) {
StopP = new LatLng(location.getLatitude(), location.getLongitude());
}
addMarker(StopP);
mDatabaseHelper.setKoniecP(getAdres(StopP)); //wyciągnięcie adresu zakonczenia podróży
//wyciągnięcie odległości i czasu podróży
mDatabaseHelper.setCzasP(String.valueOf(ObliczanieOdl(StartP, StopP)) + " KM " + "w czasie: " + WyliczCzas());
boolean insertData = mDatabaseHelper.addData();
} else {
toastMessage("Twoja podróż została zakończona");
}
}
@OnClick(R.id.butWynik)
public void butKoniec() {
if (oneStop == false) {
toastMessage("Musisz zakończyć podróż wciskając STOP");
} else {
Intent intent = new Intent(MapsActivity.this, ListDataActivity.class);
startActivity(intent);
}
}
public double ObliczanieOdl() {
Double metry = gpStracker.getDis();
return metry;
}
}
,其次是GPS助手:
public class GPStracker implements LocationListener {
Context context;
double plat;
double plon;
double clat;
double clon;
public double dis;
public GPStracker(Context c) {
context = c;
}
public Location getlocation() {
if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(context, "Uprawnienia nie przyznane", Toast.LENGTH_SHORT).show();
}
LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
boolean isGPSenabled;
isGPSenabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (isGPSenabled) {
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 1, this);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
return location;
} else {
Toast.makeText(context, "Proszę włączyć GPS", Toast.LENGTH_LONG).show();
}
return null;
}
@Override
public void onLocationChanged(Location location) {
clat = location.getLatitude();
clon = location.getLongitude();
if (clat != plat || clon != plon) {
dis += getDistance(plat, plon, clat, clon);
plat = clat;
plon = clon;
setDis(dis);
}
Toast.makeText(context, String.valueOf(dis), Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
public double getDistance(double lat1, double lon1, double lat2, double lon2) {
double latA = Math.toRadians(lat1);
double lonA = Math.toRadians(lon1);
double latB = Math.toRadians(lat2);
double lonB = Math.toRadians(lon2);
double cosAng = (Math.cos(latA) * Math.cos(latB) * Math.cos(lonB - lonA)) +
(Math.sin(latA) * Math.sin(latB));
double ang = Math.acos(cosAng);
double dist = ang * 6371;
return dist;
}
public double getDis() {
return dis;
}
public void setDis(double dis) {
this.dis = dis;
}
}