求助location为空,无法获取地理位置
书上还有网上,在谷歌官方的api文档的代码都尝试了还是无法获得
LocationManager locManager=(LocationManager)getSystemService(Context.LocationService); Criteria criteria=new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); criteria.setPowerRequirement(Criteria.POWER_LOW); criteria.setAltitudeRequired(false); criteria.setBearingRequired(false); criteria.setCostAllowed(false);//不需要花钱 String provider=locManager.getBestProvider(criteria, true);//addGpsStatusListener(listener); Location location=locManager.getLastKnownLocation(provider); locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, listener);
LocationManager locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE); // Define a listener that responds to locationupdates LocationProvider provider=locationManager.getProvider(LocationManager.NETWORK_PROVIDER); LocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) { // Called when a new location is found by the network makeUseOfNewLocation(location); } public void onStatusChanged(String provider, int status, Bundle extras) {} public void onProviderEnabled(String provider) {} public void onProviderDisabled(String provider) {} }; // Register the listener with the Location Manager to receive location updates locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);