0

I am new to android development. I am trying to get the Location using GPS in android. I have adopted two classes: one for the GPSTracker and the other one for the Activity. The location is getting set as null everytime and when i checked in LogCat, i get the following error:

native_start failed in startNavigating

I have set the permission in the manifest for GPS data as well. I even tried to pass fake latitude and longitude by using the emulator control, to no effect. what might be the reason? I am coding for the Android 2.3

My code is: //GPSTracker.java

public class GPSTracker extends Service implements LocationListener {

    private final Context mcontext;
    boolean isGPSEnabled = false;
    boolean canGetLocation = false;
    double latitude,longitude;
    Location location;

    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;

    protected LocationManager locationManager;
    protected LocationListener listener;

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

    public Location getLocation() {
        try{
            locationManager = (LocationManager) mcontext.getSystemService(LOCATION_SERVICE);


            listener = new LocationListener() {

                @Override
                public void onStatusChanged(String provider, int status, Bundle extras) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onProviderEnabled(String provider) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onProviderDisabled(String provider) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onLocationChanged(Location location1) {
                    // TODO Auto-generated method stub
                    location = location1;
                }
            };

            isGPSEnabled = locationManager.isProviderEnabled(locationManager.GPS_PROVIDER);
            if(isGPSEnabled){
                this.canGetLocation = true;
                locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, listener);
                Log.d("GPS", "Enabled");
                if(locationManager!=null){
                    location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
                    if(location!=null){
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                        //Log.d("Latitude", latitude.toString());
                        //Log.d("Longitude", longitude.toString());
                    }
                    else{
                        Log.d("Location","Null");
                    }
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }

        return location;
        // TODO Auto-generated method stub

    }

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

    public void showSettingsAlert(){
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(mcontext);
        alertDialog.setTitle("GPS Settings");
        alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

        alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                mcontext.startActivity(intent);
            }
        });
        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 arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderDisabled(String arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        // TODO Auto-generated method stub

    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    public double getLatitude() {
        // TODO Auto-generated method stub
        if(location!=null){
            Log.d("Location",location.toString());
            latitude = location.getLatitude();
        }
        return latitude;
    }

    public double getLongitude() {
        // TODO Auto-generated method stub
        if(location!=null){
            Log.d("Location",location.toString());
            longitude = location.getLongitude();
        }
        return longitude;
    }
    }

//LocationActivity.java

public class LocationActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location);



        Button button = (Button) findViewById(R.id.button1); 
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                  GPSTracker gps = new GPSTracker(LocationActivity.this);
                    if(gps.canGetLocation() == false){
                        Log.d("GPSLogFromActivity","False");
                        gps.showSettingsAlert();
                    }
                    else{
                        double latitude = gps.getLatitude();
                        System.out.println("Latitude: "+latitude);
                        double longitude = gps.getLongitude();
                        System.out.println("Longitud: e"+longitude);

                        Log.d("GPSLogFromActivity","True");
                        Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
                    }

            }
        });
Gaurav Sood
  • 157
  • 3
  • 16

1 Answers1

0

hey friend have look on this

My current location always returns null. How can I fix this?

Community
  • 1
  • 1
Bhushan Shirsath
  • 258
  • 3
  • 12
  • I also thought tried to get the location from the network provider. After changing the permission and the GPSProvider to the Network provider, i managed t remove the error, but i still get the location as Null, even after passing in data from the emulator control. Totally stuck on this one.. :/ – Gaurav Sood Mar 21 '13 at 06:52
  • hey friend private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 0; private static final long MIN_TIME_BW_UPDATES = 0; you have to keep this 0 then only you will get the location on standby current code you set it to 10 it means after the movement of 10m it will override the onlocationchamge method **** locationManager.removeUpdate(locationListner);****** locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, listener); try to get the location from NETWORK_PROVIDER also – Bhushan Shirsath Mar 21 '13 at 07:48
  • its working now. still does not work on the emulator, but works on my device. just one question. will the latitude and longitude change automatically as the location changes? currently, on my device, i got location using Maps. after i moved to another location, i had to change the location using maps itself. is there a method that i can automate that process? onLocationChange() method does suggest to do the work.... – Gaurav Sood Mar 21 '13 at 09:45
  • yes they will change automatically but the best practice is to unregister the locationListners when you go to stand by or it will drain your battery which ofcourse not good practise to develope an app so try to rely on lastknownlocation initially. ofcourse it depend on how much accurate location is required for you app if it does then you have to do compromise with battery best ideal value id MIN_DISTANCE_CHANGE_FOR_UPDATES = 10m n as you are in india so there 90% chances tht you will get null location but such thing will not happen infact newer happens outside please Rate answer UP – Bhushan Shirsath Mar 21 '13 at 13:45