Build an application which retrieve current position But How to switch or use GPS as well as Network provider in application.
There are 3 location providers in Android Which to use Depend on accuracy and availability of network.
Initialize LocationManager and LocationListener:
Now Check for GPS. GPS provides most accurate distance.If GPS is enabled then use GPS other wise Network.
This checks for GPS if enabled then provide accurate distance .
Location listener class is
There are 3 location providers in Android Which to use Depend on accuracy and availability of network.
Initialize LocationManager and LocationListener:
LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
LocationListener mlocListener = new MyLocationListener();
Now Check for GPS. GPS provides most accurate distance.If GPS is enabled then use GPS other wise Network.
if(manager.isProviderEnabled( LocationManager.GPS_PROVIDER))
{
manager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}
else
{
manager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0, mlocListener);
}
{
manager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}
else
{
manager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0, mlocListener);
}
This checks for GPS if enabled then provide accurate distance .
Location listener class is
public class MyLocationListener implements LocationListener
{
public void onLocationChanged(Location location)
{
Toast.makeText(getBaseContext(), "Current loc latitute: "+location.getLatitude() +"longitute: "+location.getLongitude(), Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String provider){}
public void onProviderEnabled(String provider){}
public void onStatusChanged(String provider, int status, Bundle extras){}
}
{
public void onLocationChanged(Location location)
{
Toast.makeText(getBaseContext(), "Current loc latitute: "+location.getLatitude() +"longitute: "+location.getLongitude(), Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String provider){}
public void onProviderEnabled(String provider){}
public void onStatusChanged(String provider, int status, Bundle extras){}
}
You will get current location Latitude and Longitude.
No comments:
Post a Comment