0

我的 Android 应用程序中有以下 onCreate() :

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.customer_map);

        context=getApplicationContext();

        mapView = (MapView) findViewById(R.id.map);
        mapView.setBuiltInZoomControls(true);
        mapView.setEnabled(true);
        mapOverlays = mapView.getOverlays();


        //set my location marker
        myLocationOverlay = new MyLocationOverlay(context, mapView);
        myLocationOverlay.disableCompass();
        myLocationOverlay.enableMyLocation();
        mapOverlays.add(myLocationOverlay);

        myLocationOverlay.runOnFirstFix(new Runnable() {
            public void run() {
                mapView.getController().animateTo(myLocationOverlay.getMyLocation());
            }
        });



        //and zoom to current location
        MapController mc=mapView.getController();
        GeoPoint centre=new GeoPoint((int)(getBestLocation().getLat()*1e6),(int)(getBestLocation().getLng()*1e6));
        mc.setCenter(centre);
        mc.animateTo(centre);
        mc.setZoom(12);
    }

如您所见,我正在尝试显示当前位置。但它根本行不通!我想我在做一些愚蠢的事情,但在过去的一个小时里我无法弄清楚。

任何见解都非常感谢。提前谢谢了,

ps这里是getBestLocation:

private Coords getBestLocation() {
        Coords returnCoords=new Coords();

        Criteria criteria=new Criteria();
        //criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setAccuracy(Criteria.POWER_LOW);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);


        lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        String provider=lm.getBestProvider(criteria, true);
        if(provider!=null){
            lm.requestLocationUpdates(provider, 2000, 1, this);
            List<String> providers = lm.getProviders(false);
            //Loop over the array backwards, and if you get an accurate location, then break out the loop
            Location l = null;
            for (int i=providers.size()-1; i>=0; i--) {
                l = lm.getLastKnownLocation(providers.get(i));
                if (l != null ){
                    returnCoords.setLat(l.getLatitude());
                    returnCoords.setLng(l.getLongitude());
                    break;
                    //Log.i("EOH",String.valueOf(l.getLatitude()));
                }
            }
        }else{
            Toast.makeText(context, "Location not available", 3000).show();
        }
        return returnCoords;
    }
4

1 回答 1

0

好的,它现在正在工作。愚蠢的是,我正在这样做:

@Override
    protected void onPause() {
        super.onPause();
        if(myLocationOverlay!=null){
            myLocationOverlay.disableMyLocation();
            myLocationOverlay.disableCompass();
        }
    }
    @Override
    protected void onResume() {
        super.onResume();
        if(myLocationOverlay!=null){
            **myLocationOverlay.disableLocation();**
            myLocationOverlay.disableCompass();
        }
    }

我是个笨蛋!

于 2012-07-06T13:07:38.713 回答