0

I want to get current location's latitude and longitude. I used LocationManager for this, but it is displaying location as null and latitude and longitude as 0.0. I have used required permissions in manifest and GPS connection.

Code:

LocationdemoActivity.java:

public class LocationdemoActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener mlocListener = new MyLocationListener();
        mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,mlocListener);
        mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mlocListener);

        Button buttonLocation = (Button) findViewById(R.id.buttonLocation);
        buttonLocation.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                Toast.makeText(
                        getApplicationContext(),
                        "loc:" + MyLocationListener.location + "latitude "
                                + MyLocationListener.latitude + "longitude "
                                + MyLocationListener.longitude, 5000).show();

            }

        });

    }
}

MyLocationListener.java:

 public class MyLocationListener implements LocationListener {

    public static double latitude;

    public static double longitude;
    public static Location location;

    @Override
    public void onLocationChanged(Location loc)
    {
        location = loc;
        loc.getLatitude();
        loc.getLongitude();
        latitude = loc.getLatitude();
        longitude = loc.getLongitude();
    }
    @Override
    public void onProviderDisabled(String provider)
    {
    }
    @Override
    public void onProviderEnabled(String provider)
    {
    }
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras)
    {
    }
    protected boolean isRouteDisplayed() {
        return false;
    }
}

Permissions in manifest:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
<uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES"/>

What is the problem?

Hesham Saeed
  • 5,358
  • 7
  • 37
  • 57
Thirupathig
  • 166
  • 9
  • where you are checking this emulator or device? – Akram Jul 26 '12 at 06:54
  • Make sure Network or GPS providers are operating in Location Settings. If you are using Emulator, you will have to emulate your location using DDMS. – Hesham Saeed Jul 26 '12 at 07:57
  • @Akki Iam checking in both device and emulator.. but still iam not getting – Thirupathig Jul 26 '12 at 09:00
  • mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,mlocListener); mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mlocListener); request with only one provider and check – Akram Jul 26 '12 at 09:01
  • @Hesham Saeed Iam running the code in device. In location settings Use GPS satelites is on. Is there anything other in settings . Is there any problem with the code .. – Thirupathig Jul 26 '12 at 09:06
  • If you are trying inside a building the GPS might not work, enable Network Provider which needs Wi-fi or Mobile Data connection. – Hesham Saeed Jul 26 '12 at 09:09
  • @Akki Even I am using only one request, still iam not getting. thanks for your reply. – Thirupathig Jul 26 '12 at 09:11
  • @Thirupathig onLocationChanged(Location loc) gets called when device will move from one location to another. try to emulate your location using DDMS tool and use GPS provider only – Akram Jul 26 '12 at 09:14
  • @Akki I used DDMS tool and used GPS provider only. It is working fine in the emulator for first time.If we run the app second time it is showing again null. But I am getting null always in the device. what should i do..?? – Thirupathig Jul 26 '12 at 10:18
  • @HeshamSaeed can you please suggest me how to enable network provider.. at my position wifi is available..... – Thirupathig Jul 26 '12 at 10:19
  • It is in the same page as where you enabled GPS. just put tick on everything inside Location settings. [Maybe you also need to remove "static" on the Location & Latitude & Longitude global variable declarations]. – Hesham Saeed Jul 26 '12 at 10:21

1 Answers1

0

Try to do something like this

mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,mlocListener);

location = mlocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

Button buttonLocation = (Button) findViewById(R.id.buttonLocation);

 buttonLocation.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {

              Toast.makeText(getApplicationContext(),
                            "loc:" + location + "latitude "
                                    + location.latitude + "longitude "
                                    + location.longitude, 5000).show();

                }

            });
Akram
  • 7,548
  • 8
  • 45
  • 72
  • 1
    hey it is working fine in device .... thanks bro.... and I have a doubt if i move to another location, lat and long will be changed right... and i want make my listener code as there are no variables and empty code in onLocationChanged(). I think so there will be no problem – Thirupathig Jul 26 '12 at 10:58