1

hello i'm pretty new to android.. i'm making an application which needs exact(approx 50m accuracy acceptable) user location.. i'm using locationmanager and locationlistener.. whenever i start the application i need user location returned. problem is that onlocationchanged method in locationlistener returns the latitude longitude only when they change.. how do i get user location ?

locmgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, loclist_netwk); this is how i'm calling the class where i've implemented locationlistener.

`

package com.example.gpsmanager;

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

public class MyLocationListener extends Activity implements LocationListener
{
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mylocation_layout);
    }
    @Override

    public void onLocationChanged(Location loc) {
        // TODO Auto-generated method stub
        loc.getLatitude();
        loc.getLongitude(); 
        String text="my current location is"+"lat: "+loc.getLatitude()+"long: "+loc.getLongitude();
        //TextView text1=(TextView) findViewById(R.id.textView1);
        //text1.setText(text+"");
        Toast.makeText(MyLocationListener.this, text, Toast.LENGTH_LONG).show();        
    }

    @Override
    public void onProviderDisabled(String arg0) {
        // TODO Auto-generated method stub
        String text="GPS Provider not availabe";
    }

    @Override
    public void onProviderEnabled(String arg0) {
        // TODO Auto-generated method stub
        String text="GPS Provider availabe";
    }

    @Override
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        // TODO Auto-generated method stub

    }

}

` pllzz plzz help guys... thankss..

tanmayub
  • 86
  • 3
  • 13
  • `getLastKnownLocation(String provider)` of `LocationManager` might help. – Glenn Feb 10 '13 at 07:00
  • getLastKnownLocation gives the last known user location not current.. I guess... – tanmayub Feb 10 '13 at 07:26
  • Look at this older topics: [how-do-i-get-the-current-gps-location-programmatically-in-android][1] and [what-is-the-simplest-and-most-robust-way-to-get-the-users-current-location-in-a][2] [1]: http://stackoverflow.com/questions/1513485/how-do-i-get-the-current-gps-location-programmatically-in-android [2]: http://stackoverflow.com/questions/3145089/what-is-the-simplest-and-most-robust-way-to-get-the-users-current-location-in-a/ – Palejandro Feb 10 '13 at 08:23
  • not really useful... the applications stops "unfortunately".... :-( – tanmayub Feb 10 '13 at 12:19

1 Answers1

0
      For user location you can use Reverse Geocoding 

      For it u have to send only lat,long.
Code is below:-

public String getAddress(double lat, double lng,Context mContext) {
        Geocoder geocoder = new Geocoder(mContext, Locale.getDefault());
        try {
            List<Address> addresses = geocoder.getFromLocation(lat, lng,1);
            String add="";
            for(int i=0;i<addresses.size();i++){
            Address obj = addresses.get(i);
            //String = obj.getAddressLine(i);
            add = add+obj.getAddressLine(i)+","+obj.getLocality()+","+obj.getAdminArea()+","+obj.getCountryName();

            Log.v("IGA", "\n"+"Address " + add);

            }
            return add;
            } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT).show();
            return null;
        }
    }
  • Not good..!! I need to find user GPS locations not the address... and I dont want the user location with OnLocationChanged... I want the user location as soon as the user logs into the application... – tanmayub Feb 12 '13 at 06:11