0

This is my last question about gps

Getting 0.0 for latitude and longitude while showing current location in map

Now here is the code I'm using to get the user's current location.

 LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Criteria crta = new Criteria();
        crta.setAccuracy(Criteria.ACCURACY_FINE);
        crta.setAltitudeRequired(false);
        crta.setBearingRequired(false);
        crta.setCostAllowed(true);
        crta.setPowerRequirement(Criteria.POWER_LOW);
        String provider = mlocManager.getBestProvider(crta, true);
        Location loc = null;
        if (provider != null) {
            loc = mlocManager.getLastKnownLocation(provider);
        }
        LocationListener mlocListener = new MyLocationListener();
        mlocListener.onLocationChanged(loc);
        mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                2000, 10, mlocListener);


public class MyLocationListener implements LocationListener{
    public MyLocationListener() {
    }

    @Override
    public void onLocationChanged(Location loc) {
        if (null != loc) {
        String Text = "Your current location is: \n" + "Latitude = \n"
        + loc.getLatitude() + "\nLongitude = \n" + loc.getLongitude();
        Toast.makeText(getApplicationContext(),Text,Toast.LENGTH_SHORT).show();

        GeoPoint myGeoPoint = new GeoPoint((int)(loc.getLatitude()*1E6),(int)(loc.getLongitude()*1E6));
         mpc.animateTo(myGeoPoint);
         mpc.setZoom(10);
         objMapView.invalidate();

        }
    }

    @Override
    public void onProviderDisabled(String provider){
        Toast.makeText(getApplicationContext(), "gps disabled",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onProviderEnabled(String provider) {
        Toast.makeText(getApplicationContext(), "gps enabled",Toast.LENGTH_SHORT).show();
    }

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

    }
}

Now the problem I'm facing is, it is not showing me the current location when the gps is turned on. The loc object , loc = mlocManager.getLastKnownLocation(provider); always returns null. I got the value for provider as gps.

But if I turn of my gps connection, the same loc object will have relevant information and it works partiall correct. That means, it gives me the nearest location. I mean the full city location where I am sitting.

But if I on my gps connection, it does not give me even the city location also, does not enters if loop only inside location listener class. I am not getting what is going wrong here.

Any one can tell me how to solve it?

Update:

This is the value I get for loc object if my gps is off

Location[mProvider=network,mTime=1331718353322,mLatitude=12.9053401,mLongitude=74.8359128,mHasAltitude=false,mAltitude=0.0,mHasSpeed=false,mSpeed=0.0,mHasBearing=false,mBearing=0.0,mHasAccuracy=true,mAccuracy=36.0,mExtras=Bundle[mParcelledData.dataSize=148]].

But if the gps is on, loca return null

Community
  • 1
  • 1
tejas
  • 2,435
  • 10
  • 37
  • 54

2 Answers2

2

CustomLocationManager.Java

import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

public class CustomLocationManager {

    private LocationManager mLocationManager;
    private LocationValue locationValue;
    private Location networkLocation = null;
    private Location gpsLocation = null;

    private Timer mTimer;

    private boolean isGpsEnabled = false;
    private boolean isNetworkEnabled = false;

    private static CustomLocationManager _instance;

    private CustomLocationManager() {}

    public static CustomLocationManager getCustomLocationManager() {
        if (_instance == null) {
            _instance = new CustomLocationManager();
        }
        return _instance;
    }

    public LocationManager getLocationManager(Context context) {
        if (mLocationManager == null)
            mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        return mLocationManager;
    }

    public boolean getCurrentLocation(Context context, LocationValue result) {
        locationValue = result;
        if (mLocationManager == null)
            mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

        try {
            isGpsEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        } catch (Exception ex) {}

        try {
            isNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        } catch (Exception ex) {}

        if (!isGpsEnabled && !isNetworkEnabled)
            return false;

        if (isGpsEnabled)
            mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, gpsLocationListener);

        if (isNetworkEnabled)
            mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, networkLocationListener);

        mTimer = new Timer();
        mTimer.schedule(new GetLastKnownLocation(), 20000);

        return true;
    }

    LocationListener gpsLocationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            mTimer.cancel();
            locationValue.getCurrentLocation(location);
            mLocationManager.removeUpdates(this);
            mLocationManager.removeUpdates(networkLocationListener);
        }

        public void onProviderDisabled(String provider) {}

        public void onProviderEnabled(String provider) {}

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

    private LocationListener networkLocationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            mTimer.cancel();
            locationValue.getCurrentLocation(location);
            mLocationManager.removeUpdates(this);
            mLocationManager.removeUpdates(gpsLocationListener);
        }

        public void onProviderDisabled(String provider) {}

        public void onProviderEnabled(String provider) {}

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

    private class GetLastKnownLocation extends TimerTask {
        CurrentLocationHandler handler;

        GetLastKnownLocation() {
            handler = new CurrentLocationHandler();
        }

        @Override
        public void run() {
            mLocationManager.removeUpdates(gpsLocationListener);
            mLocationManager.removeUpdates(networkLocationListener);

            if (isGpsEnabled)
                gpsLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

            if (isNetworkEnabled)
                networkLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

            handler.sendEmptyMessage(0);
        }
    }

    private class CurrentLocationHandler extends Handler {
        @Override
        public final void handleMessage(Message msg) {
            if (gpsLocation != null && networkLocation != null) {

                if (gpsLocation.getTime() > networkLocation.getTime())
                    locationValue.getCurrentLocation(gpsLocation);
                else
                    locationValue.getCurrentLocation(networkLocation);

                return;
            }

            if (gpsLocation != null) {
                locationValue.getCurrentLocation(gpsLocation);
                return;
            }

            if (networkLocation != null) {
                locationValue.getCurrentLocation(networkLocation);
                return;
            }

            locationValue.getCurrentLocation(null);
        }
    }
}


LocationValue.Java

import android.location.Location;

public abstract class LocationValue {
    public abstract void getCurrentLocation(Location location);
}


YourActivity.Java

private void getCurrentLocation() {
        CustomLocationManager.getCustomLocationManager().getCurrentLocation(this, locationValue);
    }

    public LocationValue locationValue = new LocationValue() {
        @Override
        public void getCurrentLocation(Location location) {
            // You will get location here if the GPS is enabled
            if(location != null) {
                Log.d("LOCATION", location.getLatitude() + ", " + location.getLongitude());
            }
        }
    };


AndroidManifest.xml

  <uses-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
  • thank you. Please give me some time to check this code. I will be back – tejas Mar 14 '12 at 10:54
  • Super, thank you very much Lalit. It is working perfect. Can you please tell me how to mark exact location of user now. I am able to move to the city where he is standing. I want to mark a round, like the one we get in map application. Would you mind to tell me the way to do it? – tejas Mar 14 '12 at 11:24
  • To draw the marker, just enable myLocation. Here is code mMyLocationOverlay = new MyLocationOverlay(VenueDescription.this,objMapView); mMyLocationOverlay.enableMyLocation(); objMapView.getOverlays().add(mMyLocationOverlay); and set zoom to some higher number like 30+ – tejas Mar 15 '12 at 09:26
0

First of all I'm not familiar with the Android location API, but did you try the GPS outside? Since GPS doesn't work inside buildings, it's pretty hard to retrieve the location of your mobile device using only your GPS. When indoors, typically your "GPS" position is determined using your WiFi acces point connection instead.

xaviert
  • 5,653
  • 6
  • 29
  • 31
  • I don't think this is causing me the problem. Also I tried default map application in android phone. That works perfectly fine.I am suspecting my code now, but not getting what exactly is wrong here. – tejas Mar 14 '12 at 09:42