I am trying an code for finding latitude and longitude but takes lot of time to give coordinates or does not give the coordinates at all, what could be the problem. I am trying my code on Samsung Galaxy tab.
LocationManager mlocManager;
double lat,lng;
LocationListener mlocListener = new MyLocationListener();
I call the below code in onCreate() method
mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
Listener MyLocationListener class code is below
public class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc){
loc.getLatitude();
loc.getLongitude();
String Text = "My current location is: " + "Latitude = " + loc.getLatitude() + "Longitude = " + loc.getLongitude();
String latLongString = "";
if (loc != null) {
lat = loc.getLatitude();
lng = loc.getLongitude();
latLongString = "Lat:" + lat + "\nLong:" + lng;
TextView myLocationText = (TextView)findViewById(R.id.FindUsTextView);
myLocationText.setText("Your Current Position is:\n" + latLongString);
System.out.println(latLongString);
}
/* findUsProgressbarField.setVisibility(View.INVISIBLE);*/
Toast.makeText( FindUs.this, Text, Toast.LENGTH_SHORT).show();
mlocManager.removeUpdates(mlocListener);
}
@Override
public void onProviderDisabled(String provider){
Toast.makeText( getApplicationContext(),"Gps Disabled, Please Enable the GPS", 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){
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mlocManager != null) {
mlocManager.removeUpdates(mlocListener);
mlocManager = null;
}
}
What could be the problem, any pointers or have I not considered some points.
Looking forward to your reply.
thanks.
If your testing inside like most people are while developing this is fairly standard. GPS’s like to see the sky. Try sitting next to a good big window. It helps me.