Getting zero location from network in android

I want to get a location from the web and store it in sqlite. Unfortunately, I always get the longitude and latitude value as 0.0. I can't seem to find the problem, although I can connect to wifi. This is a method that includes a button that calls the location method class:

public void ButtonClick(View V) {
     gps = new GPSTracker(this);
    if (usersqlite_obj.RecvDate(DateOnly,shift)) {
    Common.ShowDialogue(this, "Warning", "Your data has already been saved");
   } else
     {
        latitude = gps.getLatitude();
        longitude = gps.getLongitude();

    usersqlite_obj.open();

    usersqlite_obj.insertDAILYATTENDANCE(Recvdate, ffcode, terrcode,
            drcode, drname, addr, Recvdate, ffmgr, shift,
            Double.toString(latitude), Double.toString(longitude),
            droid_ref_no, SyncStatus,unique_droid_ref_no,DateOnly);
    usersqlite_obj.close();

    Intent i = null;
    i = new Intent(getApplicationContext(), Menu.class);
    startActivity(i);
    finish();
        }
    }
}

      

This is the GPSTracker class that I am using to get the location.

 public class GPSTracker extends Service implements LocationListener {

     private final Context mContext;
     boolean isGPSEnabled = false;
     boolean isNetworkEnabled = false;
     boolean canGetLocation = false;
     Location location; 
     double latitude; 
     double longitude; 
     private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 0; 
     private static final long MIN_TIME_BW_UPDATES = 0; 
     protected LocationManager locationManager;

    public GPSTracker(Context context) {
        this.mContext = context;
        getLocation();
    }

    public Location getLocation() {
        try {
            locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
            // getting network status
            isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (!isGPSEnabled && !isNetworkEnabled) {

            } else {
                this.canGetLocation = true;
                // First get location from Network Provider
                if (isNetworkEnabled) {
                    locationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("Network", "Network");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            } 

        } catch (Exception e) {
            e.printStackTrace();
        }

        return location;
    }

    public double getLatitude(){
        if(location != null){
            latitude = location.getLatitude();
        }

        return latitude;
    }

    public double getLongitude(){
        if(location != null){
            longitude = location.getLongitude();
        }
          return longitude;
    }

    public boolean canGetLocation() {
        return this.canGetLocation;
    }


    public void showSettingsAlert(){
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

        // Setting Dialog Title
        alertDialog.setTitle("GPS is settings");

        // Setting Dialog Message
        alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

        // On pressing Settings button
        alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                mContext.startActivity(intent);
            }
        });

        // on pressing cancel button
        alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
            }
        });

        // Showing Alert Message
        alertDialog.show();
    }

    @Override
    public void onLocationChanged(Location location) {
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

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

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }
}

      

I have set these permissions in my manifest file:

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

      

+3


source to share


2 answers


I'm sure you will have to wait for the update through

public void onLocationChanged(Location loc)
{ 
  // then get the gps coordinated from 
     curlat  = if(loc.hasLatitude())loc.getLatitude();
     curlong = if(loc.hasLongitude())loc.getLongitude();
}

      



while you are querying the variables before getting the location update so will always be null. Also you should check if loc has value before getting it.

0


source


I completed the task using this api:

http://ip-api.com/json



This will give you your current location from your IP address, just call this JSON and you will get everything you need. thank

0


source







All Articles