【问题标题】:Getting position from class android从类android获取位置
【发布时间】: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


【解决方案1】:

您的位置设置在OnSuccessListener&lt;Location&gt;(),它不会立即调用。因此,您设置了一个侦听器,但只有在获得您的位置时才会调用它。 所以,您需要做的是:在您的侦听器的 onSuccess(Location location) 方法中执行 GetLoc() 中定义的所有逻辑。

【讨论】:

    【解决方案2】:

    可能并非所有位置设置都得到满足。您应该使用 SettingsClient 类提示用户更改其位置设置。 此处的文档:https://developer.android.com/training/location/change-location-settings

    基本上你必须:

    • 创建位置请求
    protected void createLocationRequest() {
        LocationRequest mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(10000);
        mLocationRequest.setFastestInterval(5000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    }
    
    • 获取当前位置设置
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
    
    
    SettingsClient client = LocationServices.getSettingsClient(this);
    Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());
    
    • 添加任务完成侦听器以了解您的位置设置是否正常
    task.addOnSuccessListener(this, new OnSuccessListener<LocationSettingsResponse>() {
        @Override
        public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
            // All location settings are satisfied. The client can initialize
            // location requests here.
            // ...
        }
    });
    
    task.addOnFailureListener(this, new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            if (e instanceof ResolvableApiException) {
                // Location settings are not satisfied, but this can be fixed
                // by showing the user a dialog.
                try {
                    // Show the dialog by calling startResolutionForResult(),
                    // and check the result in onActivityResult().
                    ResolvableApiException resolvable = (ResolvableApiException) e;
                    resolvable.startResolutionForResult(MainActivity.this,
                            REQUEST_CHECK_SETTINGS);
                } catch (IntentSender.SendIntentException sendEx) {
                    // Ignore the error.
                }
            }
        }
    });
    

    完成这三个步骤后,您将能够获取用户位置

    【讨论】:

      猜你喜欢
      • 2015-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多