开发者

android- location application optimizing

I have created a location application which will show the current location in the google maps api on my device, but i am confused in using network provider and gps provider.

I want my application to use the network provider when the application is opened, so it can quickly point the location. then it should search for gps provider, once gps is available then it should use the gps provider. during running the application if I loose gps connectivity it should go back to network provider and wait until gps is available.

my source code is

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);
     开发者_如何学JAVA               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();
        }

    }

}


Here is an article by Google on how to juggle between various location providers

Remeber that the network provider is very inaccurate. It relies on cell-site information. Compared to GPS accuracy of 10 to 50 meters, the Network providers accuracy is 100 to 3000m. GPS providers satellite visibility is impaired in buildings or dense forests/urban areas. However since the introduction of assisted GPS, you don't have to rely on satellite visibility. GPS provider will always be the better choice.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜