我正在做一个可以跟踪跑步距离的应用程序。 我的想法是当我点击开始时,我将记录我开始运行的位置,然后当我点击停止时,它将记录我结束运行的位置。然后计算这2点之间的距离。 我不知道为什么我的距离结果不管我怎么跑我都会向我显示0km。请帮我看看是否有任何问题〜
package com.example.ifitness;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.SupportMapFragment;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class calorieburn extends ActionBarActivity implements LocationListener{
private GoogleMap map;
private Location startLocation;
private Location endLocation;
private double latA;
private double longA;
private double latB;
private double longB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calorieburn);
SetupMap();
Button start = (Button) findViewById(R.id.start);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
latA = startLocation.getLatitude();
longA = startLocation.getLongitude();
TextView distance = (TextView) findViewById(R.id.distance);
distance.setText("0 km");
TextView calorie = (TextView) findViewById(R.id.calorie);
calorie.setText("0 cal");
}
});
Button stop = (Button) findViewById(R.id.stop);
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
latB = endLocation.getLatitude();
longB = endLocation.getLongitude();
TextView distance = (TextView) findViewById(R.id.distance);
distance.setText(String.valueOf(distance(latA,longA,latB,longB)+ "km"));
TextView calorie = (TextView) findViewById(R.id.calorie);
calorie.setText(String.valueOf(distance(latA,longA,latB,longB)+ "cal"));
}
});
}
public double calories(double distance){
double calories = 0;
calories = distance * 100;
return calories;
}
public static void distanceBetween(){
}
public double distance(double lat,double lng,double latitude,double longtitude){
Location locA = new Location("locA");
locA.setLatitude(lat);
locA.setLongitude(lng);
Location locB = new Location("locB");
locB.setLatitude(latitude);
locB.setLongitude(longtitude);
double distance = locA.distanceTo(locB);
return distance;
}
private void SetupMap() {
if (map == null){
// Getting reference to the SupportMapFragment of activity_main.xml
SupportMapFragment mf = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
// get google map from the fragment
map = mf.getMap();
}
if (map !=null ){
// Enabling MyLocation Layer of Google Map
map.setMyLocationEnabled(true);
// Getting LocationManager object from System Service LOCATION_SERVICE
LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String bestprovider = lm.getBestProvider(criteria,false);
String provider = LocationManager.NETWORK_PROVIDER;
if (provider == null){
onProviderDisabled(provider);
}
// Getting Current Location
Location loc = lm.getLastKnownLocation(bestprovider);
if (loc != null){
onLocationChanged(loc);
}
map.setOnMapLongClickListener(onLongClickMapSettings());
}
}
private OnMapLongClickListener onLongClickMapSettings() {
// TODO Auto-generated method stub
return new OnMapLongClickListener(){
@Override
public void onMapLongClick(LatLng arg0) {
// TODO Auto-generated method stub
Log.i(arg0.toString(), "User Long CLicked");
}
};
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
// 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
map.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
map.animateCamera(CameraUpdateFactory.zoomTo(17));
}
private void makeUseOfNewLocation(Location location) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
}