I am not getting gps coordinates from NETWORK_PROVIDER sometimes
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0, mlocListener);
//init();
}
/* Class My Location Listener */
public class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc)
{
//_pd.dismiss();
loc.getLati开发者_Python百科tude();
loc.getLongitude();
String Text = "My current location is: " +"Latitud = " + loc.getLatitude() +"Longitud =" + loc.getLongitude();
txtInfo.setText(Text);
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)
{
}
}
this is my code....SOmetimes i get gps coordinates and sometimes i dont why is it happening?
Getting Geo Fix using GPS requires long time, hence you need to switch between Wirless network provider and GPS to get Geo coordinates reliably. It is good Idea to first poll the Wireless network and if not enabled/available then fallback to GPS provider.
Good Reference : Android-location-providers-gps-network-passive
This is because some locations doesn't show the latitude and longitude in android. This is not an big problem you just check your lat and lng returns null or not. If it returns null you just show a simple message there is no lat and lng at this location.
精彩评论