app运行没有错误,我想计算距离,当我点击按钮开始它将是开始位置,当点击重置 - 完成位置,它将计算开始和结束之间的距离位置,但它无法计算距离和速度。怎么纠正? 这是我的代码:
public class Tab1 extends Fragment implements LocationListener, View.OnClickListener{
GoogleMap googleMap;
SupportMapFragment mapFragment;
private Button buttonStart;
private Button buttonStop;
private Button buttonReset;
private Chronometer chronometerRun;
private double latStart;
private double lonStart;
private double latStop;
private double lonStop;
private LatLng latLngStart;
private LatLng latLngStop;
private TextView textViewDistance;
Location location;
long time = 0;
@Nullable
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab_1, container, false);
textViewDistance = (TextView)view.findViewById(R.id.textViewDistance);
buttonStart = (Button) view.findViewById(R.id.buttonStart);
buttonStop = (Button) view.findViewById(R.id.buttonStop);
buttonReset = (Button) view.findViewById(R.id.buttonReset);
chronometerRun = (Chronometer) view.findViewById(R.id.chronometerRunning);
buttonStart.setOnClickListener(this);
buttonStop.setOnClickListener(this);
buttonReset.setOnClickListener(this);
initMap();
return view;
}
public void initMap(){
if (googleMap == null) {
mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
}
googleMap = mapFragment.getMap();
googleMap.setMyLocationEnabled(true);
// Getting LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
// Creating a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Getting the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
// Getting Current Location
location = locationManager.getLastKnownLocation(provider);
if(location!=null){
onLocationChanged(location);
}
locationManager.requestLocationUpdates(provider, 20000, 0, this);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
}
public double CalculationByDistance(LatLng StartP, LatLng EndP) {
int Radius = 6371;// radius of earth in Km
double lat1 = StartP.latitude;
double lat2 = EndP.latitude;
double lon1 = StartP.longitude;
double lon2 = EndP.longitude;
double dLat = Math.toRadians(lat2 - lat1);
double dLon = Math.toRadians(lon2 - lon1);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
+ Math.cos(Math.toRadians(lat1))
* Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2)
* Math.sin(dLon / 2);
double c = 2 * Math.asin(Math.sqrt(a));
double valueResult = Radius * c;
double km = valueResult / 1;
DecimalFormat newFormat = new DecimalFormat("####");
int kmInDec = Integer.valueOf(newFormat.format(km));
double meter = valueResult % 1000;
int meterInDec = Integer.valueOf(newFormat.format(meter));
Log.i("Radius Value", "" + valueResult + " KM " + kmInDec
+ " Meter " + meterInDec);
return Radius * c; //km
}
@Override
public void onClick(View v)
{
switch(v.getId())
{
case R.id.buttonStart:
chronometerRun.setBase(SystemClock.elapsedRealtime() - time);
chronometerRun.start();
latStart = location.getLatitude();
// Getting longitude
lonStart = location.getLongitude();
Toast.makeText(Tab1.super.getActivity() , "latStart: "+latStart+"; lonStart: "+lonStart,Toast.LENGTH_LONG).show();
// Creating a LatLng object
latLngStart = new LatLng(latStart, lonStart);
Log.i("latStart: "+latStart , "lonStart: "+lonStart);
break;
case R.id.buttonStop:
time = SystemClock.elapsedRealtime() - chronometerRun.getBase();
chronometerRun.stop();
break;
case R.id.buttonReset:
time = SystemClock.elapsedRealtime() - chronometerRun.getBase();
Toast.makeText(Tab1.super.getActivity() , "Time: "+time,Toast.LENGTH_SHORT).show();
latStop = location.getLatitude();
// Getting longitude of the current location
lonStop = location.getLongitude();
Toast.makeText(Tab1.super.getActivity() , "latStop: "+latStop+"; lonStop: "+lonStop,Toast.LENGTH_LONG).show();
// Creating a LatLng object for the current location
latLngStop = new LatLng(latStop, lonStop);
Log.i("latStop: "+latStop , "lonStop: "+lonStop);
double distance = CalculationByDistance(latLngStart,latLngStop);
String sDistance = Double.toString(distance);
textViewDistance.setText(sDistance);
if (time > 0 ) {
double speed = distance / time*1000; // time in ms
Toast.makeText(Tab1.super.getActivity() , "Distance: "+ distance +"; Speed: "+speed,Toast.LENGTH_LONG).show(); }
time = 0;
chronometerRun.setBase(SystemClock.elapsedRealtime());
chronometerRun.stop();
break;
}
}
@Override
public void onLocationChanged(Location location) {
// Getting latitude of the current location
double latitude = location.getLatitude();
// Getting longitude of the current location
double longitude = location.getLongitude();
// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
// Showing the current location in Google Map
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(14));
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
答案 0 :(得分:0)
你不必像这样自己调用onLocationChanged方法,每次位置发生变化时都会自动调用它。 您必须在重置按钮代码中再次调用getLastKNownLocation并将该位置作为最后一个位置(就像在init映射中那样)或者在onLocationChanged方法中更新自身的全局变量。所以(伪伪代码):
onLocationChanged(Location location)
{
globalVarLocation.latitude = location.latitude;
globalVarLocation.longitude = location.longitude;
}