简体   繁体   English

获取当前位置而无需在Android中使用地图

[英]get current location without using map in android

how to get the current user location without using a map in the activity? 如何在活动中不使用地图的情况下获取当前用户位置? i just want to store the coordinates of the location ? 我只想存储位置的坐标? i used this code but it doesn't works ! 我使用了这段代码,但是没有用!

LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

Criteria criteria = new Criteria();

String provider = locationManager.getBestProvider(criteria, true);

Location location = locationManager.getLastKnownLocation(provider);
latitude = location.getLatitude();
longitude = location.getLongitude();

This is what I am using in my app, working fine : 这是我在应用程序中使用的,工作正常:

 public class TheLocationClass implements LocationListener  {
    final long MIN_TIME_INTERVAL = 60 * 1000L;
    private boolean isGPSEnabled = false;
    private boolean isNetworkEnabled = false;
    private Location location;          //  location.getLatitude() & location.getLongitude() will give you what you need...
    private LocationManager locationManager;

    public TheLocationClass()  {
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    }

    public void requestForUpdates(){

        if(locationManager == null) {
            return;
        }

        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (isNetworkEnabled) {
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,TheLocationClass.this);
            if (locationManager != null) {
                Location tempLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                if (tempLocation != null && isBetterLocation(tempLocation,location))
                    location = tempLocation;
            }
        }
        if (isGPSEnabled) {
            locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, TheLocationClass.this, null);
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0, TheLocationClass.this);
            if (locationManager != null) {
                Location tempLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                if (tempLocation != null  && isBetterLocation(tempLocation,location))
                    location = tempLocation;
            }
        }
    }

    public void stopLocationUpdates(){
        if(locationManager!=null){
            locationManager.removeUpdates(TheLocationClass.this);
        }
    }

    private boolean isBetterLocation(Location location, Location currentBestLocation) {
        if (currentBestLocation == null) {
            return true;
        }

        // Check whether the new location fix is newer or older
        long timeDelta = location.getTime() - currentBestLocation.getTime();
        boolean isSignificantlyNewer = timeDelta > MIN_TIME_INTERVAL;
        boolean isSignificantlyOlder = timeDelta < -MIN_TIME_INTERVAL;
        boolean isNewer = timeDelta > 0;

        // If it's been more than two minutes since the current location, use the new location because the user has likely moved
        if (isSignificantlyNewer) {
            return true;
        } else if (isSignificantlyOlder) {
            return false;
        }

        // Check whether the new location fix is more or less accurate
        int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
        boolean isLessAccurate = accuracyDelta > 0;
        boolean isMoreAccurate = accuracyDelta < 0;
        boolean isSignificantlyLessAccurate = accuracyDelta > 200;

        // Check if the old and new location are from the same provider
        boolean isFromSameProvider = isSameProvider(location.getProvider(),currentBestLocation.getProvider());

        // Determine location quality using a combination of timeliness and accuracy
        if (isMoreAccurate) {
            return true;
        } else if (isNewer && !isLessAccurate) {
            return true;
        } else if (isNewer && !isSignificantlyLessAccurate  && isFromSameProvider) {
            return true;
        }
        return false;
    }

    private boolean isSameProvider(String provider1, String provider2) {
        if (provider1 == null) {
            return provider2 == null;
        }
        return provider1.equals(provider2);
    }

    @Override
    public void onLocationChanged(Location loc) {
        if (loc != null  && isBetterLocation(loc, location)) {
            location = loc;
        }
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
}

Just create instance of this class and call requestForUpdates(). 只需创建此类的实例并调用requestForUpdates()。

Variable location has newly updated accurate location of device. 可变位置具有最新更新的设备精确位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM