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 具有属性航向和高度(我不是 100% 确定)和速度。检查有效的高度和路线。路线(和速度)仅在设备移动时有效。

但是你能不能简单地查询位置提供者的类型?(全球定位系统)

于 2013-02-02T14:03:49.587 回答