将LocationManager方法从MapActivity分离为新类

时间:2012-04-19 10:05:35

标签: android

我是Android的新手,我有这个MapActivity类,它读取用户的位置并在地图上显示。我将LocationListener方法与MapActivity一起放在一个类中。我需要一个建议,如何将两个方法分开是最好的,所以我只需要在MapActivity上调用LocationManager类,因此编码看起来很整洁。

谢谢。

这是班级:

public class MyMapActivity extends MapActivity implements OnClickListener {
    public static final String TAG = "GoogleMapsActivity";
    private MapView mapView;
    private LocationManager locationManager;
    Geocoder geocoder;
    Location location;
    LocationListener locationListener;
    CountDownTimer locationtimer;
    MapController mapController;
    MapOverlay mapOverlay = new MapOverlay();

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.my_map_activity);
        initComponents();

        final ImageButton refresh = (ImageButton) findViewById(R.id.btn_nav_impressum);
        refresh.setOnClickListener(this);

        final ImageButton search =(ImageButton) findViewById(R.id.btn_nav_locater);
        search.setOnClickListener(this);

        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        if (locationManager == null) {
            Toast.makeText(MyMapActivity.this,
                    "Location Manager Not Available", Toast.LENGTH_SHORT)
                    .show();
            return;
        }
        location = locationManager
                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (location == null)
            location = locationManager
                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (location != null) {

            animateMap(location);
            List<Overlay> listOfOverlays = mapView.getOverlays();
            listOfOverlays.clear();
            listOfOverlays.add(mapOverlay);
        }
        locationListener = new LocationListener() {
            @Override
            public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
            }

            @Override
            public void onProviderEnabled(String arg0) {
            }

            @Override
            public void onProviderDisabled(String arg0) {
            }

            @Override
            public void onLocationChanged(Location l) {
                location = l;
                locationManager.removeUpdates(this);
                if (l.getLatitude() == 0 || l.getLongitude() == 0) {
                } else {
                    double lat = l.getLatitude();
                    double lng = l.getLongitude();
                    Toast.makeText(MyMapActivity.this,
                            "Location Are" + lat + ":" + lng,
                            Toast.LENGTH_SHORT).show();
                }
            }
        };
        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
            locationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER, 1000, 10f, locationListener);
        locationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 1000, 10f, locationListener);
        locationtimer = new CountDownTimer(30000, 5000) {
            @Override
            public void onTick(long millisUntilFinished) {
                if (location != null)
                    locationtimer.cancel();
            }

            @Override
            public void onFinish() {
                if (location == null) {
                }
            }
        };
        locationtimer.start();
    }

    public void animateMap(Location location){
        double lat = location.getLatitude();
        double lng = location.getLongitude();
        Toast.makeText(MyMapActivity.this,
                "Sie sind an\n" + lat + "\n" + lng, Toast.LENGTH_SHORT)
                .show();
        GeoPoint point = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
        mapController.animateTo(point, new Message());
        mapOverlay.setPointToDraw(point);
    }

    public MapView getMapView() {
        return this.mapView;
    }

    private void initComponents() {
        mapView = (MapView) findViewById(R.id.mapView);
        mapView.setBuiltInZoomControls(true);
        //mapView.setStreetView(true);
        mapController = mapView.getController();
        mapController.setZoom(16);
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

    class MapOverlay extends Overlay {
        private GeoPoint pointToDraw;

        public void setPointToDraw(GeoPoint point) {
            pointToDraw = point;
        }

        public GeoPoint getPointToDraw() {
            return pointToDraw;
        }

        @Override
        public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
                long when) {
            super.draw(canvas, mapView, shadow);

            Point screenPts = new Point();
            mapView.getProjection().toPixels(pointToDraw, screenPts);

            Bitmap bmp = BitmapFactory.decodeResource(getResources(),
                    R.drawable.pin_normal);
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 24, null);
            return true;
        }
    }

0 个答案:

没有答案