0

After reviewing some articles on Stack Overflow, I could not get solutions to my question. As usual, I am always getting null in return of getLastKnownLocation. I have implemented LocationListener as well. Below is code.

For your information, I have checked both providers (network and GPS) in below code and they are always returning false however if i check value of sBestProvider then I am getting "Network". How it is coming?

Permissions.

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />  

JAVA Code

public class ActivityMarkingLocation extends Activity implements LocationListener {

GoogleMap oMap ;
LocationManager oLocationManager;
ConnectionDetector oConnectionDetector ;
Location oLocation;
Button oBtnGetCurrentLocation;
String sBestProvider;

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

    oLocationManager = (LocationManager)  getSystemService(Context.LOCATION_SERVICE);
    Criteria oCriteria = new Criteria();
    sBestProvider = oLocationManager.getBestProvider(oCriteria, false);
    boolean b =oLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    System.out.println("GPS Status " + b);

    boolean c = oLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    System.out.println("Network Status " + c);
    System.out.println("Provider " + sBestProvider);        
    oLocation = oLocationManager.getLastKnownLocation(sBestProvider);
    System.out.println("Provider : " + sBestProvider);
    System.out.println("LocationManager : " + oLocationManager);

    if (oLocation == null)
        System.out.println("Location Null " );
    else
        System.out.println("Location not null");

    oBtnGetCurrentLocation = (Button) findViewById(R.id.btnGetCurrentLocation);
    oBtnGetCurrentLocation.setOnClickListener(new OnClickListener() {

        @Override   
        public void onClick(View v) {

            oLocation = oLocationManager.getLastKnownLocation(sBestProvider);

            if (oLocation != null)
                System.out.println("Location found");
            if (oLocation != null)
                System.out.println("Position : " + oLocation.getLatitude() + "  " + oLocation.getLongitude());


        }
    });

}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    oLocationManager.requestLocationUpdates(sBestProvider, 600, 10, this);



}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_landing_screen, menu);
    return true;
}
public boolean isGoogleMapsInstalled()
{
    try
    {
        ApplicationInfo info = getPackageManager().getApplicationInfo("com.google.android.apps.maps", 0 );
        return true;
    } 
    catch(PackageManager.NameNotFoundException e)
    {
        return false;
    }
}

@Override
public void onLocationChanged(Location arg0) {
    // TODO Auto-generated method stub
    oLocation = arg0;
    System.out.println("Location changed");
}

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

}

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

}

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

}
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Ashish Chauhan
  • 39
  • 1
  • 10

2 Answers2

0

Edit: Try this Solution for you

Are you running app on emulator or on mobile phone ? If you are running on emulator you have to simulate gps input to your vitual device. Extended answer for you To connect to the console open a command line and type:

telnet localhost 5554  

You then can use the geo command to set a latitude, longitude and if needed altitude on the device that is passed to all programs using the gps location provider. See the link above for further instructions.

The specific command to run in the console is:

geo fix <longitude value> <latitude value>

Maybe this would be helpful link here

Community
  • 1
  • 1
0

If you are trying to develop from the house, maybe your GPS isn't catching the signal.

Also, you may have GPS disabled.

Try using another provider - WiFi or GSM.

Jehy
  • 4,729
  • 1
  • 38
  • 55
  • device wifi is on and even it is showing me "Network" as a provider. however, since morning facing this weird issue. – Ashish Chauhan Mar 06 '14 at 12:05
  • Yup, Wifi is on, but the best provider is obviously GPS. And GPS can be unable to get your location at some place. – Jehy Mar 06 '14 at 12:08