-3

Androidのgpsを使用して距離を表示するための次のJavaファイルを作成しました。トーストに距離を表示しますが、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

私があなたの質問を正しく理解していれば、それは次のように簡単です:

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 に答える
0
  1. 意味のある変数名を指定してください。par1ととは何par2ですか? これから先、一見しただけではわからないでしょう。これを使用して距離を計算しているように見えます (両方を 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 に答える