【发布时间】:2019-02-19 01:58:32
【问题描述】:
我正在寻找一个课程来获得一个获得纬度和经度的位置。 我从片段中调用这个类,这得到了最好的提供者并调用了一个方法来获取一个位置变量。我有这个变量位置,但是当我返回变量 pos 时,位置为空;为什么?我要传递这个变量?
var pos into 方法没问题,但是当我返回时为 null。
public class locationmng {
public Context context;
private FusedLocationProviderClient fusedLocationProviderClient;
Location position;
public locationmng(Context c){
context=c;
}
public Location GetLoc() {
if(ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
LocationManager lm = (LocationManager)
context.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = lm.getBestProvider(criteria, true);
if (bestProvider.equals(LocationManager.NETWORK_PROVIDER)) {
position = getcoarseposition();
return position;
}
}
@SuppressLint("MissingPermission")
public Location getcoarseposition() {
final Location[] pos = new Location[1];
fusedLocationProviderClient=
LocationServices.getFusedLocationProviderClient(context);
fusedLocationProviderClient.getLastLocation()
.addOnSuccessListener((Activity) context, new
OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null) {
pos[0] =location;
}else {
Log.i("locmng","not found loc ");
}
}
});
Log.i("locationmanager6894","ho trovato "+ a[0].getLongitude()+ "lat: "+ a[0].getLatitude());
return pos[0];
}
【问题讨论】:
-
你试过我的代码了吗?
标签: android class methods geolocation location