2011-09-22 82 views
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(); 
     } 

    } 

} 

回答

0

这里是如何将不同位置提供

Remeber,网络提供商是非常不准确的之间变戏法的article by Google。它依赖于手机信息。与10到50米的GPS精度相比,网络提供商的精度为100到3000米。 GPS提供商卫星可见度在建筑物或茂密森林/城市地区受损。然而,自推出辅助GPS后,您不必依赖卫星的可视性。 GPS提供商将永远是更好的选择。

+0

不,我希望它始终处于活动状态,所以首先它应该抓住网络提供商,直到它获得gps活动。请在代码中帮助我 – Santosh