Android一次获取用户位置
Posted
技术标签:
【中文标题】Android一次获取用户位置【英文标题】:Android get User Location once 【发布时间】:2018-02-14 20:35:53 【问题描述】:我知道这方面有很多东西,但即使在我(几乎)全部阅读之后,我仍然无法弄清楚问题所在。
基本上,当我打开应用程序或按下按钮并保存在共享首选项时,我想获得用户 FINE LOCATION。 我的旧代码有效,但由于获取坐标的速度很慢,因此无法保存共享首选项(GPS 图标不显示在栏上)。
旧:
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);
我在 *** 上的帖子中使用的新代码无法正常运行(GPS 图标显示在栏上):
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);
;
谢谢。
【问题讨论】:
【参考方案1】:您可以尝试此方法在打开应用程序或按下按钮并保存在共享首选项时获取一次位置。
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() 用于检查用户是否打开了互联网连接。
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;
【讨论】:
Log.e("Location ", "latitude : " + latitude + "longitude :" + longitude);
会给你位置
我认为那里还缺少其他东西?什么是“gps”?
GPSTracker类的一个对象,你可以从GPSTracker找到这个类,你可以有更多的解释以上是关于Android一次获取用户位置的主要内容,如果未能解决你的问题,请参考以下文章
Android:使用FusedLocationProviderClient获取一次位置?
Android:FusedLocationProvider,每隔几秒获取一次位置
如何在 Android 中使用后台服务每 5 分钟获取一次设备位置?