本文介绍了Android一次获取用户位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这周围有很多事情,但是即使我(几乎)全部阅读了之后,仍然无法弄清问题所在.

I know that there are a lot of things around this but even after I read them all (almost) I still can't figure it out the issue.

基本上,我想在打开应用程序或按一个按钮并保存在共享首选项"时获取用户的精细位置".我的旧代码可以正常工作,但是由于获取坐标的速度很慢,因此无法保存共享的首选项(GPS图标未显示在栏中).

Basically I want to get the user FINE LOCATION when I open the app or press a button and save on Shared Preferences.My old code worked but since was slow to get the coordinates, it fails to save on sharedpreferences (GPS icon don't show on bar).

OLD:

 public void askLocation() {
    if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        String provider = locationManager.getBestProvider(criteria, true);
        try {
            mLocation = locationManager.getLastKnownLocation(provider);
        } catch (SecurityException e) {
            //
        }

        String currentCoordinates = somename.getString("LastLocation", "0,0");

        if (mLocation != null) {
            double currentLatitude = mLocation.getLatitude();
            double currentLongitude = mLocation.getLongitude();

            currentCoordinates = currentLatitude + "," + currentLongitude;
        }

        SharedPreferences.Editor editor = somename.edit();
        editor.putString("LastLocation", currentCoordinates).commit();

        Log.d("SomeName", "lastLocation start: " + currentCoordinates);
    }
}

我在Stackoverflow上的帖子中使用的我的新代码未正确显示(GPS图标显示在栏中):

My New code, used from a post on Stackoverflow, is not getting properly (GPS icon display on bar):

public void askLocation() {
    if (ContextCompat.checkSelfPermission(MainActivity.this,                                                 Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)         {
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_COARSE);
        criteria.setPowerRequirement(Criteria.POWER_LOW);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setSpeedRequired(false);
        criteria.setCostAllowed(true);
        criteria.setHorizontalAccuracy(Criteria.ACCURACY_HIGH);
        criteria.setVerticalAccuracy(Criteria.ACCURACY_HIGH);

        // Now create a location manager
        LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        Looper looper = null;
        locationManager.requestSingleUpdate(criteria, locationListener, looper);

        String currentCoordinates = somename.getString("LastLocation", "0,0");
        Log.d("SomeName", "lastLocation askLocation: " + currentCoordinates);
    }
}

final LocationListener locationListener = new LocationListener() {
    @Override
    public void onLocationChanged(Location location) {
        mLocation = location;
        String currentCoordinates = somename.getString("LastLocation", "0,0");

        if (location != null) {
            double currentLatitude = location.getLatitude();
            double currentLongitude = location.getLongitude();

            currentCoordinates = currentLatitude + "," + currentLongitude;
        }

        SharedPreferences.Editor editor = somename.edit();
        editor.putString("LastLocation", currentCoordinates).commit();
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        Log.d("Status Changed", String.valueOf(status));
    }

    @Override
    public void onProviderEnabled(String provider) {
        Log.d("Provider Enabled", provider);
    }

    @Override
    public void onProviderDisabled(String provider) {
        Log.d("Provider Disabled", provider);
    }
};

谢谢.

推荐答案

打开应用程序或按一个按钮并保存在共享首选项"时,您可以尝试使用此方法获取一次位置.

You can try this method to get the location once when open the app or press a button and save on Shared Preferences.

               if (Constants.isOnline(getApplicationContext())) {
                        gps = new GPSTracker(v.getContext(),this);
                        if (gps.canGetLocation()) {

                            latitude = gps.getLatitude();
                            longitude = gps.getLongitude();
                            Log.e("Location ", "latitude : " + latitude + "longitude :" + longitude);

                        } else {
                            gps.showSettingsAlert();
                        }
                }

isOnline()用于检查用户是否具有Internet连接.

And the isOnline() is to check user has intenet connection on or not.

 public static boolean isOnline(Context c) {
    ConnectivityManager cm = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    } else {
        Toast.makeText(c, "No Internet Connection.", Toast.LENGTH_SHORT).show();
    }
return false;

}

这篇关于Android一次获取用户位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 08:47
查看更多