17

I am using the following code to get the current location namely (latitude and longitude), but I am not getting current location (latitude and longitude).

Anyone knows why?

package com.ram.currentlocation;

import android.app.Activity;    
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

public class Location_Gps extends Activity
{

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      /* Use the LocationManager class to obtain GPS locations */
      LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

      LocationListener mlocListener = new MyLocationListener();
      mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
    }

    /* Class My Location Listener */
    public class MyLocationListener implements LocationListener
    {

      @Override
      public void onLocationChanged(Location loc)
      {

        loc.getLatitude();
        loc.getLongitude();

        String Text = "My current location is: " +
        "Latitud = " + loc.getLatitude() +
        "Longitud = " + loc.getLongitude();

        Toast.makeText( getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
      }

      @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)
      {

      }
    }
}

P.S.: I am using the following permissions in the manifest file:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_CORSE_LOCATION"/>
Mahdi Alkhatib
  • 1,954
  • 1
  • 29
  • 43
Jeeva
  • 1,166
  • 4
  • 18
  • 39
  • 1
    First mention what permission are you using and where are you checking this in device or emulator...? – Dinesh Sharma May 23 '11 at 10:34
  • Do you have `ACCESS_FINE_LOCATION` in the manifest? – Gabriel Negut May 23 '11 at 10:35
  • I am using two permission in my manifest I am getting current location latitude 0.0 and longitude 0.0 – Jeeva May 23 '11 at 10:35
  • Duplicate of http://stackoverflow.com/questions/5988681/i-want-to-use-gps-but-my-code-uses-wifi-why/5988838#5988838 – Nikhil May 23 '11 at 10:41
  • [hear][1] is the best solution for this problem. [1]: http://stackoverflow.com/questions/8291728/how-i-can-get-current-geographical-location-of-the-device-programatically/10595644#10595644 – Datta Kunde May 15 '12 at 07:00
  • You don't need to request both permissions. If you are granted permission to access the user's fine location, you're automatically granted permission to access the coarse location as well. – Lyudmil Nov 08 '12 at 09:36
  • http://stackoverflow.com/a/17857993/1318946 – Pratik Butani Jul 25 '13 at 12:28

4 Answers4

25

You have a mistake in your manifest file. Correct one is:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
evilone
  • 22,410
  • 7
  • 80
  • 107
5

You have to send location fix to emulator,as emulator is software you need to provide with location fix.

If you are using eclipse go to

Window -> Show view -> Other

Select Android tab and search for emulator control.
After u see emulator control window navigate to location controls.
As u see in below pic

sending loc fix

After you have sent loc fix you can use

Location loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

Where lm is LocationManager object so 90% of work is done ;)

Cheers!

chengbo
  • 5,789
  • 4
  • 27
  • 41
Santosh Kumar
  • 189
  • 1
  • 9
3

In your android manifest did you set the permissions?

android.permission.ACCESS_FINE_LOCATION

As far as only getting (0,0) co-ords you're probably using the emulator. If you're using eclipse go to the emulator control and at the bottom you can send the fake co-ords to the device

manno23
  • 184
  • 1
  • 9
1

You are not using the correct permission. The correct one is:

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

By the way, your demand can only be fulfilled by:

<using-permission android:name="ACCESS_FINE_LOCATION" />

So there is no need for ACCESS_COARSE_LOCATION

Mahdi Alkhatib
  • 1,954
  • 1
  • 29
  • 43
Noman
  • 4,049
  • 10
  • 38
  • 59