-3

我在 android 中借助 gps 编写了以下用于显示距离的 java 文件。它在 toast 中显示距离,但我想以 km 显示它,所以我该怎么做?

    package com.dansaurabh.speedone;

    import android.app.Service;
    import android.content.Intent;
    import android.location.GpsStatus.Listener;
    import android.location.Criteria;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.os.IBinder;
    import android.util.Log;
    import android.widget.Toast;

    public class gpslocation extends Service {
    public final static String key1 = "com.saurabh.speedone.mes1";
    LocationManager locationManager;
    Location currentLoc;
    // Double par1,par2,par3,par4;
    private double par1 = 0;
    private double par2 = 0;
    private double currentlong = 0;
    private double currentlat = 0;
    String distance;

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub

        Log.d("LocationUpdateService", "onCreate is called");
        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        /* ################ */

        final Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);
        criteria.setPowerRequirement(Criteria.POWER_LOW);

        /* ################ */
        Log.d("LocationUpdateService", "onStartCommand is called");

        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) == 
     false) {
            // put dialoug box of start gps service
            // getNetworkLocation();
            // add dilouge for start gps

            Toast.makeText(this, "start GPS service", 
    Toast.LENGTH_LONG).show();

        } else {
            getGPSLocation();
        }

        return super.onStartCommand(intent, flags, startId);
    }

    /*
     * private void getNetworkLocation(){ Log.d("LocationUpdateService",
     * "getNetworkLocation is called"); currentLoc =
     * locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
     * 
     * String data = "Network : " + currentLoc.getLatitude() + " :: "
     * +currentLoc.getLongitude(); // updateUI(data); } // above tells about
     * last known location
     */

    private void getGPSLocation() {

        /*
         * Log.d("LocationUpdateService", "getGPSLocation is called");
         * currentLoc =
         * locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
         * if(currentLoc!=null){ String data = "getLastKnownLocation : GPS : " +
         * currentLoc.getLatitude() + " :: " +currentLoc.getLongitude(); //
         * updateUI(data);
         * 
         * 
         * }else{ updateUI("No GPS Current Loc found"); }
         */
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                0, new LocationListener() {

                    public void onStatusChanged(String provider, int 
    status,
                            Bundle extras) {
                        // TODO Auto-generated method stub

                    }

                    public void onProviderEnabled(String provider) {
                        // TODO Auto-generated method stub
                        // updateUI(provider + " Enabled");
                    }

                    public void onProviderDisabled(String provider) {
                        // TODO Auto-generated method stub
                        // updateUI(provider + " Disabled");
                    }

                    public void onLocationChanged(Location location) {
                        // TODO Auto-generated method stub
                        currentLoc = locationManager

   .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (currentLoc != null) {

                            currentlat = 
    currentLoc.getLatitude();
                            currentlong = 
    currentLoc.getLongitude();
                            distance();
                            // in km

                        }
    else {
    Toast.makeText(gpslocation.this, "no location changed", Toast.LENGTH_LONG).show();
   }
                    }

                    public void distance() {
                        // measure distance

                        double earthRadius = 3958.75;

                        double dLat = Math.toRadians(par1 -  
    currentlat);
                        double dLng = Math.toRadians(par2 - 
    currentlong);
                        double a = Math.sin(dLat / 2) * 
    Math.sin(dLat / 2)
                                + 
    Math.cos(Math.toRadians(currentlat))
                                * 
    Math.cos(Math.toRadians(par1))
                                * Math.sin(dLng / 2) *  
    Math.sin(dLng / 2);
                        double c = 2 * Math.atan2(Math.sqrt(a),
                                Math.sqrt(1 - a));
                        double dist = earthRadius * c;


                        Intent it = new Intent(gpslocation.this, 
    Run.class);
                        it.putExtra(key1, distance);

                        startActivity(it);
                    }
                });

    }

    @Override
    public void onDestroy() {
        // updateUI("onDestroy is called");
        Toast.makeText(this, "app closeing", Toast.LENGTH_LONG).show();
        Intent it = new Intent(gpslocation.this, Run.class);
        it.putExtra(key1, distance);

        startActivity(it);

        super.onDestroy();
    }

    private void updateUI(CharSequence dist) {
        Log.i("LocationUpdateService,distance", "Data :: " + dist);
        Toast.makeText(this, dist, Toast.LENGTH_SHORT).show();
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    }

请帮助我提前谢谢

4

2 回答 2

0
  1. 提供有意义的变量名。什么是par1par2?我敢打赌,从现在开始,即使你乍一看也不会知道。看起来您正在使用它来计算距离(将两者都定义为 0 并且不再设置),因此您正在计算到北极的距离(抱歉,没有浪费太多时间来分析应该简化的代码) .

  2. 在 Android 中,Location如果可以,最好使用对象而不是原始double坐标。

  3. 计算长距离(甚至是小距离)是一件棘手的事情。不要假装你可以比你之前的其他人做得更好。正如 AlexWien 在我面前的答案中所说,寻找一个合适的公式。或者,更好的是,使用内置的Location.distanceTo(Location),它返回到另一个点的距离,以为单位。对于定制(?-不确定Android API在这方面的可扩展性如何,我敢打赌它是完全的),网上也有可以使用的库。

如果这是我,我会提供我的内置的、引用的Location(比如,Location myDestination),检索当前的Location(比如,Location currentLocation),然后使用currentLocation.distanceTo(myDestination). 然后,我可能会转换并舍入该值以提供以公里(或英里,对于盎格鲁撒克逊人)为单位的良好输出,使用 Java 中众所周知的方法对数字进行舍入以获得良好的输出。

简单,如果你问我。:)

于 2013-03-09T14:34:30.153 回答
0

如果我正确理解了您的问题,那么它很简单:

private void updateUI(CharSequence dist) {
    Log.i("LocationUpdateService,distance", "Data :: " + dist);
    Toast.makeText(this, dist.toString() + " km", Toast.LENGTH_SHORT).show();
}
于 2013-03-09T13:35:49.950 回答