1

最高のプロバイダーから場所を取得しようとしています。GPSを有効にしました。また、緯度と経度を印刷するときは、ネットワーク プロバイダーから取得しています。

私の質問は:

GPS が有効になっている場合は、GPS を介して場所を 30 秒間検索します。その後、精度が 200 メートル未満になったら、それを使用します。精度が 200 メートルを超える場合は、もう一度検索して、ネットワーク プロバイダーから始めます。

その後、両方の精度を比較し、より正確なプロバイダーのデータを取得します。

これが私のコードです:

LocationUtil.java

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;

public class LocationUtil
{
    Activity activity;
    Location location;

    public LocationUtil(Activity activity)
    {
        this.activity = activity;
    }

    public int getLogitudeE6()
    {
        LocationManager lm = (LocationManager) activity
                .getSystemService(Context.LOCATION_SERVICE);
        Location location = lm
                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
        int lg = (int) (((double) location.getLongitude()) * 1E6);
        System.out.println("Longitude :: " + lg);
        return lg;
    }

    public int getLatitudeE6()
    {
        LocationManager lm = (LocationManager) activity
                .getSystemService(Context.LOCATION_SERVICE);

        Location location = lm
                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
        int lt = (int) (((double) location.getLatitude()) * 1E6);
        System.out.println("Latitude :: " + lt);
        return lt;
    }

    public double getLogitude(Location location)
    {
        if (location == null)
        {
            LocationManager lm = (LocationManager) activity
                    .getSystemService(Context.LOCATION_SERVICE);

            location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if (location == null)
            {
                location = lm
                        .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            }
            this.location = location;
        }

        return location.getLongitude();
    }

    public double getLatitude(Location location)
    {
        if (location == null)
        {
            LocationManager lm = (LocationManager) activity
                    .getSystemService(Context.LOCATION_SERVICE);
            location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if (location == null)
            {
                location = lm
                        .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            }
            this.location = location;
        }
        return location.getLatitude();
    }

    public double getAccuracy(Location location)
    {
        if (location == null)
        {
            LocationManager lm = (LocationManager) activity
                    .getSystemService(Context.LOCATION_SERVICE);
            location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if (location == null)
            {
                location = lm
                        .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            }
            this.location = location;
        }
        return location.getAccuracy();
    }
    public double getLogitude()
    {
        if (location == null)
        {
            LocationManager lm = (LocationManager) activity
                    .getSystemService(Context.LOCATION_SERVICE);
            Location location = lm
                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
            this.location = location;
        }

        return location.getLongitude();
    }


    public double getLatitude()
    {
        if (location == null)
        {
            LocationManager lm = (LocationManager) activity
                    .getSystemService(Context.LOCATION_SERVICE);
            Location location = lm
                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
            this.location = location;
        }
        return location.getLatitude();
    }
}

CaptureMain.java

import java.util.Timer;

import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class CaptureMain extends Activity implements LocationListener {
    /** Called when the activity is first created. */
    TextView txtNewLocation;
    String strLatBack, strLongBack, strLatitude, strLongitude,strAccuracy;
    LocationUtil locationUtil;
    Location location;
    LocationManager lm;
    String bestProvider;
    Timer timer;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        txtNewLocation = (TextView) findViewById(R.id.txtLocation);
        locationUtil = new LocationUtil(this);
        lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        try {
            Criteria criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_FINE);
            bestProvider = lm.getBestProvider(criteria, false);

            Log.i("Log", "Best provider is :  "+bestProvider);
            strLatitude = String.valueOf(locationUtil.getLatitude(location));
            strLongitude = String.valueOf(locationUtil.getLogitude(location));
            strAccuracy = String.valueOf(locationUtil.getAccuracy(location));
            txtNewLocation.setText("Latitude :" + strLatitude + ", Longitude :"
                    + strLongitude + " , Accuracy "+ strAccuracy);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onLocationChanged(Location location) {
        System.out.println("On Location change called:: ");

        if (location != null) {
            this.location = location;
        }
        strLatitude = String.valueOf(locationUtil.getLatitude(location));
        strLongitude = String.valueOf(locationUtil.getLogitude(location));
        strAccuracy = String.valueOf(locationUtil.getAccuracy(location));
        if (strLatitude.equals("") || strLongitude.equals("")) {
            txtNewLocation.setText("Locating current position..");
        } else {
            txtNewLocation.setText("Latitude :" + strLatitude + ", Longitude :"
                    + strLongitude + " , Accuracy "+ strAccuracy);
        }

    }

    @Override
    protected void onStart() {
        super.onStart();
        startListening();
    }

    @Override
    protected void onResume() {
        super.onResume();
        startListening();
    }

    @Override
    protected void onPause() {
        super.onPause();
        stopListening();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopListening();
        finish();
    }

    private void stopListening() {
        if (lm != null)
            lm.removeUpdates(this);
    }

    private void startListening() {
        lm.requestLocationUpdates(bestProvider, 0, 0, this);
    }

    @Override
    public void onProviderDisabled(String provider) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

}

マニフェストに追加された権限:

   <uses-permission android:name="android.permission.INTERNET" >
    </uses-permission>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
4

1 に答える 1

0

あなたのアルゴリズムを詳細に調べたわけではありませんが、200m は GPS の適切なしきい値ではありません。水平精度は 50 m 未満である必要があります。

GPS のみが属性 course と高度 (100% 確信はありません) と speed を持っています。有効な高度とコースも確認してください。コース (および速度) は、デバイスが移動した場合にのみ有効です。

しかし、単純にロケーション プロバイダーのタイプを照会することはできないのでしょうか? (GPS)

于 2013-02-02T14:03:49.587 に答える