1
我創建了一個位置應用程序,它將在我的設備上的google maps api中顯示當前位置,但我在使用網絡提供商和gps提供商時感到困惑。android-位置應用程序優化
我希望我的應用程序在應用程序打開時使用網絡提供程序,因此它可以快速指向位置。那麼它應該搜索GPS提供商,一旦GPS可用,那麼它應該使用GPS提供商。在運行應用程序時,如果我鬆動GPS連接,它應該返回到網絡提供商,並等待GPS可用。
我的源代碼是
public class MyGoogleMap1Activity extends MapActivity
{
private static final long min_distance = 1; // in Meters
private static final long min_time = 1000; // in Milliseconds
protected LocationManager locationManager;
protected MyLocationListener locationListener;
HelloItemizedOverlay itemizedoverlay;
List<Overlay> mapOverlays;
MapView mapView;
/** Called when the activity is first created. */
@Override public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try
{
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.google_maps_pin);
itemizedoverlay = new HelloItemizedOverlay(drawable);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
min_time,min_distance ,locationListener);
}
catch(Exception e)
{
Toast.makeText(MyGoogleMap1Activity.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
@Override protected boolean isRouteDisplayed()
{
return false;
}
private class MyLocationListener implements LocationListener
{
public void onLocationChanged(Location location)
{
try
{
if (location != null)
{
int lat = (int) (location.getLatitude() * 1E6); //coordinates are in microdegrees
int lng = (int) (location.getLongitude() * 1E6);
GeoPoint point = new GeoPoint(lat, lng);
OverlayItem overlayitem = new OverlayItem(point, "", "");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
MapController myMapController = mapView.getController();
myMapController.animateTo(point);
myMapController.setZoom(16);
}
String message = String.format("Current Location \n Longitude: %1$s \n Latitude: 2$s",location.getLongitude(), location.getLatitude());
Toast.makeText(MyGoogleMap1Activity.this,message, Toast.LENGTH_LONG).show();
}
catch(Exception e)
{
Toast.makeText(MyGoogleMap1Activity.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
Toast.makeText(MyGoogleMap1Activity.this,"Status Changed",Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s)
{
Toast.makeText(MyGoogleMap1Activity.this,"Provider disabled by the user. GPS turned off",Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s)
{
Toast.makeText(MyGoogleMap1Activity.this,"Provider enabled by the user. GPS turned on",Toast.LENGTH_LONG).show();
}
}
}
不,我希望它始終處於活動狀態,所以首先它應該抓住網絡提供商,直到它獲得gps活動。請在代碼中幫助我 – Santosh