根据此代码,位置始终返回null。请检查我的代码,然后告诉我有什么问题。当我在调试模式下运行项目时,provider =“ network”但位置始终为null。我使用DDMS发送纬度和经度值,但没有变化。我不明白是什么问题

这部分在onCreate方法中

lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);
        criteria.setPowerRequirement(Criteria.POWER_LOW);

        provider = lm.getBestProvider(criteria, true);
        location = lm.getLastKnownLocation(provider);

            updateLocation(location);


onResume方法中的这一部分

@Override
    protected void onResume() {
        super.onResume();
        lm.requestLocationUpdates(provider, 5000, 10, this);
    }


我使用LocationListener(MapActivity实现了LocationListener),这是onLocationChanged部分

public void onLocationChanged(Location location) {
        updateLocation(location);
    }


最后这是我的updateLocation()

private void updateLocation(Location location){
        if (location != null) {
            Double lat = location.getLatitude()*1E6;
            Double lng = location.getLongitude()*1E6;
            point = new GeoPoint(lat.intValue(), lng.intValue());

            mapView = (MapView) findViewById(R.id.mapView);
            mc = mapView.getController();
            mapView.setBuiltInZoomControls(true);

            //List<Overlay> overlays = mapView.getOverlays();
            myLocOverlay = new MyLocationOverlay(this, mapView);
            //overlays.add(myLocOverlay);
            mapView.getOverlays().add(myLocOverlay);
            myLocOverlay.enableMyLocation();

            myLocOverlay.runOnFirstFix(new Runnable() {
                public void run() {
                    mc.animateTo(myLocOverlay.getMyLocation());
                    }
            });

            mc.setZoom(15);
            mapView.invalidate();

            }

    }


我已授予基本权限,并且使用了Google Maps库

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />

<uses-library
            android:name="com.google.android.maps"
            android:required="true" >
        </uses-library>

最佳答案

我认为您应该使用LocationManager.GPS_PROVIDER请求。您可以测试我的代码:

mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1l,1f, new LocationListener() {
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
         // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

    }

    @Override
    public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

    }

    @Override
    public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
        updateLocation(location);
    }
    });


祝好运!
^-^

07-24 13:39