Geolocation - Location returns always null - android

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

  Location returns always null from this code. Please check my codes and tell me whats wrong. When I run the project on debug mode, provider="network" but location always null. I send latitude and longitude values by using DDMS but doesn t change. I don t understand whats is the problem   
  This part in the onCreate method   
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);
  This part in the onResume method   
@Override   
    protected void onResume() {  
        super.onResume();
        lm.requestLocationUpdates(provider, 5000, 10, this);
    }
  I use LocationListener (MapActivity implements LocationListener) and this is onLocationChanged part    
public void onLocationChanged(Location location) {
        updateLocation(location);   
    }
  finally this is my 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();

            }

    }
  I gave essential permissions and I use google maps library   
<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>

Answers

I think u should use LocationManager.GPS_PROVIDER to request. You can test my code:

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);               
    }
    });

Good luck! ^-^

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/11755921/location-returns-always-null-android

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils