如何从GPS提供商而不是网络提供商访问该位置?

时间:2013-02-02 07:30:37

标签: android gps location location-provider

我正在尝试从最佳提供商处获取该位置。我启用了 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" />

1 个答案:

答案 0 :(得分:0)

我没有详细研究过你的算法,但200米不是GPS的好门槛。 水平精度应低于50米。

只有GPS具有属性过程和高度(我不是100%肯定)和速度。检查有效的高度和路线。 课程(和速度)仅在设备移动时有效。

但是,你能不能简单地查询位置提供者的类型? (GPS)