Search code examples
androidlocation

Locating current position in android without GPS


I want to develop an application in which, if i open the app, it will show current location of my device. I want to do it without using gps. I have write a code for it. But it will show United States location while opening the map. I want it to show my current location (i.e Pune, India). How do i get my approximate correct location?

Here is my code

protected void onCreate(Bundle arg0) {
    // TODO Auto-generated method stub
    super.onCreate(arg0);
    setContentView(R.layout.main);

    LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

      LocationListener locListener = new LocationListener(){
          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();
          }

Solution

  • You should request 'mlocManager' to locate, and set its listener:

    public void getCurrentLocation()
    {
        if (mlocManager != null) {
            mlocManager .requestLocationUpdates(
                    LocationManager.NETWORK_PROVIDER, 0, 0, locListener );
            mlocManager .requestLocationUpdates(
                    LocationManager.GPS_PROVIDER, 0, 0, locListener );
        }
    
    }