gpt4 book ai didi

java - 获取当前用户位置

转载 作者:行者123 更新时间:2023-12-02 05:09:01 25 4
gpt4 key购买 nike

我正在尝试获取当前用户位置,但在某些设备上它可以工作,但在某些设备上我一直在等待位置。

这是我的代码:

private final LocationListener mLocationListener = new LocationListener() {
@Override
public void onLocationChanged(final Location location) {
// TODO
Log.v("--", "get address 121");
Main.this.location = location;
getAddress();
locationManager.removeUpdates(mLocationListener);
}

@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
Log.v("--", "provider enabled");
}

@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Log.v("--", "provider disabled");
}
};

public void putExtra() {
Intent intent = new Intent(this, Settings.class);
intent.putExtra("timesobj", times);
startActivity(intent);
}

/**
* Get current/last known location and display city, country name and update
* calculations
* */
public void getAddress() {
Log.v("--", "get address 1");
boolean isGPSProviderEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean network_enabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
Log.v("--", "get address 31 " + isGPSProviderEnabled + " gps - "
+ isConnectedToNetwork());

if (isGPSProviderEnabled || network_enabled) {
Log.v("--", "get address 2");
Criteria c = new Criteria();
Log.v("--", "provider " + locationManager.getBestProvider(c, true));
location = locationManager.getLastKnownLocation(locationManager
.getBestProvider(c, false));

if (location == null) {
Log.v("--", "get address 6");
locationManager.requestLocationUpdates(
locationManager.getBestProvider(c, false), 1000, 100,
mLocationListener);
} else {
Log.v("--", "get address 3");
if (isConnectedToNetwork()) {
new AsyncTask<Void, Void, Void>() {
protected Void doInBackground(Void... params) {
try {
com.myapp.utils.Geocoder geocoder = new com.myapp.utils.Geocoder(
Main.this);
GeocoderModel geocoderModel = geocoder
.getFromLocation(
location.getLatitude(),
location.getLongitude(), 5);
city = geocoderModel.getCity();
country = geocoderModel.getCountry();
prefs.edit().putString(Constants.CITY, city)
.apply();
Log.v("--", "get address 4");
} catch (IOException e) {
Log.v("--", "get address 11");
e.printStackTrace();
} catch (LimitExceededException e) {
Log.v("--", "get address 12");
e.printStackTrace();
}
return null;
};

protected void onPostExecute(Void result) {
prefs.edit().putString(Constants.COUNTRY, country)
.apply();
prefs.edit().putString(Constants.CITY, city)
.apply();
populateList(location);
};
}.execute();
} else {
city = prefs.getString(Constants.CITY,
getString(R.string.app_name));
Log.v("--", "get address 33 " + location.getLatitude());
populateList(location);
}
}
} else {
Log.v("--", "get address 5");
startGpsEnableDialog();
}
}

这是我的日志:

V/--      ( 5498): get address 1
V/-- ( 5498): get address 31 true gps - true
V/-- ( 5498): get address 2
V/-- ( 5498): provider gp
V/-- ( 5498): get address 6

有人可以帮我看看这段代码中可能有什么问题,并告诉我如何正确获取用户位置吗?

谢谢!

最佳答案

尝试换行

locationManager.getBestProvider(c, false)

locationManager.getBestProvider(c, true)

如上所述here ,“enabledOnly - 如果为 true,则仅返回当前启用的提供程序”。您可能会找回未启用的提供程序。
另一件事,你似乎正在使用 Criteria,我通常不使用它,但从你的代码来看,你似乎没有用它做太多事情。我建议你再看一下。我找到了一个代码示例here这可能对你有帮助。

private void startLocationStuff(){
locManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
locListener=new MyLocationListener();
final Criteria criteria=new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
bestProvider=locManager.getBestProvider(criteria,true);
}

您可能需要考虑使用位置管理器,而不使用 this 等条件。 .

关于java - 获取当前用户位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27505198/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com