简体   繁体   中英

Getting Latitude and Longitude Gives Me Always 0,0 uisng GPS

I am getting the a specific error whenever i get latitude and longitude it always shows me 0,0 i have physically changed my location to get effect but still it shows me 0,0 ??

Here is code for my mainacticity class

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();
            }
    }
}

Here is code for my GPSTRacker Class

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;
    }
}

Here is manifest file permissions

 <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" />

GPSTracker is broken. It should never be used. It was voted up here many years ago, when people were new to location tracking and didn't realize the subtle bugs in it. But it should not be used anymore. Its broken by design to a point where fixing it would make it a totally different library. In particular- it confuses a provider being enabled with actually having values and it assumes that getLastKnownLocation will return values when it may not.

Take a look at the ProviderLocationTracker and FallbackLocationTracker classes I've posted here . They provide an API which allows you to accurately tell if a location exists, and fixes a variety of other bugs in GPSTracker.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM