用于计算自行车行驶距离的Android应用程序

时间:2017-02-25 10:52:54

标签: android

我想构建一个Android应用程序,用于计算自行车行驶的距离,并根据所覆盖的总距离发送维护或服务通知。有人请帮助我!

 public class Main2Activity extends AppCompatActivity implements View.OnClickListener {
    Button start, stop;
    TextView distanceValue, dist, last;
    DistanceService gps;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        start = (Button) findViewById(R.id.btn_start);
        //stop = (Button) findViewById(R.id.btn_stop);
        distanceValue = (TextView) findViewById(R.id.distance);
        start.setOnClickListener(this);
       // stop.setOnClickListener(this);


       // dist = (TextView) findViewById(R.id.txt_dist);
        //last = (TextView) findViewById(R.id.txt_last);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,@NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case Utility.REQUEST_CODE_ASK_PERMISSIONS:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(getApplicationContext(), "Granted", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(getApplicationContext(), "Permission Denied", Toast.LENGTH_LONG).show();

                }
                break;
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.btn_start:
                gps = new DistanceService(Main2Activity.this,distanceValue);
                startService(new Intent(this, DistanceService.class));


                break;



        }

    }
}
Distance Service.java
.............
public class DistanceService {
    private static final int REQUEST_CODE_ASK_PERMISSIONS = 123;
    double distanceKm,distanceMeters;

    protected LocationManager locManager;
    private LocationListener locListener = new myLocationListener();
    static final Double EARTH_RADIUS = 6371.00;

    private boolean gps_enabled = false;
    private boolean network_enabled = false;
    private final Context context;
    TextView tv;
    public DistanceService(Context ctx,TextView textView){
        context=ctx;
        tv=textView;
        location();
    }
    public void location() {

        locManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);

        try {
            gps_enabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        } catch (Exception ex) {
            ex.printStackTrace();
           Toast.makeText(context,ex.toString(),Toast.LENGTH_LONG).show();
        }
        try {
            network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        } catch (Exception ex) {
        }
        Log.v("Debug", "in on create.. 2");
        if (gps_enabled) {
            checkPermission(context);
            locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
            Log.v("Debug", "Enabled..");
        }
        if (network_enabled) {
            locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,locListener);
            Log.v("Debug", "Disabled..");
        }
        Log.v("Debug", "in on create..3");
    }

    private class myLocationListener implements LocationListener
    {
        double lat_old=0.0;
        double lon_old=0.0;
        double lat_new;
        double lon_new;
        double time=10;
        double speed=0.0;
        @Override
        public void onLocationChanged(Location location) {
            Log.v("Debug", "in onLocation changed..");
            if(location!=null){
                checkPermission(context);
                locManager.removeUpdates(locListener);
                //String Speed = "Device Speed: " +location.getSpeed();
                lat_new=location.getLongitude();
                lon_new =location.getLatitude();
                String longitude = "Longitude: " +location.getLongitude();
                String latitude = "Latitude: " +location.getLatitude();
                double distance =getDistance(lat_new, lon_new, lat_old, lon_old);
                speed = distance/time;
                tv.setText( longitude+"\n"+latitude+"\nDistance is: "
                        +distance+"\nSpeed is: "+speed);

                Toast.makeText(context, longitude+"\n"+latitude+"\nDistance is: "
                        +distance+"\nSpeed is: "+speed , Toast.LENGTH_SHORT).show();
//                tv.setText(longitude+"\n"+latitude+"\nDistance is: "
//                        +distance+"\nSpeed is: "+speed );
                lat_old=lat_new;
                lon_old=lon_new;
            }
        }
        @Override
        public void onProviderDisabled(String provider) {}
        @Override
        public void onProviderEnabled(String provider) {}
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {}
    }
    public double getDistance(double lat1, double lon1, double lat2, double lon2){
        Location locationA = new Location("point A");
        locationA.setLatitude(lat1);
        locationA.setLongitude(lon1);

        Location locationB = new Location("point B");
        locationB.setLatitude(lat2);
        locationB.setLongitude(lon2);
        distanceMeters = locationA.distanceTo(locationA);
        distanceKm = distanceMeters / 1000f;
        return  distanceKm;
       // last.setText("mtr:"+distanceMeters+"km:"+String.format("%.2f Km",distanceKm ));
    }

//    public double CalculationByDistance(double lat1, double lon1, double lat2, double lon2) {
//        double Radius = EARTH_RADIUS;
//        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));
//        return Radius * c;
//    }

    public static boolean checkPermission(final Context context)
    {
        int currentAPIVersion = Build.VERSION.SDK_INT;
        if(currentAPIVersion>= 23)
        {
            if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ||
                    ContextCompat.checkSelfPermission (context, Manifest.permission.ACCESS_COARSE_LOCATION )!= PackageManager.PERMISSION_GRANTED) {
                if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.ACCESS_FINE_LOCATION)||
                        ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.ACCESS_COARSE_LOCATION) ) {
                    AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
                    alertBuilder.setCancelable(true);
                    alertBuilder.setTitle("Permission necessary");
                    alertBuilder.setMessage("External storage permission is necessary");
                    alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
                        public void onClick(DialogInterface dialog, int which) {
                            ActivityCompat.requestPermissions((Activity) context,
                                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},  REQUEST_CODE_ASK_PERMISSIONS  );
                        }
                    });
                    AlertDialog alert = alertBuilder.create();
                    alert.show();
                } else {
                    ActivityCompat.requestPermissions((Activity) context,
                            new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},  REQUEST_CODE_ASK_PERMISSIONS );
                }
                return false;
            } else {
                return true;
            }
        } else {
            return true;
        }
    }

1 个答案:

答案 0 :(得分:0)

为了计算得到旧的Lat-Lang并获得新的Lat-Lang并找到它们之间的距离

logistic_regression()