0

I am using the following code to find current location of user

LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, this);

public void onLocationChanged(Location location) {
    Log.e("changed","location");
    // TODO Auto-generated method stub
    showLocation(location); 
}

But the location of user is not finding.If i change provider to Network Provider its working.But with GPS provider only it not working.

koti
  • 3,681
  • 5
  • 34
  • 58
  • Follow [this](http://stackoverflow.com/questions/1513485/how-do-i-get-the-current-gps-location-programmatically-in-android) Also if you are tesTing this on emulator.Make sure to pass lat and longi through DDMS – LuminiousAndroid Jul 10 '12 at 09:52

4 Answers4

0

Will you please elaborate more? Do you need to find the current location in mapview by Long or Lat? If this is your question then do one thing : Find Lat and Long by sending manually through DDMS or by generating Lat and Lang on click at mapview : It can be done by:

 public void onLocationChanged(Location loc) {
         loc.getLatitude();
     loc.getLongitude();
        String Text = "My current location is: " + "Latitud = " + loc.getLatitude() + "Longitud = " + loc.getLongitude();
    Toast.MakeTest(getApplicationContext(),Text,Toast.Length_Long).show()
  }

So by this you can get your lat and Long.After getting Lat and Long at mapView's click listener you can write below code:

    Projection proj = view1.getProjection();
  GeoPoint loc = proj.fromPixels((int)ev.getX(), (int)ev.getY()); 
   //Getting Lat and Log 
 String longitude =  Double.toString(((double)loc.getLongitudeE6())/1000000);
 String latitude = Double.toString(((double)loc.getLatitudeE6())/1000000);
  GeoPoint point = new GeoPoint(  (int) (loc.getLatitudeE6()),(int) (loc.getLongitudeE6()));
   //Getting location from lat and Long
   String address="";
    Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
     try {
     List<Address> addresses = geoCoder.getFromLocation(point.getLatitudeE6()  / 1E6,     
    point.getLongitudeE6() / 1E6, 1);
    if (addresses.size() > 0) {
  for (int index = 0; index < addresses.get(0).getMaxAddressLineIndex(); index++)
    address += addresses.get(0).getAddressLine(index) + " ";
     }
     }
    catch (IOException e) {                
                              e.printStackTrace();
                          }   
      Toast t =Toast.makeText(getApplicationContext(), "Longitude: "+ longitude +"  Latitude: "+ latitude+" name: "+address, Toast.LENGTH_LONG);
      t.show();

You will get output as you click at mapview. For click event on MapView please write above code in dispatchTouchEvent() as

    public boolean dispatchTouchEvent(MotionEvent ev) {
    int actionType = ev.getAction();
    switch (actionType) {
        case MotionEvent.ACTION_UP:
         //write above code here
              }
          }  

Try this one. Edit: Check your code with this given below code.

    public class MyLocationListener implements LocationListener {
    public void onLocationChanged(Location loc) {
        loc.getLatitude();
        loc.getLongitude();
        String Text = "My current location is: " + "Latitud = "
                + loc.getLatitude() + "Longitud = " +    loc.getLongitude();
       latituteField.setText(String.valueOf(loc.getLatitude()));
   longitudeField.setText(String.valueOf(loc.getLongitude()));
    }

       public void onProviderDisabled(String provider) {
   Toast.makeText(getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT).show();
    }
     public void onProviderEnabled(String provider) {
 Toast.makeText(getApplicationContext(), "Gps Enabled",Toast.LENGTH_SHORT).show();
    }
    public void onStatusChanged(String provider, int status, Bundle extras) {
        }
}
5extremers
  • 66
  • 2
  • 10
  • yes i need to show the current location of user in map view and should update on map view while user is moving – koti Jul 10 '12 at 09:46
0

change the requestLocationUpdates method so that it updates faster and with less change in coordinates. Change it to:-

requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

Try it now. If possible move the phone some meters so that it is detected soon by the GPS satellites. I am presuming that GPS is switched on.

gkris
  • 1,209
  • 3
  • 17
  • 44
  • i changed the code as per your code and i move to outside from my home but no luck still not finding – koti Jul 10 '12 at 09:50
  • Sometimes the GPS takes time to get the location fix, but it doesn't fail. How far did u move? – gkris Jul 10 '12 at 09:52
  • Try adding a toast in the onLocationChanged method, just to be sure. Toast.makeText(this, "found location", Toast.LENGTH_SHORT).show(); – gkris Jul 10 '12 at 09:56
0

First check Your GPS is enabled or disabled.After enabling GPS ,using Location listener to get current location.I am using follwing code to get gps location

String sourceLat, sourceLong;
LocationManager locManager;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);        

    txtCurrLat = (TextView) findViewById(R.id.txtCurrLat);
    txtCurrLong = (TextView) findViewById(R.id.txtCurrLong);

    mapView = (MapView) findViewById(R.id.mapview);


    geoPoint = null;
    mapView.setSatellite(false);

    locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000L,500.0f, locationListener);
    Location location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

    if(location != null)                                
    {
        double fromLat = location.getLatitude();
        double fromLong = roundTwoDecimals(location.getLongitude());

        Toast.makeText(getApplicationContext(), fromLat +fromLong+""  , Toast.LENGTH_SHORT).show();
        sourceLat = Double.toString(fromLat);
        sourceLong = Double.toString(fromLong);

        txtCurrLat.setText(sourceLat+","+sourceLong);   

        btnShow.setOnClickListener(this);
    }else{

        Toast.makeText(getApplicationContext(), "Location is not avilable", Toast.LENGTH_SHORT).show();
    }





}

private void updateWithNewLocation(Location location) {

    String latLongString = "";
    if (location != null) {
        double lat = location.getLatitude();
        double lng = location.getLongitude();
        sourceLat = Double.toString(lat);
        sourceLong = Double.toString(lng);
        latLongString =  sourceLat + "," + sourceLong;
    } else {
        latLongString = "No location found";
        Toast.makeText(getApplicationContext(), latLongString+"", Toast.LENGTH_SHORT).show();
    }
    txtCurrLat.setText(latLongString);
}

private final LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
        updateWithNewLocation(location);
    }

    public void onProviderDisabled(String provider) {
        Toast.makeText( getApplicationContext(), "Gps is Disabled.Please enabale gps", Toast.LENGTH_SHORT ).show();
        updateWithNewLocation(null);
    }

    public void onProviderEnabled(String provider) {
        Toast.makeText( getApplicationContext(), "Gps is Enabled", Toast.LENGTH_SHORT).show();
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {

    }
};
Avadhani Y
  • 7,566
  • 19
  • 63
  • 90
Mohammod Hossain
  • 4,134
  • 2
  • 26
  • 37
0

GPS does not provide a 'get my location on demand' functionality. You have to be patient and wait until the device gets a fix from several satellites. This may take several minutes, even with a clear view of the sky. This is why the OS implements this as a callback. onLocationChanged runs when the device has a fix and you just have to wait for it.

NickT
  • 23,844
  • 11
  • 78
  • 121
  • i need to update the user current location while moving the user.Is it better to use network provider.while moving also – koti Jul 10 '12 at 09:58
  • No, I wouldn't say so - network provider locations can be very inaccurate. Here in London, it's not too bad, as there are lots of phone masts to triangulate from. Out in rural areas the indicated position could be more than 1km away from the true position. – NickT Jul 10 '12 at 10:02