获得纬度和经度给我总是0,0 uisng GPS

时间:2017-01-06 07:42:07

标签: android android-studio gps

每当我得到纬度和经度时,我都会得到一个特定的错误它总是显示0,0我已经改变了我的位置以获得效果但是它仍然显示我0,0 ??

这是我的主题类

的代码
public class MainActivity extends AppCompatActivity {
    Thread t;
    ConnectivityManager connectivityManager;
    NetworkInfo activeNetworkInfo;
    SaveDataOffline saveoffline;
    SaveDataOnline saveonline;
    GPSTracker loc;
    TelephonyManager telephonyManager;
    String x;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        loc = new GPSTracker(this);
        t = new Thread(new Runnable() {
            @Override
            public void run() {
                if(loc.isupdate==true) {
                    SaveData();
                    loc.isupdate = false;
                }
            }
        });
    }
    public void SaveData(){
            if(loc.location!=null){
                connectivityManager
                        = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
                activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
                boolean isConnected = activeNetworkInfo != null &&
                        activeNetworkInfo.isConnectedOrConnecting();
                telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
                if(!isConnected){
                    //notonline
                    Toast.makeText(this,"Offline",Toast.LENGTH_LONG).show();
                    saveoffline = new SaveDataOffline(this);
                    saveoffline.insertData(String.valueOf(loc.getLat()),String.valueOf(loc.getLng()),String.valueOf(telephonyManager.getDeviceId()));
                }
                else{
                    //online
                    Toast.makeText(this,"Online",Toast.LENGTH_LONG).show();
                    saveonline = (SaveDataOnline) new SaveDataOnline(this,String.valueOf(loc.getLat()),String.valueOf(loc.getLng()),String.valueOf(telephonyManager.getDeviceId())).execute();

                }
                // \n is for new line
            }else{
                // can't get location
                // GPS or Network is not enabled
                // Ask user to enable GPS/network in settings
               // loc.showSettingsAlert();
            }
    }
}

以下是我的GPSTRacker类的代码

public class GPSTracker extends Thread implements LocationListener {

    Context context;
    LocationManager locationManager;
    String provider;
    String lat;

    public String getLng() {
        return lng;
    }

    public String getLat() {
        return lat;
    }

    String lng;
    Location location;
    boolean isupdate;
    public  GPSTracker(Context context){
        this.context = context;
        isupdate = false;
        new Thread(new Runnable() {
            @Override
            public void run() {
                check();
            }
        });
    }

    void check(){
        // Get the location manage
        locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        provider = locationManager.getBestProvider(criteria, false);
        location = locationManager.getLastKnownLocation(provider);

        // Initialize the location fields
        if (location != null) {
            onLocationChanged(location);
            isupdate = true;
        }

    }
    @Override
    public void onLocationChanged(Location location) {
        this.lat = String.valueOf(location.getLatitude());
        this.lng = String.valueOf(location.getLongitude());
    }

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

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }

    public Context getActivity() {
        return context;
    }
}

这是清单文件权限

 <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

1 个答案:

答案 0 :(得分:3)

GPSTracker坏了。永远不应该使用它。很多年前,它被投票到这里,当时人们不熟悉位置跟踪并且没有意识到其中的细微错误。但它不应该再被使用了。它被设计破坏到一定程度,固定它将使它成为一个完全不同的库。特别是 - 它使正在启用的提供程序混淆了实际拥有的值,并假设getLastKnownLocation将返回值,而不是。

查看我发布的here的ProviderLocationTracker和FallbackLocationTracker类。它们提供了一个API,允许您准确地告诉 一个位置是否存在,并修复GPSTracker中的各种其他错误。